|
 |
|
Functions, sometimes known as methods, are one of the most powerful constructs of programming languages. It is a way to organize some lines code, so that you can easily refer to it.
For example, if you were writing a script where you needed a count down timer in several different parts of it. You could write (copy) the code several times every where you needed it. A better approach would be write a function so that you could simply call the program each time you needed it. That way you only needed to write the code for the timer once. Another advantage is that if you ever needed to change to code for the timer, you could change it in one place and the changes would be applied everywhere.
Writing a function in Lua is fairly easy. It is defined with the function keyword, followed by the function name. The body of the function is one or more Lua statements and closed with the end keyword. It follows this basic format
function functionName()
// Lua Code
// Lua Code
// Lua Code
end |
Here's an example of a simple function:
function sandwichList()
sandwich = {"bread","cheese","turkey","mayo","tomato"}
print ("Here's what you need to make a great sandwich!!")
for i,v in ipairs(sandwich) do
print("Ingredient " .. i .. ": " .. v .. "!")
end
end |
We use the function we have just made like we would any other function. So we can simply do this:
And you get this as the output:
Here's what you need to make a great sandwich!!
Ingredient 1: bread!
Ingredient 2: cheese!
Ingredient 3: turkey!
Ingredient 4: mayo!
Ingredient 5: tomato! |
Now as far as functions go, our sandwich list example is a really simple function. We can also have functions return a value back to us. Consider the following function:
function getName()
local name = zs.func.prompt("What is your name?")
return name
end
print("Your name is " .. getName()) |
We've got quite a bit going on here. We have our function call, and it is returning the variable 'name' to us. This is done using the return keyword. We then call our function to use. Note that we can call a function within function, which we have done by invoking the getName() function from one of the print() function.
In many cases where functions are desired, we may not want to get our input directly from the user. Instead we want to pass on or more arguments to our function. One simple example would caculate the square of a number.
function square(number)
local result = number * number
return result
end |
We can now use the function as follows:
input = zs.func.prompt("Please enter a number:")
print("The square of " .. input .. " is " .. square(input)) |
If we ran this script and entered 3.5 at the prompt, we would get the following output:
The square of 3.5 is 12.25 |
Functions can accept multiple input parameters. For example:
function addNumbers(number1, number2)
return (number1 + number2)
end |
This function takes two input parameters number1 and number2 and returns their sum.
You have now the tools necessary to perform impressive programming feats with Lua and CMUD. The next section will cover practical examples of how to successfully utilize CMUD and the integrated Lua programming environment. Before you do, you may want to look at some of the pointers Tim Toady has to share.
Tim Toady
You'll note that in some of examples we've used the local keyword when declaring our variables. This is used to establish the scope of the to enclosing statement block. What this essentially does is limit the scope of the "local variable" to the enclosing function, will only be available within that function. In most cases, Lua variable declarations are global by default.
Function arguments in Lua are nil by default. What this means is if your function accepts more arguments than it receives, then arguments that do not receive values hae a value of nil within the function. Consider the following:
function proveNil(arg1, arg2, arg3, arg4)
if (arg1 == nil) then
print("Arg1 is nil")
else
print ("Arg1 is: " .. arg1)
end
if(arg2 == nil) then
print("Arg2 is nil")
else
print("Arg2 is: " .. arg2 )
end
if (arg3 == nil) then
print("Arg3 is nil")
else
print ("Arg3 is: " .. arg3)
end
if(arg4 == nil) then
print("Arg4 is nil")
else
print ("Arg4 is: " .. arg4)
end
end
proveNil(5, "Cow", 6.3, "Hello Everybody")
print("-----------------------------")
proveNil(5, "Cow")
print("-----------------------------")
proveNil()
print("-----------------------------") |
The output would be
Arg1 is: 5
Arg2 is: Cow
Arg3 is: 6.3
Arg4 is: Hello Everybody
-----------------------------
Arg1 is: 5
Arg2 is: Cow
Arg3 is nil
Arg4 is nil
-----------------------------
Arg1 is nil
Arg2 is nil
Arg3 is nil
Arg4 is nil |
Lua functions can accept a table as input to a function. Consider:
function tableTest(var1, table1, var2)
print("Variable 1 is " .. var1)
print("Table 1 contains:")
for i,v in ipairs(table1) do
print(i .. ": " .. v )
end
print("Variable 2 is " .. var2)
end
tableTest(5, {"sandwich", "burger", 7.5, 6, "Mr. Smith"}, "cat") |
The output would be:
Variable 1 is 5
Table 1 contains:
1: sandwich
2: burger
3: 7.5
4: 6
5: Mr. Smith
Variable 2 is cat |
One powerful feature in Lua is that allows you to return multiple results at once. Suppose you wanted to return two values back from a function. While it may be challenging in some other programming languages, in Lua it is quite simple and straight-forward.
function getNumbers()
local number1 = zs.func.prompt("Enter the first number:")
local number2 = zs.func.prompt("Enter the second number:")
return number1, number2
end |
You can access the values quite simply.
x, y = getNumbers()
print("The first number is " .. x .. " and the second number is " .. y) |
It is also possible to only one of the numbers. Consider the following:
|
|