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 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

26th Sep 2020, 8:04 AM
Garvita Bhateja
4 Answers
+ 3
You can't return multiple value directly.
26th Sep 2020, 8:06 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 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
26th Sep 2020, 8:07 AM
Arsenic
Arsenic - avatar
+ 2
You are returning multiple valued at a time have a look on it.. it can't be done Garvita Bhateja Hope this helps ✌️
26th Sep 2020, 8:16 AM
Piyush
Piyush - avatar
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
26th Sep 2020, 8:19 AM
The future is now thanks to science
The future is now thanks to science - avatar