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.
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.
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.
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,
,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,
,where "FunctionName" is the name we want to assign our function with.
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.
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.
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,
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.
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,
"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