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

• Difficulty Level : Easy
• Last Updated : 27 Dec, 2021

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?

This article focuses 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 using namespace std; // Function declaration and// definitionint AddSub(int c, int d){    int x, y;    x = c - d;    y = c + d;     // Returning two integers    // instead of one    return (x, y);} // Driver codeint 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;}

C

 // C program to implement// the above approach#include  // Function declaration and// definitionint AddSub(int c, int d){    int x, y;    x = c - d;    y = c + d;     // Returning two integers    // instead of one    return (x, y);} // Driver codeint 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);}

Java

 // Java program to implement// the above approach import java.util.*; class GFG{ // Function declaration and// definitionstatic int[] AddSub(int c, int d){    int x, y;    x = c - d;    y = c + d;     // Returning two integers    // instead of one    return new int[]{x, y};} // Driver codepublic static void main(String[] args){    // Initializing the variables    int i = 100, j = 200;    int []k;     // Calling AddSub function    k = AddSub(j, i);     // Printing k    System.out.print("The value of k = " +  (k[k.length-1]));}}// This code is contributed by 29AjayKumar

Python3

 # Python program to implement# the above approach # Function declaration and# definitiondef AddSub(c, d):    x, y = 0, 0;    x = c - d;    y = c + d;     # Returning two integers    # instead of one    return [x, y]; # Driver codeif __name__ == '__main__':       # Initializing the variables    i = 100; j = 200;    k = 0;     # Calling AddSub function    k = AddSub(j, i);     # Printing k    print("The value of k = ", (k[len(k) - 1]));     # This code is contributed by 29AjayKumar

C#

 // C# program to implement// the above approachusing System; public class GFG{ // Function declaration and// definitionstatic int[] AddSub(int c, int d){    int x, y;    x = c - d;    y = c + d;     // Returning two integers    // instead of one    return new int[]{x, y};} // Driver codepublic static void Main(String[] args){    // Initializing the variables    int i = 100, j = 200;    int []k;     // Calling AddSub function    k = AddSub(j, i);     // Printing k    Console.Write("The value of k = " +  (k[k.Length-1]));}}  // This code is contributed by shikhasingrajput

Javascript



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 using namespace std; // Function declaration and// definitionint 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 codeint 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  // Function declaration and// definitionint 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 codeint 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);}

Java

 // Java program to implement// the above approachimport java.util.*; class GFG{ // Function declaration and// definitionstatic 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 new int[]{ p, q, r, s, t };} // Driver codepublic static void main(String[] args){         // Initializing the variables    int i = 100, j = 200;    int []k;     // Calling Operations function    k = Operations(j, i);     // Printing k    System.out.printf("The value of k = %d",                      k[k.length - 1]);}} // This code is contributed by shikhasingrajput

Python3

 # Python program to implement# the above approach # Function declaration and# definitiondef Operations(c, d):    p, q, r, s, t = 0,0,0,0,0;    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 codeif __name__ == '__main__':     # Initializing the variables    i = 100; j = 200;    k = 0;     # Calling Operations function    k = Operations(j, i);     # Printing k    print("The value of k = ", k[len(k) - 1]); # This code is contributed by shikhasingrajput

C#

 // C# program to implement// the above approachusing System; public class GFG{ // Function declaration and// definitionstatic 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 new int[]{ p, q, r, s, t };} // Driver codepublic static void Main(String[] args){         // Initializing the variables    int i = 100, j = 200;    int []k;     // Calling Operations function    k = Operations(j, i);     // Printing k    Console.Write("The value of k = {0}",                      k[k.Length - 1]);}} // This code is contributed by 29AjayKumar

Javascript



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