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?

  • Difficulty Level : Easy
  • Last Updated : 27 Dec, 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? 

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 <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;
}

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);
}

Java




// Java program to implement
// the above approach
 
import java.util.*;
 
class GFG{
 
// Function declaration and
// definition
static 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 code
public 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
# definition
def AddSub(c, d):
    x, y = 0, 0;
    x = c - d;
    y = c + d;
 
    # Returning two integers
    # instead of one
    return [x, y];
 
# Driver code
if __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 approach
using System;
 
public class GFG{
 
// Function declaration and
// definition
static 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 code
public 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




<script>
// javascript program to implement
// the above approach// Function declaration and
// definition
function AddSub(c , d)
{
    var x, y;
    x = c - d;
    y = c + d;
 
    // Returning two integers
    // instead of one
    return [x, y];
}
 
// Driver code
 
// Initializing the variables
var i = 100, j = 200;
var k;
 
// Calling AddSub function
k = AddSub(j, i);
 
// Printing k
document.write("The value of k = " +  (k[k.length-1]));
 
// This code is contributed by shikhasingrajput
</script>

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 <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);
}

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);
}

Java




// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function declaration and
// definition
static 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 code
public 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
# definition
def 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 code
if __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 approach
using System;
 
public class GFG
{
 
// Function declaration and
// definition
static 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 code
public 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




<script>
// javascript program to implement
// the above approach
 
// Function declaration and
// definition
function Operations(c , d)
{
    var 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
// Initializing the variables
var i = 100, j = 200;
var k;
 
// Calling Operations function
k = Operations(j, i);
 
// Printing k
document.write("The value of k = ",
               k[k.length - 1]);
 
// This code is contributed by shikhasingrajput
</script>

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!