Language: EN

programacion-referencias-a-funciones

What are and how to use Function References

A REFERENCE to function is a type of data that, instead of simply having data, contains a link to a specific function in memory.

At first, the concept may be surprising because when you start programming, you get used to having variables with data on one side, and functions that manipulate them on the other.

But when we talk about variables, we said that a variable is “a box” that could contain different things. Or as Peter Griffin would say… “a box can contain anything… even a boat!”

Now, this includes containing functions, not just data.

referencia-funciones

A variable containing a function

What are the advantages of being able to assign the function to a variable? Now we can treat a function as an object that can be assigned, passed as an argument, returned as a result, or stored in a data structure.

Don’t worry if “you find it hard to get the hang of it”. But it is a very powerful technique that allows us to separate the implementation of the function from its alias to invoke it, which facilitates code reuse and the creation of modular code.

Function reference

Let’s translate what we’ve said into some code. Don’t worry too much about the syntax, we’ll see specific examples later. Now it’s just an excuse to understand each other.

What I was saying is that we are normally used to having,

  • Variables that contain data, like my_variable that contains 10
  • Functions, like DoSomething, that perform actions

That is, something like this,

// a variable with data
const my_variable = 10

// a function
function DoSomething()
{
}

DoSomething() // invoke the function

Now let’s see how a REFERENCE to the function would look. In this case, it would look something like this,

// a function
function DoSomething()
{
}

// a variable containing a function
const my_variable = doSomething;

my_variable() // invoke a function through the variable

Instead of calling the function by its original name, we can use our REFERENCE to the function to invoke it.

It is important to differentiate between assigning and invoking the function. For example, in the following case, observe the difference between assigning the function and invoking the function.

// this assigns the function to the variable
var my_variable = doSomething;

// this invokes the function and assigns the result to the variable
var my_variable = doSomething();

The way to invoke the function will depend on the language we are using. In general, it is (). But in any case the concepts are the same. Next, we’ll see specific cases of implementation in languages.

Examples of function references in different languages

Let’s see an example of how to make a reference to a function in different programming languages. For the example, we will create a sum function that takes two parameters a and b and returns the sum.

Then we will create a sum_function variable that contains a reference to the function. Then we invoke this function through the variable, instead of using its name.

This is how this example would look in different programming languages.

C# has the concept of a delegate, which is a system of Function References.

// Definition of the sum function
int Sum(int a, int b)
{
	return a + b;
}

void Main(string[] args)
{
	// Assigning the function to a delegate
	Func<int, int, int> sum_function = Sum;

	// Invoking the function through the delegate
	Console.WriteLine(sum_function(3, 4)); // Prints: 7
}

In C++, there is the concept of a pointer, which allows for Function References.

// Definition of the sum function
int sum(int a, int b) {
    return a + b;
}

int main() {
    // Assigning the function to a function pointer
    int (*sum_function)(int, int) = sum;

    // Invoking the function through the pointer
    std::cout << sum_function(3, 4) << std::endl; // Prints: 7
}

JavaScript is a dynamically typed language. Of course, it is possible to use variables to contain other functions. In fact, this is very common in this language.

// Definition of the sum function
function sum(a, b) {
    return a + b;
}

// Assigning the function to a variable
const sum_function = sum;

// Invoking the function through the variable
console.log(sum_function(3, 4)); // Prints: 7

Finally, Python is also compatible with Function References. In this case, it is not even necessary to declare the variable, just assign the function.

# Definition of the sum function
def sum(a, b):
    return a + b

# Assigning the function to a variable
sum_function = sum

# Invoking the function through the variable
print(sum_function(3, 4)) # Prints: 7

As we can see, apart from the differences in syntax, the concepts and even the usage are basically identical.

Internal operation Advanced

The operation of function references is implemented in different ways depending on the programming language. However, in broad terms, we can give a common explanation of how they work.

When a function is defined in a programming language, its code is stored at a specific location in memory. This location is identified by a unique memory address.

A function reference is simply a data type that contains this memory address. When we pass a function as a reference to another function or store it in a variable, what we are really doing is storing this memory address.

referencia-funcion-interno

When we use a function reference to invoke the referenced function, the program accesses the memory address stored in the reference and executes the code stored at that location.

In addition, this allows the program to dynamically invoke the referenced function at runtime, providing flexibility and dynamism to the code.

In reality, if you think about it, it is nothing more than a GO-TO. But supercharged, and much more controlled and secure.