Language: EN

programacion-que-es-una-funcion

What is a function

A function is a block of code that we give a name that performs a specific task. They are a fundamental tool for structuring and reusing code in modern programming.

Functions allow to reuse the 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.”

programacion-que-es-una-funcion

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 put the code (which, by the way, 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 raiz_cuadrada function is “a machine” where:

  • A 25 enters
  • A 5 comes out

What is “inside the machine” and what it does, for now, we don’t care much. What matters to us 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 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 the code to be structured 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.

In addition, functions can be,

  • Created by ourselves
  • Created by other people

Don’t be too scared about “created by ourselves.” It is so common that you will do it constantly, without any effort.

Also, 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… it’s called that way.

In most languages, the invocation operator is parentheses ( ). So to call a function we use the name of the function, followed by ( ). Thus,

suma(3, 5)

Although not in all languages it is like this. For example, in Haskell or F# the function name is simply used

suma 3 5

In any case, the concept is the same. Invoke a function that has been defined, so that it performs its task.

Thanks to functions we can make reusable code fragments, identified by a name. We can use these functions ourselves, or other people can.

Examples of functions in different languages

Let’s see how functions are defined in different programming languages.

In strict typing languages, the type returned by the function is usually used to start the definition of the function. An example of this is C++, C#, and Java:

void miFuncion() {
  // function body
}

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() {
  // function body
}

miFuncion(); // call the function

While in Python the reserved word def is used:

def miFuncion():
  # function body

miFuncion()

Apart from these differences, in general, we see that functions are defined in a very similar way and called in a very similar way.

Best practices and cleaning tips 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 recommended that a function has a single responsibility or specific task. This makes it easier to understand and maintain the code. If a function is performing multiple tasks, consider dividing it into smaller and 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 dividing it into smaller and reusable functions.

Avoid side effects: Functions should be “pure” whenever possible, that is, they should not have side effects and should only depend on their arguments. This makes the code more predictable and easier to reason about.