What will happen if a Function is tried to return more than one value at a time?
Last Updated :
21 Jul, 2022
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++
#include <iostream>
using namespace std;
int AddSub( int c, int d)
{
int x, y;
x = c - d;
y = c + d;
return (x, y);
}
int main()
{
int i = 100, j = 200, k;
k = AddSub(j, i);
cout << "The value of k = " << k;
}
|
C
#include <stdio.h>
int AddSub( int c, int d)
{
int x, y;
x = c - d;
y = c + d;
return (x, y);
}
int main()
{
int i = 100, j = 200, k;
k = AddSub(j, i);
printf ( "The value of k = %d" , k);
}
|
Java
import java.util.*;
class GFG{
static int [] AddSub( int c, int d)
{
int x, y;
x = c - d;
y = c + d;
return new int []{x, y};
}
public static void main(String[] args)
{
int i = 100 , j = 200 ;
int []k;
k = AddSub(j, i);
System.out.print( "The value of k = " + (k[k.length- 1 ]));
}
}
|
Python3
def AddSub(c, d):
x, y = 0 , 0 ;
x = c - d;
y = c + d;
return [x, y];
if __name__ = = '__main__' :
i = 100 ; j = 200 ;
k = 0 ;
k = AddSub(j, i);
print ( "The value of k = " , (k[ len (k) - 1 ]));
|
C#
using System;
public class GFG{
static int [] AddSub( int c, int d)
{
int x, y;
x = c - d;
y = c + d;
return new int []{x, y};
}
public static void Main(String[] args)
{
int i = 100, j = 200;
int []k;
k = AddSub(j, i);
Console.Write( "The value of k = " + (k[k.Length-1]));
}
}
|
Javascript
<script>
function AddSub(c , d)
{
var x, y;
x = c - d;
y = c + d;
return [x, y];
}
var i = 100, j = 200;
var k;
k = AddSub(j, i);
document.write( "The value of k = " + (k[k.length-1]));
</script>
|
Output:
The value of k = 300
Time Complexity: O(1)
Auxiliary Space: O(1)
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++
#include <iostream>
using namespace std;
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;
return p, q, r, s, t;
}
int main()
{
int i = 100, j = 200, k;
k = Operations(j, i);
printf ( "The value of k = %d" , k);
}
|
C
#include <stdio.h>
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;
return (p, q, r, s, t);
}
int main()
{
int i = 100, j = 200, k;
k = Operations(j, i);
printf ( "The value of k = %d" , k);
}
|
Java
import java.util.*;
class GFG{
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;
return new int []{ p, q, r, s, t };
}
public static void main(String[] args)
{
int i = 100 , j = 200 ;
int []k;
k = Operations(j, i);
System.out.printf( "The value of k = %d" ,
k[k.length - 1 ]);
}
}
|
Python3
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;
return [p, q, r, s, t ];
if __name__ = = '__main__' :
i = 100 ; j = 200 ;
k = 0 ;
k = Operations(j, i);
print ( "The value of k = " , k[ len (k) - 1 ]);
|
C#
using System;
public class GFG
{
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;
return new int []{ p, q, r, s, t };
}
public static void Main(String[] args)
{
int i = 100, j = 200;
int []k;
k = Operations(j, i);
Console.Write( "The value of k = {0}" ,
k[k.Length - 1]);
}
}
|
Javascript
<script>
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;
return [ p, q, r, s, t ];
}
var i = 100, j = 200;
var k;
k = Operations(j, i);
document.write( "The value of k = " ,
k[k.length - 1]);
</script>
|
Output:
The value of k = 0
Time Complexity: O(1)
Auxiliary Space: O(1)
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...