Language: EN

programacion-funciones-como-parametros

Passing functions as parameters

We have already seen that it is possible to save a reference to a function in a variable. So, it should come as no surprise that we can also pass a function as a parameter to another function.

Passing references to functions simply means that a function can accept references to other functions as arguments. Of course, it is also possible to return a function as return.

This allows the receiving function to use the passed function as a parameter, either by invoking it directly, storing it for later use, passing it to another function, or even doing nothing with it. Ultimately, whatever you want, just like any other variable.

Why would someone want to pass an entire function to another? Well, because it allows for some very interesting things. Mainly, it allows you to write more general and reusable functions, as they can work with different behaviors specified by the referenced function.

In short, they are great for creating powerful, reusable tools. It may take a little effort to get used to them, but it is worth it. So let’s see their use with a couple of examples.

Use Cases of Function References

For example, imagine that you have a function that traverses the elements of a collection. In addition, the way to traverse it is complex, so you would like to reuse that code as much as possible.

Now you want to use your function to traverse the collection and, if they meet a certain criteria, execute an action. If you pass the criterion function and the action function, you can reuse the logic of your traverse_collection function.

function traverse_collection(criterion, action)
{
	// do things
}

Another example, imagine that a function has to perform a very long and complicated process. In addition, this process can either succeed 👍, or give an error ❌.

You can create a function that performs the process, and pass it the two functions. One for success to be executed if everything goes well, and one for error if something goes wrong.

function do_something_very_complicated(success, error)
{
	// do something very complicated
	// with possible error calls()

	// and if everything goes well, success
	success();
}

Examples of functions as parameters in different programming languages

Let’s see how the use of Higher Order functions and passing functions as parameters is done in different programming languages.

In this example, we are going to create two functions Add and Subtract, which take two parameters a and b and perform the appropriate operation.

On the other hand, we will have a higher-order function called ExecuteOperation. This function takes two parameters a and b, and the operation to execute.

Finally, we invoke ExecuteOperation with different parameters of a and b, and the operation to perform (Add or Subtract).

Higher Order Functions

Functions that accept other functions as arguments or return functions as results are known as higher-order functions.

That is, a Higher Order Function is,

  • It can accept one or more functions as arguments
  • It can return a function as a result

It is a term that has become very popular, because it has an (especially theoretical) important role in functional programming. And functional programming “is in vogue”.

Note the irony in my comments

But don’t give it too much importance. In the end, they are nothing more than normal and ordinary functions, just like any other. It’s just that some of the data types it handles are function references.

Relationship with Lambda Functions

Lambda functions, also known as anonymous functions, are a way of defining inline functions without the need to assign them a name.

Lambda functions are especially useful when you need a function that you are only going to use once, especially if it is short. Here it is not worth creating a “normal” function, and lambda functions make your life easier.

So on one hand we have a syntax for short functions, and on the other functions that accept other functions. Do you see where I’m going with this? These are concepts that fit together very well.

Indeed, the combination of lambda functions and passing function references as parameters is a common and very common practice in many modern programming languages.

Internal operation Advanced

The internal operation of passing and returning functions from a function is very simple, if you understand the concept of a reference to a function.

Remember that a reference to a function is a variable that contains the address of a function, rather than a data, or the address of another variable.

Apart from that, for the receiving function the management is exactly the same as with any other type of variable. It receives it, returns it, copies it, stores it… the function basically doesn’t care.

But the difference will be in the use made of the variable. And, if the language has typing, in the check it makes that the types are correct.

But in reality, there is no difference in the operation of a Reference to a function, than with any other type of variable.