A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provide better modularity for your application and a high degree of code reusing.
You may consider that also the own programming language provides many build-in functions in addition to the functions you may create.
Let see at first its syntax:
local function nameFunction() -- Insert stuff end -- Calling the function nameFunction()
This simple syntax starts with the local keyword, which limit its scope to the file where this function is declared, at next the function keyword followed with the function's name, which describe the scope of this function, a couple of brackets are added after, and at the end the end keyword must be inserted to close the function.
Note that the function at this point will do nothing until you may want to call it by typing the name of the function followed with the brackets.
What i will insert inside these brackets?
Inside this brackets some arguments are added to manage the behavior of the function's code, and at the same time the value you want to retrieve is interchangeable, depending on which argument you want to pass in.
This example demonstrates how the syntax should be:
local function addNumbers(num1, num2, num3) print(num1 + num2 + num3) end -- Call the function to retrieve the value of addNumbers() addNumbers(3, 6, 2) -- Call again the same function with other arguments addNumbers(12, 54, 43)
By typing lua followed by the filename where you are writing this code followed with .lua format, inside your terminal you can retrieve these values.
As you can see when the function is called, it prints the sum of the three values you have passed inside brackets, so at first it prints 11 and at the second call it prints 109.
As i said before, all programming languages have many built-in functions that have their own behavior like print() function.
Now let see in action one of them using another function we create:
local function retrieveType(var1, var2, var3, var4) print(type(var1), type(var2), type(var3), type(var4)) end -- Declare some variables local name = "Jack" local age = 24 local old = (true ~= false) local func = function() end -- Now let's call the function with passing these variables retrieveType(name, age, old, func)
The type() function let you retrieve the type of the variable you want to check.
After you run this chuck of code, you'll see the output of:
string number boolean function
What should i do if i want to retrieve a value from a function?
The return statement is the best way to retrieve a precise value that you want to store, for example within a variable,by entering the name of the function with the argument as the value of the variable.
Let see this in action:
local function someValues(val1, val2, val3) local sum = (val1 + val2) / val3 return sum end -- Now by declaring a variable you can retrieve the value of sum inside the function local sum = someValues(123, 44, 3) print("sum = " .. sum)
This output should be:
sum = 55.666666666667
As you can see, this value is a bit ugly, so to avoid this number it will also be called math.floor() or math.ceil().
Continuing with the previous chunk of code, we'll have:
print("sum floor = " .. math.floor(sum)) print("sum ceil = " .. math.ceil(sum))
sum floor = 55
sum ceil = 56
You may now notice that the math.floor() function returns the integer smaller than (or equal to) sum and math.ceil() returns the integer larger than (or equal to) sum.
Check this video to see the functions in action: