Language: EN

programacion-retorno-funciones

What is the return value of a function

The return values are a result that, optionally, can be returned from a function. Many times it is very useful to return this value to be used elsewhere in the program.

When we talk about What functions are, we had said that a function is a block of code that executes an action. Functions have, optionally, some inputs and an output.

programacion-funcion

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

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

In general, a single return value can be returned in a function. This is because of the way functions work internally. Although, as we will see at the end, it is possible to “bypass” this restriction.

Examples of return in different languages

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

For that we will use the sum function that we defined in the previous entry and that was “a bit crooked”.

It had actually been “a bit crooked” because in the case of the sum function, although it is a very simple example, it makes a lot of sense for it to return a value,

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

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

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

// Use of the Sum function
int result = Sum(3, 5);

In the case of JavaScript, which is a dynamically typed language, it is not necessary to indicate 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 Sum(a, b) {
  return a + b;
}

// Use of the function
var result = Multiply(3, 5);

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

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

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

let result = sum(3, 5);

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

// Function definition
def sum(a, b):
  return a + b

# Use of the function
result = sum(3, 5)

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

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

Again, we see that apart from the differences in syntax between different languages and the differences because some are typed and others are not, in general, the operation and concept are the same in all languages

Return of multiple values

As we said, functions generally can only return a single value. This is because of the way functions work internally when executing in the processor.

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

(int, int) remainderDivision(int a, int b)
{
    int divisionResult = a / b;
    int remainder = a % b;
    return (divisionResult, remainder);
}

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

Some languages do this grouping transparently for us so that it feels like more than one value can be returned, but internally it is returning a group that contains the values.

Best practices and cleanliness tips 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 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 a text). This can be “forced” in almost all languages, even in typed ones we can return an object. Don’t do it.

Similarly, maintain a consistent return type in similar or related functions. For example, if you have a collection of elements and functionsgetById or getByName, it is convenient for all of them to 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 need to return collections of elements.

Avoid returning empty null values: The return value must 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 the use of 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 an element is found 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 if the result has been found, and the position.