Chapter 1: Variables, values and types Chapter 2: Properties and Instances Chapter 3: Conditionals and If Statements Chapter 4: Functions and Blocks 4.1: Blocks 4.2: Functions 4.3: Calling Functions 4.4: Parameters 4.5: Return Chapter 5: Event Listeners
4.1: Blocks

When we refer to a block in LUAU, what are we referring to? As defined by lua.org, A block is the body of a control structure, the body of a function, or a chunk (the file or string with the code where the variable is declared).

But what does this mean?

Remember in the pervious chapter, where we discussed if statements and said that if statements end with the keyword end?

That if statement was actually a block itself. A block's end is declared by using the keyword end.

Each block has a scope, which essentially is just the area where a variable is recognized. local variables can only be accessed if they are created before or inside a block.

Let's give an example. In the if statement example, lets create a variable above the if statement.

local num = 10

if CONDITION then
print(num) --This will print the variable num, since the variable num is being accessed within its scope.
end

print(num) --This will print the variable num, since the variable num is being accessed within its scope.

Both of the print statements above will not return an error since the variable is being accessed within its scope.

Let's take a look at a different example.

if CONDITION then
local num = 10
print(num) --This will print the variable num, since the variable num is being accessed within its scope.
end

print(num) --This will give an error since the variable num is being accessed outside of its scope.

In the example above, only the first print statement will not error, while the second print statement will give an error since the local variable num is accessed outside of its scope.

When using local variables, always make sure to keep in mind scope.

4.2: Functions

A function is a set of procedures that carries out the code we provide it.

Functions are useful since they simplify our code if we need to use the same set of instructions multiple times. Instead of writing the same code multiple times, we can just call our function.

Let's say we needed a function to display multiple print messages. We can either declare our function as a local function or a global function. To declare a local function we simply write,

local function FunctionName ()
--Instructions Here
end

,where "FunctionName" is the name we want to assign our function with.

Notice that our functions end with the keyword "end". This means that functions are also a type of block and scope should be considered.

On the other hand, if we wanted to declare a global function, we simply write,

function FunctionName ()
--Instructions Here
end

,where "FunctionName" is the name we want to assign our function with.

4.3: Calling Functions

Now that you know how to declare a function, how do we use it? To use a function (Also know as calling a function) we simply refer to the functions name with parentheses. If our functions name is "SuperFunction" the we call it by simply writing,
SuperFunction()

Let's give an example of this. We are going to create a function called "PrintAllMessages" which will print a series of messages. We need to first declare our function and then call it.

local function PrintAllMessages ()
Print("Hello")
Print("My Name is")
Print("Bob...")
end

PrintAllMessages()

This function will print the 3 messages we wrote when called. Instead of writing the 3 print statements every time we want to print the messages, we can just call the PrintAllMessages() function that we created.

4.4: Parameters

Functions may seem useful already, but what if we wanted to use the same instructions but with some minor changes? In the example above, what if we wanted to print a different series of messages each time?

This is where parameters come in. A parameter is just a variable or value that we pass to a function. What does this mean? When we say we pass a value to a function, we simply send the value to that function.

Let's say we wanted to create a function that prints anything we give it 3 times,

local function PrintAllMessages (Parameter)
Print(Parameter)
Print(Parameter)
Print(Parameter)
end

PrintAllMessages("Our First Message") -- This will print "Our First Message" 3 times
PrintAllMessages("Our Second Message") -- This will print "Our Second Message" 3 times

We can pass as many parameters as we want and each parameter can either be a variable or a value. Additionally, each Parameter we put inside the function declaration can be any name we decide.

4.5: Return

Another useful thing we can do with functions is return a value using the keyword return

You may want a function that calculates a value for you which will then be stored in a variable for later use. To do this, we just need to use "return". For example, if we wanted to create a function that calculates the square of a number and returns that value, we can write,

local function CalculateSquare (numValue)
return numValue*numValue -- This will return the square of numValue
end

local squareOfTwo = CalculateSquare(2) -- This will store the value 4 which is the square of 2

"return" is very useful and simplifies code drastically.

Things to Note:
* Return will finish a function, meaning that code written after a return is done will not run.
* Return can return any variable or value
* Return can return nothing which is useful to stop a function

Chapter 5: Event Listeners