With functions, we can store pieces of code. This allows us to execute this code whenever we want.
There are 2 ways to create a function:
example = function () print("Hello World!") end
and the more common way:
function example() print("Hello World!") end
You start by writing the keyword
function, followed by the name of the function. A function is a type of variable, so the same rules apply as when you name a variable. This function's name is
example. After the name we put parantheses
(). Now we can start typing the code we want to put inside our function. In this case, we put in
print("Hello World!") When you're done you close the function with an
Note that when you run the code, you'll see no "Hello World!" in your console, this is because we still have to execute the function. We execute a function like this:
example() --Output: "Hello World!"
You simply type the function's name, followed by parantheses. This is what we call a function-call.
Take a look at this code:
function example(num) print("I like the number " .. num) end example(15) example(2) example(44) example(100) print(num) --Output: --"I like the number 15" --"I like the number 2" --"I like the number 44" --"I like the number 100" --nil
Inside the parantheses of the function we can put what we call parameters. Parameters are temporary variables that only exist inside the function. In this case we place the parameter
num. And now we can use
num like any other variable.
We execute our function multiple times, each time with a different number. And thus each time we print the same sentence, but with a different number. The number we put inside the parantheses is what we call an argument. So in the first function-call, we pass the argument 15 to the parameter
At the end of our code we print
num, outside of our function. This gives us
nil. This means that num has no value. It's not a number, or string, or function. It's nothing. Because like I said before, parameters are variables that are only available inside the function.
Functions can return a value, which we can store inside a variable, for example. You can return a value by using the
function giveMeFive() return 5 end a = giveMeFive() print(a) --Output: 5
a becomes the value that giveMeFive returns.
--Multiple parameters and arguments are separated by a comma function sum(a, b) return a + b end print(sum(200, 95)) --Output: --34 --295
sum returns the sum of
b. We don't necessarily need to put the value our function returns in a variable first. We can directly print the value.
By using functions we don't have to repeat certain code over and over again. We can simply place a function-call instead. This is great, because if you were to change the function in the future, you don't have to change it on all those places you put your function. Always try to avoid repeating code.
Functions can store code that we can execute at any time. We call a function by writing its name followed by parantheses. We can put values inside these parantheses. These values are passed to the function's parameters, which are temporary variables that only exist within the function. Functions can also return a value. Functions remove repetition and that is a good thing.