Return Multiple Values from a Function in C/C++



In C or C++, we cannot return multiple values from a function directly. In this Article, we will see how to use some trick to return more than one value from a function.

Returning Multiple Values from a Function

We can return multiple values from a function by using the method. Below is the list of methods that are used to return multiple values from a function in C/C++:

Returning Multiple Values Using Pointers

Pass the arguments by their addresses and modify their values using pointers so that value gets changed into the original argument.

Example

Let's see an example where the task is to find the greater and smaller of two distinct numbers. We could write multiple functions. The major issue is the complexity of calling multiple functions because we need to return multiple values and, of course, have to type more lines of code.

#include <iostream>
using namespace std;
void compare(int x, int y, int* address_greater, int* address_smaller)
{
   if (x > y) {
      *address_greater = x;
      *address_smaller = y;
   }
   else {
      *address_greater = y;
      *address_smaller = x;
   }
}

int main()
{
  int greater, smaller, x, y;
  x = 10;
  y = 20;
  compare(x, y, &greater, &smaller);
  cout << "The greater number is " << greater << " and the smaller number is " << smaller;
  
  return 0;
}

Following is the output of the code:

The greater number is 20 and the smaller number is 10

Returning Multiple Values Using Structures

The structure is a user-defined data type. So we can construct a structure with two integer variables, store the greater and smaller values in that variable, and then use the structure's content.

Example

The following example demonstrates how you can return multiple values using structures:

#include <stdio.h>
struct greaterSmaller {
   int greater, smaller;
};
typedef struct greaterSmaller Struct;
Struct findGreaterSmaller(int x, int y)
{
   Struct s;
   if (x > y) {
      s.greater = x;
      s.smaller = y;
   }
   else {
      s.greater = y;
      s.smaller = x;
   }
   return s;
}
int main()
{
   int x, y;
   Struct result;
   
   x = 10;
   y = 20;
   
   result = findGreaterSmaller(x, y);
   printf("The greater number is %d and the" " smaller number is %d", result.greater, result.smaller);
   
   return 0;
}

Following is the output of the above code:

The greater number is 20 and the smaller number is 10

Returning Multiple Values Using Arrays

The array will only work when the items are the same type. When an array is passed as an argument, then its base address is passed to the function, so whatever changes are made to the copy of the array, it is changed in the original array.

Example

The following program is to return multiple values using an array, i.e., store the greater value at arr[0] and the smaller at arr[1]:

#include <iostream>
using namespace std;
// Store the greater element at 0th index
void find_greater_smaller(int x, int y, int arr[])
{

   // Store the greater element at
   // 0th index of the array
   if (x > y) {
      arr[0] = x;
      arr[1] = y;
   }
   else {
      arr[0] = y;
      arr[1] = x;
   }
}

int main()
{
   int x, y;
   int arr[2];
   
   x = 10;
   y = 20;
   
   find_greater_smaller(x, y, arr);
   
   cout << "The greater number is " << arr[0]  << " and the " "smaller number is " << arr[1];
   
   return 0;
}

Following is the output of the above code:

The greater number is 20 and the smaller number is 10
Updated on: 2025-06-18T18:38:24+05:30

28K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started