A function is a block of code that we name that performs a specific task. They are a fundamental tool for structuring and reusing code in modern programming.
Functions allow code reuse, modularization, and structuring in a more manageable way. A good definition of functions is key to writing clean and maintainable code.
To explain what a function is, it is often said as an example that it is a “machine” where “things go in” and “things come out.”
For example, suppose you want to calculate the square root of a number. Imagine that every time you had to calculate a square root, you had to write the code (which I can tell you, is not short)! It would be crazy!
In this case, someone has already taken the trouble to program that functionality for you. This way you can call it comfortably from your program.
var raiz = raiz_cuadrada(25)
In this case, the function raiz_cuadrada
is “a machine” in which:
- A 25 goes in
- A 5 comes out
What is “inside the machine” and what it does, for now, does not matter much. What matters is that it works and that we can use it to calculate square roots.
Advantages of using functions
One of the main reasons to use functions is that they allow for code reuse. Instead of having to copy and paste the same code in different parts of a program, you can create a function and call it in different places as needed (this not only makes the code easier to write but also to maintain and update).
Another important advantage of functions is that they allow you to structure code into smaller and more manageable modules (this makes the code easier to understand and debug, and allows multiple programmers to work on the same project more efficiently).
Additionally, functions can be,
- Created by ourselves
- Created by other people
Don’t be too scared by the “created by ourselves” part. It’s something so common that you’ll do it constantly, without much effort.
Additionally, functions can be grouped into libraries or packages. For example, you could have a library of mathematical functions, with many utilities for performing calculations.
How to invoke a function
The process of using a function is called “calling” or “invoking” the function (which sounds like sacrificing a goat 🐐 in a pentateuch to call the evil one, but what can I do… that’s what it’s called).
In most languages, the invocation operator is the parentheses ( )
. So, to call a function, we use the function name followed by ( )
. Thus,
suma(3, 5)
Although this is not the case in all languages. For example, in Haskell or F#, you simply use the function name
suma 3 5
In any case, the concept is the same. Invoking a function that has been defined, for it to execute its task.
Examples of functions in different languages
Let’s see how functions are defined in different programming languages.
In statically typed languages, it is common to use the return type of the function to begin the definition. Examples of this are C++, C#, and Java:
void miFuncion() {
// body of the function
}
miFuncion(); // call the function
Other languages prefer to use a specific reserved word to start the function definition.
For example, in JavaScript and TypeScript, the reserved word function
is used:
function miFuncion() {
// body of the function
}
miFuncion(); // call the function
While in Python, the reserved word def
is used:
def miFuncion():
# body of the function
miFuncion()
Apart from these differences, in general, we see that functions are defined in a very similar way and invoked in a very similar way.
Best practices Tips
Use descriptive names: Choose function names that reflect their purpose and what they do. Descriptive names make the code easier to understand and more readable.
A function should be responsible for a single thing: It is advisable for a function to have a single responsibility or specific task. This makes the code easier to understand and maintain. If a function is performing multiple tasks, consider splitting it into smaller, cohesive functions.
Keep your functions short and concise: Functions should have a clear responsibility and perform a specific task. If a function becomes too long, consider splitting it into smaller, reusable functions.
Avoid side effects: Functions should be “pure” whenever possible, meaning they should not have side effects and should only depend on their arguments. This makes the code more predictable and easier to reason about.