A function is a block of code to which we give a name that performs a specific task. They are a fundamental tool for structuring and reusing code in modern programming.
Functions allow you to reuse code, modularize it, and structure it 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 if every time you had to do a square root you had to write the code (which, I’m telling you, is not short). It would be crazy!
In this case, someone has already taken the trouble to program that functionality for you. So 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, doesn’t matter much to us. What matters is that it works and that we can use it to calculate square roots.
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 pentacle to summon the devil, but what can I do… that’s what it’s called).
In most languages, the invocation operator is parentheses ( ). So, to call a function we use the function name, followed by ( ). Like this,
suma(3, 5)
Although not in all languages it’s like that. For example, in Haskell or F# they simply use the function name.
suma 3 5
In any case, the concept is the same. Invoke a function that has been defined, so that it executes its task.
Examples of Functions in Different Languages
Let’s see how functions are defined in different programming languages.
In strongly typed languages, the return type of the function is usually used to start its 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 begin 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 very similarly and are invoked very similarly.
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 one thing: It is advisable for a function to have a single responsibility or specific task. This facilitates code understanding and maintenance. If a function is performing multiple tasks, consider splitting it into smaller, more 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.
