Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

What will happen if a Function is tried to return more than one value at a time?

  • Last Updated : 23 Nov, 2021

Prerequisite: How to return multiple values from a function in C or C++?

There are often cases of using return statements while dealing with function calls. Generally, only one thing is returned whether it’s a primitive data type like integer, character, etc, or a non-primitive data type like an array, string, vector, etc depending upon the function’s return type.
But what will happen if we try to return more than one value via return statements? 

We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today!

This article focusses on discussing the scenario when more than one values are returned via return statements.

Predict the Output:

C






// C program to implement
// the above approach
#include <stdio.h>
  
// Function declaration and
// definition
int AddSub(int c, int d)
{
    int x, y;
    x = c - d;
    y = c + d;
  
    // Returning two integers
    // instead of one
    return (x, y);
}
  
// Driver code
int main()
{
    // Initializing the variables
    int i = 100, j = 200, k;
  
    // Calling AddSub function
    k = AddSub(j, i);
  
    // Printing k
    printf("The value of k = %d", k);
}

C++




// C++ program to implement
// the above approach
#include <iostream>
using namespace std;
  
// Function declaration and
// definition
int AddSub(int c, int d)
{
    int x, y;
    x = c - d;
    y = c + d;
  
    // Returning two integers
    // instead of one
    return (x, y);
}
  
// Driver code
int main()
{
    // Initializing the variables
    int i = 100, j = 200, k;
  
    // Calling AddSub function
    k = AddSub(j, i);
  
    // Printing k
    cout << "The value of k = " << k;
}

Output:

The value of k = 300

Explanation:
Most of you must be wondering the output of the above code will an “Error” because in the above code, more than allowed values are being returned to the function call. 
In the cases like this when multiple values are returned without taking special precaution like array, pointers, structures, references, tuple, classes and objects, then in that case the last value is returned and all the values before that are simply ignored. 
The number of returning values can be many but only the last occurring will be returned and also one can also ignore the brackets or braces “( )” and can write without braces just by separating the multiple values by a comma as shown in the below code:

C




// C program to implement
// the above approach
#include <stdio.h>
  
// Function declaration and
// definition
int Operations(int c, int d)
{
    int p, q, r, s, t;
    p = c - d;
    q = c + d;
    r = c * d;
    s = c / d;
    t = c % d;
  
    // Returning multiple integers
    // instead of one
    return (p, q, r, s, t);
}
  
// Driver code
int main()
{
    // Initializing the variables
    int i = 100, j = 200, k;
  
    // Calling Operations function
    k = Operations(j, i);
  
    // Printing k
    printf("The value of k = %d", k);
}

C++




// C++ program to implement
// the above approach
#include <iostream>
using namespace std;
  
// Function declaration and
// definition
int Operations(int c, int d)
{
    int p, q, r, s, t;
    p = c - d;
    q = c + d;
    r = c * d;
    s = c / d;
    t = c % d;
  
    // Returning multiple integers
    // instead of one
    return p, q, r, s, t;
}
  
// Driver code
int main()
{
    // Initializing the variables
    int i = 100, j = 200, k;
  
    // Calling Operations function
    k = Operations(j, i);
  
    // Printing k
    printf("The value of k = %d", k);
}

Output:

The value of k = 0

Explanation:
Here the output is 0 because the last variable t which is equal to c % d = 0. So when multiple values are returned then the value of t gets assigned to the variable k.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!