// The basics
Here we will talk about functions, one of the fundamental tools when writing our programs. We will see what they are and why they are so important.
# What are functions and what are they for?
Imagine a large program, like Word or Excel: they are millions of lines of code, impossible to handle if it is not organized in some way.
To solve these kinds of things we have something called functions. A function can be seen as a little block of code that does something, and follows the strategy of "divide and conquer".
We have a big, difficult task. We can divide it into smaller, easier-to-solve tasks.
We solve these tasks and unite that solution.
Our program is like this task: it is big, has many functionalities, it does many things; and when programming it we divide it into smaller parts, simpler functionalities, and then we invoke these solutions. Generally, our main program ends up having a few lines that invoke functions.
We've been using functions but we haven't seen them: every program starts with a main function, for example Main.
We also used primitives read, print, which are functions that communicate with the operating system.
We can also see functions as a little box: it has a name, an entry point, and an exit point. It is a little block that does something.
When creating it, it encapsulates the task it executes and when using it, it is like a black box: I invoke the function, I don't care how it does it, I only know that it does it and it's useful for me to take the result.
The function has a name: it has to be something that represents it, that indicates what it does, to be able to understand it.
The input are the parameters: parameters are the data, the variables that we pass to a function.
Think of the function as a closed box: it can only use what we pass to it.
And from the outside we don't see what's inside either, we only see the result.
This leads us to the concept of scope: the ambit of variables, where they live, from where they can be used.
A function can create its own variables: they are local variables to the function, from the outside we don't see them.
And we also have global variables, which depending on the language are created outside or are declared as global variables, and can be used throughout the program, even within functions even if we don't pass them as parameters.
It is a bad practice to use global variables because they stay for the entire life of the program occupying memory, and the program becomes more difficult to debug, because it's hard to detect where the value of the variable is changed. So we forget about global variables and pass entry parameters to the function.
And the output, the result it returns, returns a single result.
We must state what data type it returns.
For example, we have the average function, which returns a decimal type and receives three parameters, three grades that can be of decimal type. It creates the local variable result, adds the three grades, divides them by 3 and returns that result.
Where we want to use it, we invoke it with the name and passing the parameters. We can use it as many times as we want.
# Advantages of using functions
- Avoids duplicating code: we can invoke the function as many times as we need, we don't have to write the same code twice.
- Use of libraries: we can use libraries of functions made by others, include them in our program and save time, favoring code reuse.
- Teamwork: allows dividing work into parts, modules or functions. Each one does a part and then they integrate.
# Parameter by value and reference
Something important about functions are the parameter passages by value and by reference.
When we pass a parameter, a piece of input data, to a function, we can change its value within the function, and have this new value reflected outside of it. This is known as passing by reference.
To indicate passage by reference, the parameter is passed with a special indication, which depends on the language (for example, an ampersand &). If modified inside, the change is reflected outside.
In passing by value, if the variable changes within the function, it doesn't affect the value outside; upon leaving the function it maintains the one it had upon entering.
To conclude the functions topic, best practices say that each function should perform a single task and that its name should be representative.
That's as far as we go with the functions topic. This is one of the basic topics that a programmer must master, and the only way to achieve it is by practicing; it's not enough to just read theory. In the next article we will see Arrays, a variable that allows storing more than one value.