Lua Functions - Lua (programming language)

What is the use of functions in Lua?

Function in Lua is a group of statements which perform a task together. Code can be divided into separate functions and the division usually unique. Therefore each function will perform a specific task.

Lua language offers several built-in methods which can be called by a program. For example, method print() to print the argument passed as input in console.

A function can be known with different names like a method or a sub-routine or a procedure etc.

Defining a Function

General form of a method definition in Lua programming language is as shown below

Method definition in Lua programming language will consist of a method header and a method body. All the parts of a method are mentioned below

  • Optional Function Scope − keyword local can be used to limit the scope of the function or ignore the scope section. This will make it a global function.
  • Function Name − this is the actual name of the function and the function name and the parameter list will mutually represent the function signature.
  • Arguments − An argument is like a placeholder and when a function is invoked, a value can be passed to the argument. This value is referred to as the actual parameter or argument. Parameter list refers to the type, order, and number of the arguments of a method. Arguments are optional i.e.; a method can contain no argument.
  • Function Body − method body will contain a collection of statements which define what the method does.
  • Return − In Lua, it is possible to return multiple values by following the return keyword with the comma separated return values.

Example

Below is the source code for a function called max(). This function takes two parameters num1 and num2 and returns the maximum between the two

Function Arguments

If a function wants to use arguments, it should declare the variables which accept the values of the arguments. These variables will be called as the formal parameters of the function.

Formal parameters will behave like other local variables inside the function and will be created upon entry into the function and destroyed upon exit.

Calling a Function

While creating a Lua function, you will be providing a definition of what the function has to do. To use a method, you should call that function to perform the defined task.

When a program calls a function, program control will be transferred to the called function. Called function will perform the mentioned task and when the return statement is executed or when the function's end is reached, it will return program control back to the main program.

To call a method, you just have to pass the required parameters along with the method name and if the method returns a value, then you can store the returned value. For example

When you run the above code, below output will be displayed.

Assigning and Passing Functions

In Lua, a function can be assigned to variables and they can also be passed as parameters of another function. Below is a simple example for assigning and passing a function as parameter in Lua.

When you run the above code, below output will be displayed.

Function with Variable Argument

Functions can be created with variable arguments in Lua using '...' as its parameter. You can understand this better by seeing an example in which the function returns the average and it can take variable arguments.

When you run the above code, below output will be displayed.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Lua (programming language) Topics