Language: EN

programacion-retorno-funciones

What is the return value of a function

Return values are a result that can optionally be returned from a function. Often, it is very useful to return this value for use in another part of the program.

When we talked about what functions are, we said that a function is a block of code that performs an action. And that functions have, optionally, some inputs and an output.

programacion-funcion

Multiple inputs, a single output

The return value of the function is the output that, optionally, the function can give us so that the value returned can be used elsewhere in the code.

In general, only a single return value can be returned from a function. This is due to the way functions work internally (although, as we will see at the end, it is possible to “bypass” this restriction).

Return values are important because they allow us to expand the modularity and reusability of our functions.

Examples of return in different languages

Next, we will look at some examples of how to return a function in different programming languages.

For this, we will use the sum function we defined in the previous entry, which was left “somewhat incomplete”.

It was precisely left “somewhat incomplete” because in the case of the sum function (although it is a very simple example) it would benefit greatly from being able to return a value.

In the case of C, C++, C#, and Java, the return value is indicated in the function definition by specifying the type of returned value before the function name.

Then, to return the value from any point in the function, the reserved word return is used followed by the value we want to return.

// Function definition
int Suma(int a, int b) {
  return a + b;
}

// Using the Suma function
int resultado = Suma(3, 5);

In the case of JavaScript, which is a dynamically typed language, it is not necessary to specify the type of value to return. It is only necessary to call the return function from any point in the function with the value we want to return.

// Function definition
function Suma(a, b) {
  return a + b;
}

// Using the function
var resultado = Suma(3, 5);

In the case of TypeScript, which, as we remember, is a version of JavaScript that introduces strict typing, it is necessary to indicate the return type of the function; in this case, it is placed after the function name separated by :.

Similarly, as in the previous cases, at some point in the function we have a return statement indicating the value we want to return.

// Function definition
function suma(a: number, b: number): number {
  return a + b;
}

let resultado = suma(3, 5);

In the case of the Python language, it is very similar to the previous ones. As a dynamically typed language, it is not necessary to indicate the type of value returned. So we simply have to call return at some point in the function with the value we want to return.

# Function definition
def suma(a, b):
  return a + b

# Using the function
resultado = suma(3, 5)

To give an example of slightly different syntax in the case of a function in SQL, the syntax would be similar to the following.

CREATE FUNCTION sumar_numeros(@a INT, @b INT)
RETURNS INT AS
BEGIN
    DECLARE @resultado INT;
    SET @resultado = @a + @b;
    RETURN @resultado;
END;

Again, we see that apart from the syntax differences between different languages and the differences between some being typed and others not, the functioning and concept are the same in all languages.

Returning multiple values

As we mentioned, functions generally can only return a single value (this is due to the way functions work internally when executed on the processor).

However, it is possible to return a collection of multiple values in a data structure and return it as a return value.

(int, int) divisionResto(int a, int b)
{
    int resultadoDivision = a / b;
    int resto = a % b;
    return (resultadoDivision, resto);
}

In this example, the divisionResto function returns two values: the result of the division and the remainder. Both values are grouped in a tuple that is returned as a return value.

Some languages perform this grouping transparently for us, giving the impression that more than one value can be returned, but internally what is being returned is a group that contains the values.

Best practices Tips

Choose an appropriate return type: Select the return data type that is most appropriate for the function and its purpose. Ensure that the return type reflects the information that the function should provide to the caller.

Maintain consistency in the return type: Although the language allows it, avoid returning two different types of return values (for example, a number and text). This can be “forced” in almost all languages; even in typed languages, we can return an object. Don’t do it.

Also, maintain a consistent return type in similar or related functions. For example, if you have a collection of elements and functions getById or getByName, it is advisable that they all return the same type. In this example, a single element.

If you need to return other types, use another “family” of functions, such as getAll or getWhere..., if you have to return collections of elements.

Avoid returning null empty values: The return value should have a clear and useful meaning. Avoid returning empty or null values when possible. If the function cannot produce a valid result, consider using exceptions or special error codes.

Consider using complex data structures: If the function needs to return multiple related values, consider using complex data structures, such as tuples, dictionaries, or custom classes.

A typical example is the index_of() function, which is a function that returns the position where it finds an element in a collection. It is a common (and dirty) practice to return -1 if the value is not found. It is much better to return a tuple indicating whether the result was found and the position.