If you have basic foundation of mathematics, you should heard about function. Functions in mathematics and programming are similar concept but differ in their execution and purpose.
In Math, a function is a relation between a set of inputs (we call it domain) and a set of possible outputs (call it range), where each input is related to exactly one output.
However, in programming:
A function is a block of code designed to perform a specific task. It can take inputs (we call it parameters) and return a value.
In Lua, you can use function to organize your code into reusable blocks, making it more modular and easier to understand. In this tutorial, I will cover the basics of creating and using functions in Lua, remember to complete the examples to guide you through the process.
How to Create Functions in Lua?
In Lua, you define a function using the function
keyword followed by the function name and a set of parentheses. Inside the parentheses, as mentioned, you can specify the parameters. After setting the parameters, the function will accept it. And then inside the function body, it contains the code to be executed when the function is called. Let’s see the basic syntax:
Basic Function Definition
function functionName(parameters)
-- Function body
end
Let’s start with a simple function that you can print a greeting message:
function greet()
print("Hello, World!")
end
-- Call the function
greet() -- Output: Hello, World!
As you can see, the function named greet() is defined. Once you call the function in the Lua program, it print out “Hello, World!”. One of the benefit of creating function is you are able to “CALL” multiple time of the function.
Functions with Parameters
As mention earlier, functions can take parameters. That’s mean the pass-in values as variables can hold the values passed to the function when it is called. Let’s see how it’s work in below example:
Lua Function with Parameters
function greet(name)
print("Hello, " .. name .. "!")
end
-- Call the function with different arguments
greet("Alice") -- Output: Hello, Alice!
greet("Bob") -- Output: Hello, Bob!
In the above example, every time you pass a new value inside the function, e.g. Alice, Bob, the function will print out the name. Passing parameters with function is extremely useful. You can design a much more complicated function to archive more dynamic tasks.
Lua Function with Return Values
In some situations, you may want to return a value by calling a function. For example, assume you made a function to calculate the sum of two numbers. By using the return keyword, the function will return a value. Meaning that you are allowed to pass data back from the function to the caller.
Example of Function with Return Value in Lua
function add(a, b)
return a + b
end
-- Call the function and store the result
local sum = add(5, 3)
print(sum) -- Output: 8
Lua Function with Multiple Return Values
Lua functions can also return multiple values. This can be useful in various situations where you need to return more than one result from a function.
Example of Function with Multiple Return Values
function divide(a, b)
local quotient = a / b
local remainder = a % b
return quotient, remainder
end
-- Call the function and capture multiple return values
local q, r = divide(10, 3)
print("Quotient: " .. q) -- Output: Quotient: 3.3333333333333
print("Remainder: " .. r) -- Output: Remainder: 1
Scope: Local Functions
By default, functions are global in Lua, meaning they can be accessed from anywhere in your code. However, you can also define local functions that are only accessible within a specific block of code. We will use local keyword to define the scope.
Example of Local Function in Lua
local function multiply(a, b)
return a * b
end
-- Call the local function
local product = multiply(4, 5)
print(product) -- Output: 20
Lua Higher Order Functions
In Lua, functions are first-class values, meaning you can store them in variables, pass them as arguments to other functions, and return them from other functions.
Example of Higher-Order Function
function applyOperation(a, b, operation)
return operation(a, b)
end
-- Define some basic operations
function add(a, b)
return a + b
end
function subtract(a, b)
return a - b
end
-- Use applyOperation with different operations
local sum = applyOperation(10, 5, add)
local difference = applyOperation(10, 5, subtract)
print("Sum: " .. sum) -- Output: Sum: 15
print("Difference: " .. difference) -- Output: Difference: 5
Conclusion
Remember that functions are a powerful concept in any programming. You can write modular, reusable code. You can try more by defining functions with parameters and return values, and then you should have flexible and efficient scripts in your game. Noted that manage scope is important. By defining local functions when you need to limit their scope to specific blocks of code.