Functions and if statements

Hi there, again! Good job on completing of the previous section of the tutorial, without further ado, let’s jump into the deep water!

Let’s design a function, that when we call it, it’ll say hello world for us!

function welcome()
	print( "Hello World!" )
end

Great – we created a function with the name of welcome, and when it executes – it should say hello world! Except, why did it not work?

Well it didn’t work because we just defined it, but we haven’t actually used it! We just created it, so let’s call it just like math.min!

function welcome()
	print( "Hello World!" )
end

welcome()
welcome()
welcome()

Neat – it said Hello World three times – and it was way quicker done than writing three times print( “Hello World!” ) wasn’t it? It’s good to create functions for behaviours that you repeat often.

Lets design a function that takes in 2 arguments, numbers, in that case, and multiplies them whilst printing the result!

function multiply( arg1, arg2 )
	print( "Result: " .. arg1 * arg2 )
end

multiply( 50, 20 )
multiply( 2, 30 )

Like before, we’ve defined a function called multiply, but we added 2 possible arguments to it – when you call a function, those passed arguments are stored in “arg1” and “arg2” boxes – You can name the boxes whatever you want, but keep it concise and on topic.

Returns

What if we want the function to return the result so we can store it for later use?
You’d do it this way:

function multiply( arg1, arg2 )
	return arg1 * arg2 
end

x = multiply( 5, 20 )
y = multiply( 5, x )

print( y )

In this case, the function we defined a function that returns the result, and we assign that result to variable x – then we use that variable x to calculate another multiplication of whatever result got stored in variable x – then we simply assign the result of that calculation inside of y, and print it!

By the way, we can return multiple values! Look:

function mutlireturn( x, y )
	return x * y, x / y
end

x, y = multireturn( 5, 5 )
print( x, y )

Should yield us a print with 2 numbers – first, the result of multiplication, so 25 – and a result of division, so 1. Good job. Ah yes, you can define variables like that as well, look:

x, y, z = 5, 10, "Hello"

x is 5, y is 10, z is “Hello” – You get it, you’re smart!

If Statements

What kind of a script are we making, if it’s so linear?
What if we want to do different behaviours based on some conditions? well, there comes the if statement!

if ( condition ) then

end

So far you saw few data types – Strings, Integers (numbers without decimals), functions – But you haven’t heard about booleans just yet – Simply saying, boolean is true or false, either of!

x = "Hello World" == "Goodbye World"

This will not assign either of those strings, it will actually assign false to x – Why? Well, == is a logical operator – It checks if the value at the either side is equal to the other side – There’s a trick to it though.

x = 1 == "1"

Will still return false – Because the data types don’t match! It’s possible to override that behaviour, but right now, remember that the object AND the object type have to be equal for the equals operator to work.

You can read more about different operators here, there are operators such as ~= (not equal to) or >= (higher than or equal to)

Let’s put all of that knowledge to test, and create something something a little bit more advanced.

function announceResult( arg1 )
	print( "Result " .. arg1 )
  
	if( arg1 > 25 ) then 
    	print( "wow that result was big!" ) --If the result was bigger than 25, if not, proceed to else if
    elseif( arg1 > 10 ) then 
    	print( "wow that result was medium!" ) --If the result was bigger than 10 and previous if didn't pass
    else
    	print( "meh that result wasn't that big" ) --If no if's passed do this
    end
end

function square( arg1 )
	return arg1 * arg1
end

function multiply( arg1, arg2 )
	return arg1 * arg2
end

x = square( 5 ) --Should equal 25
y = multiply( x, 5 ) -- Should equal 100
y = multiply( y, 2 ) --We cen reassign variables at any moment! Should change y value from 100 to 200 (2 * previous y value which was 25)
announceResult( y ) -- prints result

In a nutshell, we define 3 functions, then we define a variable x, which has the value of squared 5,
then we define y, which has the value of whatever is in x times 5 – and then we override y again, with the result of multiplication of itself times 2, we pass the number over to the announceResult function and we print it first, and then we have an if statement that checks if number is higher than 25, then it says that it’s big, if it isn’t over 25, then it goes lower down the if block – checks if it’s higher than 10 – announces that it’s medium – and if it doesn’t meet neither of the conditions – then it just says that it’s little.

You can have multiple conditions on the if block, like such:

x = 24

if ( x % 2 == 0 and x > 10 ) then
	print( "number is divisible by 2 and is higher than 10" )
else
	print( "number doesn't pass neither/or both conditions" )
end

Basically, checks if x is divisible by 2 (% is called a modulo operator – returns the remainder after division of two numbers) and is higher than 10, this if block should pass since 24 is higher than 10 and is divisible by 2.

On the other hand, you can do the inverse – if you want any either of the conditions to make the if statement block pass, you can use OR:

x = -24

if ( x >= 25042 or x < 0 ) then
	print( "Number is either higher than 25042, or is lower than 0" )
end

So far we’ve been doing operations directly in the statement – you can save the result to a variable first, if you’d like.

x = 5
result = x > 50

if ( result ) then
	print( "Higher than 50!" )
end

In this case, result equals to false because x, which is 5, is lower than 50.

Gotcha? Great – one last thing regarding if statements – You can pass objects or variables other than false / true into them – The rule in Lua is:

Anything that is not nil or false, is false.
Anything else, like Strings, objects, integers, even 0’s (which isn’t the case in many, many languages) result in true

So, quickly representing that:

if ( nonexistentvariable ) then -- Won't pass because nonexistentvariable is not defined anywhere (therefore it is nil)
	print( "doesn't pass" )
end

if ( 0 ) then
	print( "passed" )
end

if ( false || nil ) then 
	print( "doesn't pass" ) 
end

Note: nil is a reserved word for nothing – It think of it as “void”, or emptiness – useful to check if things are existent, like:

x = y == nil

X will amount to true, because y wasn’t defined before and therefore is nil, nil is equal to nil, so that boolean expression returns true.

Whoop, you’ve reached the end!

Tune in over the next few days to learn about variable scopes!
Good job for reaching this point!