0

Check the code in the description for a return issue. What's the difference between printing and returning a value ?

https://code.sololearn.com/cOqlq717eOB8/?ref=app

1st Aug 2019, 5:32 PM
The unknown 321
The unknown 321 - avatar
4 Antworten
+ 2
In your case, those two function are actually different. Let's start with the calculation2, since it's the easiest ro understand. The calculation2 function will add its parameters and then directly print it to the console, and that it. However, the the other function (calculation) add those numbers and return that sum, how the sum is treated is up to the function caller. In your case, you want to print the sum, so you call calculation inside the cout statement, like this cout << calculation(20, 5); But what if you want to assign that sum to another variable? Then you call the function like this *inside main function int sum = calculation(12, 7); You can't do that with calculation2 since it's only calculate the sum and print it directly, so it has no value to be evaluated further
1st Aug 2019, 5:47 PM
Agent_I
Agent_I - avatar
0
Ace does this mean that return hides the value but prints it when in need ?
1st Aug 2019, 6:09 PM
The unknown 321
The unknown 321 - avatar