Returning multiple values from a function

In C programming language, whenever we want to return multiple values from a function the first thing that comes to our mind is “Call by addresses” or “Call by reference”. During a function call when we send in a variable as an argument and manipulate that variable inside the function the scope of this updated variable will remain to that function only and in order to get that updated variable in the calling function we need to return its value. But if we want to do this with multiple variables using a single function call we can do so by calling a function by reference

Returning multiple variable from a single function only needs you to understand a single concept “Passing argument to a function by reference”. There is another method of passing an argument to a function by value that is discussed above but that’s not our concern right now. Let’s first discuss “pass by reference” in detail.

Call by reference

Calling a function by the reference of a variable means sending in argument the address or pointer to a variable instead of the variable itself. As a result, inside the function when we update the value at the given pointer address it will update the value of that variable in the memory location. Hence even if we return from that function without any returning variable, the memory location will stay updated. In this way, we can update multiple variables in a single function call and the need of returning multiple variables from one function will be fulfilled.

Returning multiple values from function

When we talk about returning 2 or 3 variables from one function we don’t actually mean returning the variables using return statement i.e. “return a” or “return 0”. We mean only updating 2 or 3 variables by that function call which is actually the functionality of a return statement. As we discussed above passing a value by reference to a function can help us achieve this functionality we will now use this methodology in an example to elaborate it more.

Code

We will now jump to the example in which I will explain the use of the method “call by reference” to return multiple values from a function.

#include<stdio.h>
void Multiple_returns(int num_1, int num_2, int *product, int *sum) {
   *product = num_1 * num_2;
   *sum = num_1 + num_2;
}
int main() {
   int num_1 = 10, num_2 = 5;
   int pro, sum;
   Multiple_returns(num_1, num_2, &pro, &sum);
   printf("Product is: %d\nSum is: %d\n", pro, sum);
   return 0;
}

In the example code shown in the image above, the function named Multiple_returns returns void when talking about the return type of the function. But when calling the function at line number 9 shows that this function call is updating the values of the variable pro and sum. Which is only possible because the variables location in memory is being updated during the function call.

Code Outptut

The output of this function is shown in the figure below.

Returning multiple values from a function

In this function we have updated two variables in a single function call we can also do this with three variables using the same methodology of passing the pointers to variables and updating 3 variables in one function call is left as an assignment for the reader.

  1. C Program Compilation steps & Example with GCC Linux
  2. Introduction to Pointers
  3. How to find an array size without using sizeof()
  4. Pointer Arithmetic, Pointer Size, and Pointer Type
  5. In which order function parameters are evaluated in C/C++?
  6. Scope of global and static variables in C/C++
  7. Difference between Structure and Union in C
  8. C Program to remove duplicate elements from an array
  9. Difference Between void main(), int main() and void main(void) in C/C++
  10. Return Multiple Values from a function

Leave a Reply