0
Check the code in the description for a return issue. What's the difference between printing and returning a value ?
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
0
Ace does this mean that return hides the value but prints it when in need ?