Functions and their uses

Understanding functions in a easy way!

by BlueBonsai_1991

Author Avatar

What are functions and how are they used?


We can think at a function like a group of statements, executed every time the function is called. With a function you can do many things, such as:

     -resetting a player's character when he touches a part.
     -making a part fall when the player touches a button.

And much more!

Basically, everything is based on functions, from character's movement to complicate game dynamics.

How to use a function?

In Lua, for declaring a function we use this code format:

function myFunctionName()
    --some code here
end

As you see it's very simple declaring a function! Let's see together some examples:

function SayHello()
    print("hello")
end

But there is a small problem: if we click 'Play' in the Studio, nothing would happen! Why? Simple: a function is a group of statements that are executed ONLY when you call the function. What does that mean? That means you have to tell to the script you want the group of statements to work, so we have to call the function.

e.g.

function SayHello() --declaring our function
    print("Hello world!")
end

SayHello() --calling our function

Now, if you click Play in your Studio, everything will work fine! As you see, we just repeated the function's name for calling it!

What is a function's argument?

We must introduce a new concept: a function's argument. What is it and how does it work? Let's make it clear with a simple example:

We wanted to make a calculator so it would return the sum of 2 numbers chosen by us.

Let's declare some variables first of all:

local firstNum = 3

local secondNum = 4

Now let's make a function for making their sum:

function SumOfNumbers(argumentOne, argumentTwo)
    print(argumentOne + argumentTwo)
end

SumOfNumbers(firstNum, secondNum)

Output:

7


What happened? What is all this messy code? Well, we did a very simple thing: we declared 2 variables with custom values. After this we declared a function with something enclosed between the ( ). What are these? They are called arguments! Let's continue analysing our code: After the declaration of the function, we have called it but this time inserting something else, different from the arguments, inside the ( ). We inserted our variables! But, where did the arguments go? We simply told the code that the variables will take the argument's place so it became like the function's 'food'. We feed the function with arguments that are replaced by values assigned by us! We can write the code in a different way, for understanding better how it works:

function MyFunction()
    local myNum = 3
    local mySecondNum = 4
    print(myNum + mySecondNum)
end

**


Output:

7


They return the same output! So why should I use arguments instead of simply doing the sum of numbers like that?

The advantage of using arguments is that you can make complex functions in a very easy way! Let's say you wanted to make a calculator where a player inserts some numbers and gets their sum! Without arguments that would be really hard! With arguments you simply give the values of the inputs to some variables, declare a function with some arguments, make the sum of the arguments, call the function by replacing the arguments with the variables!

That's it! You can do everything you want to do with arguments!


Thank you for your time, I hope this guide was easy to understand!

View in-game to comment, award, and more!