0
Please refer to the code below and explain me the following : 1. Why does only one value get printed even if we are using return
#include <iostream> using namespace std; int calc(int x, int y) { int sum=x+y; int mul=x*y; int rem=x%y; return sum,mul,mul,rem,sum; return mul; } int main() { cout<<calc(5,2); } Here output=7
4 Answers
+ 3
You can't return multiple value directly.
+ 3
You should keep in mind that a function can return ONLY ONE entity at a time from a function.
your program returns from the function back to the place where it was called as soon as it encounters a retuen statement.
in line "return sum,mul,mul,rem,sum;"
your program evaluates evaluates the expression and returns the last value that is *sum* in this case
+ 2
You are returning multiple valued at a time have a look on it.. it can't be done Garvita Bhateja
Hope this helps âïž
0
If you want to take the all the value of that function you can use an array on the calc parameter and put the values on that array. Then you don't also need a int type as array becomes a reference type in the parameter :
https://code.sololearn.com/c3TZLt89T9xa/?ref=app