Updated May 22, 2023
Definition of Lua print
Lua print in Lua programming language is an inbuilt function that is used for printing the required statement in the console. Lua print has lots of advantages associated with it specially when it comes on the development and implementation aspect as programmers mostly need this print functions many times to debug or to print a required message which is necessary for acknowledgement. There are many ways of using Lua print some are like just printing of the passed parameter as output to the console, printing of the statement in two lines, and many more.
Syntax:
The Syntax flow for Lua print is as follows :
Optional_function_scope fnction_name (arg1, arg2, arg3…arg_n);
Function_body
Return_type
- Optional_function_scope: Represents the scope of the function or variable like local or global.
- Function_body: The body is the content of the function that it comprises of.
- Return_type: Return type is the value that needs to be returned.
How print function works in Lua?
- Lua is a programming language that is used for writing and acknowledging the programmers with the help of inbuilt functions like Lua print.
- Calling a Lua print function helps to provide a better view to the development and implementation as it helps in making overall coding interesting and easy to adopt.
- Lua print function is so easy to use and manipulate that just after feeding an input it gives the output in the console that is used for printing the required result.
- A function in Lua is the best feature to be used as it includes many build-in functions that are used for usage and manipulation with incorporation of some of the braces and arithmetic operations. Just calling the function is what we need to perform that’s it.
- Once the function is called all the code present within the function starts its execution and can output the necessary input fed into the function and code.
- If a function same as a print function does not have any values to print the message or say if it is empty then, in that case, it is very much important to keep in mind the function requires bare minimum values to work properly and have to provide these values within the brackets by any means by providing a comma-separated list in the brackets.
- Values that are provided within the braces and are taken by functions to do and complete its job are known as parameters and play a very pivotal role in print function of Lua programming language as well.
- It is not mandatory that any function will take any parameter it thus depends upon the requirement that functions usually takes certain types of parameters only according to requirement.
- Suppose there is a scenario where the function wants to print sentences then in that case it will consider mostly string datatype as parameter. The string datatype mostly comprises of characters and letters to frame any sentence.
- It is not that complex thing to implement and perform coding in Lua but then it depends on some of the basic things that are needed to be kept in mind while a print function is encountered for basic development and implementation.
- The basic working is like it takes a string, array, or whichever data structure as input to be shown as the output whenever the function is called.
- It’s the worth noticing thing in Lua print is that the print function automatically puts a new line at the end of whatever is tried to be printed, so if print function is called twice then the line break for segregating two print statements will occur automatically without any other special characters.
Examples
Let us discuss examples of Lua print.
Example #1
This program demonstrates the print function which is called at the time of finding & comparing the minimum value within the print function as shown in the output.
Code:
function min(a_0, b_1)
if (a_0 < b_1) then
rsltnt_vl = a_0;
else
rsltnt_vl = b_1;
end
return rsltnt_vl;
end
print("Comparison is_made b/w minimum_value out of two is: ",min(5,12))
print("Comparison is_made b/w minimum_value out of two is: ",min(6,18))
Output:
Example #2
This program demonstrates the String with some values and calling of print function to print the string with values as shown in the output.
Code:
st_0 = "Fruits"
print("\"st_0 appears as :\"",st_0)
st_1 = 'Animals'
print("st_1 appears as :",st_1)
st_2 = [["Vehicles"]]
print("st_2 appears as :",st_2)
Output:
Example #3
This program demonstrates the working of an array with the Lua print function by printing the values provided as part of the array as shown in the output.
Code:
arr_0 = {}
for j_vl=5,8 do
arr_0[j_vl] = {}
for l=9,11 do
arr_0[j_vl][l] = j_vl*l
end
end
for j_vl=5,8 do
for l=9,11 do
print(arr_0[j_vl][l])
end
end
Output:
Example #4
This program demonstrates the printing of average variable with manipulation of the arguments present within the function as shown in the output.
Code:
function avg_0(...)
rslt_vl = 0
local arg_1 = {...}
for j,k in ipairs(arg_1) do
rslt_vl = rslt_vl + k
end
return rslt_vl/#arg_1
end
print("The avg_val is",avg_0(8,6,3,4,5,2))
Output:
Example #5
This program demonstrates the stateful iterators which is used for traversing in the required arrays and then calls the Lua print function for acknowledgement as shown in the output.
Code:
ar_3 = {"apple", "banana"}
function elmnt_itrt (collection)
local indx = 0
local count = #collection
return function ()
indx = indx + 1
if indx <= count
then
return collection[indx]
end
end
end
for elmnt in elmnt_itrt(ar_3)
do
print(elmnt)
end
Output:
Example #6
This program demonstrates the sorting table which will print the sorted cars within an array shown in the output.
Code:
cars = {"santro","celerio","kwid","duster"}
for a,b in ipairs(cars) do
print(a,b)
end
table.sort(cars)
print("Table Sorted")
for a,b in ipairs(cars) do
print(a,b)
end
Output:
Conclusion
Lua is a programming language that is very easy and flexible to adopt and understand. It helps in making the build-in functions like Lua print usage quite pivotal. Due to its versatile and easy nature, the programmers are interested to use and adopt this programming language for development and implementation of various projects.
Recommended Articles
We hope that this EDUCBA information on “Lua print” was beneficial to you. You can view EDUCBA’s recommended articles for more information.