Programming & Scripting Tutorials

Lua: Functions





You can create functions in lua to do tasks for you when you call them.
To create a function we type "function" and then the name of the function, then the parameters we want the function to take (the values we give the function).
Read through this example:

function squared(input)
print(input*input)
end

It creates a function called 'squared' and 'squared' needs for input to be specified when we call it, then it prints the input squared (input time input).
We would call this function like so:

squared(5)

In the above example we are specifying input as 5, therefore it will output 25.
Functions are so useful, as now if we wanted to square a different number we could just call the function again!
The full code for this program might look something like this:

function squared(input)
print(input*input)
end

squared(2)
squared(5)
squared(7)
squared(1)

We can make the function accept more parameters by separating them with commas like so:

function squaredtwo(input1, input2)
print(input1*input1)
print(input2*input2)
end

Now all the functions we have made are all well and good, however maybe we wanted to customise the output a bit; To do this instead of making the function print the answer, we can make it RETURN the value, this means that when we call the function it gives us back a value, so we can call the function in print or io.write:

function squared(input)
return input*input
end

io.write("Two Squared: ", squared(2), "\n")
io.write("Five Squared: ", squared(5), "\n")


The great this about lua functions is that they can return more than one value! Its always a pain in C++ or Java when you can only return one value, so lua can return two!!
This is were we can really user the "squaredtwo" function we made earlier, to make it return two values we simply seperate them by a comma!

function squaredtwo(input1, input2)
squareofone = nil
squareoftwo = nil
squareofone = input1*input1
squareoftwo = input2*input2
return squareofone, squareoftwo
end


print(squaredtwo(2, 5))

Also because of lua's very cool variable assignment methods we could put the two values in variables for us to use:

function squaredtwo(input1, input2)
squareofone = nil
squareoftwo = nil
squareofone = input1*input1
squareoftwo = input2*input2
return squareofone, squareoftwo
end

valueOne, valueTwo = squaredtwo(2, 5)
print(valueOne .. "\n" .. valueTwo)

This cool assignment method means we could also switch two values around if we wanted to (without annoying temp variables), for example:

var1 = 5
var2 = 10

var1, var2 = var2, var1

print(var1 .. "\n" .. var2)

Going back to the functions a second, you can also create functions with an unknown ammounts of parameters (another one of lua's useful tricks), for example something that adds some values together:

function add(...)
    a = 0
        for i, b in ipairs{...} do
            a = a + b
        end

    return a
end

print(add(5,9,14,63))

This Lua tutorial was written by


Back to Lua

Advertisement: