Functions In Lua

Functions in Lua can receive parameters, perform the specified code and return back to the place where they are called. Functions are designated by the function keyword and should not be the same as a reserved keyword and cannot not start with a number.

This function named sum has two parameters a and b, which are formed and come back. To call our function, we write her name and assigns the result of a variable or function outputs directly.

In this example, the result of a function is assigned to the variable res, which is then displayed. In the last line, we will immediately display the function result to the screen.

The function does not need to have parameters, you can do without them. Also, the function is not required to return a value.

In this example, the function is used within a loop that displays the numbers from 1 to 5.

Usually, the main function of the code written below.