If Statements and Functions

From PZwiki
Jump to: navigation, search
Language policy Language: [[::If Statements and Functions|English]]


If Statements

If statements allow the user to control the conditions for when code is run. It tests its conditions and then completes then or else statements accordingly, with else being optional. In full, the format is as follows:

a = 1
if a > 5 then
  print("More than 5!")
  elseif a < 5 then
  print("Less than 5!")
  else
  print("Number is 5!")
end

As the value of a changes, potentially so will the output. Both else and elseif are optional:

a = 1
if a > 5 then
  print("More than 5!")
end

elseif is a useful alternative to putting separate if statements under else - meaning only one end is needed for the whole if statement.
Like most things in Lua, if statements can be nested:

a = 3
if a > 5 then
   if a/2 == math.floor(a/2) then
     print("a is more than 5 and is even")
     else
     print("a is more than 5 and is odd")
   end
  elseif a < 5 then
   if a/2 == math.floor(a/2) then
     print("a is less than 5 and is even")
     else
     print("a is less than 5 and is odd")
   end
  else
  print("a is 5!")
end

math.floor returns the nearest lower integer (whole number). The math library will be explored in later tutorials. Although, it should be mentioned this can be written using one if statement:

a = 9
if a > 5 and a/2 == math.floor(a/2) then
 print("a is more than 5 and is even")
 elseif a > 5 and a/2 ~= math.floor(a/2) then
 print("a is more than 5 and is odd")
 elseif a < 5 and a/2 == math.floor(a/2) then
 print("a is less than 5 and is even")
 elseif a < 5 and a/2 ~= math.floor(a/2) then
 print("a is less than 5 and is odd")
 else
 print("a is 5!")
end

Functions

Functions, like print, allow the same piece of code to be used repeatdedly without having to re-write it every time. They can be creating using this format:

function increment(a)
--statements go here
end

a here represents any arguments that could be used by the function - like strings entered into print.
Here is an example of a function:

function incrementPrint(a)
print(a)
a = a+1
print(a)
end

incrementPrint(5)

Note that while a has been used to indicate where an argument goes, a variable by the same name won't be used unless entered as an argument. For example:

function increment(a)
print(a)
a = a+1
print(a)
end

a = 5
increment(a)
print(a)

The a referred to inside the function is different to the variable a - this is the difference between local and global variables and will be explained in a later tutorial. What this does mean is that you don't have to worry about arguments conflicting with variable names - although equally variables of the same name can't be accessed outside the function.
If you try the following code, you may notice that there is no output:

function increment(a)
a = a+1
end

print(increment(1))

This is because no value is returned from the function. This can be rectified using return - however, return is the last statement to be read in the function:

function increment(a)
a = a+1
return a
end

print(increment(1))

When calling a function with a single argument which is either a literal string or table constructor, then you do not need the parenthesis:

print"Hello World!"

Summary

  • if statements check their condition to see if it is true and runs the appropriate statement
  • They can include elseif and or else
  • You only need one end per if statement
  • Functions allow the same piece of code to be run again
  • Arguments are local variables inside a variable
  • return can be used to return anything inside the function
  • Functions with a single argument that is a string or table constructor do not need parentheses