0

Examine this code

#include <iostream> using namespace std; int operate (int y, int x ) { return y - x ; } int main () { cout << operate (3, 4) ; } <1> what's the difference between variables as parameters and variable in its way <3> what made the operation (3-4)= -1? was the "return"? <4> why should I put y and x as variables and as parameters in func operate and calculate them in the func main ?????

19th Jan 2017, 7:54 PM
Amr Monsef
Amr Monsef - avatar
4 Answers
+ 3
1.- No diference, the parameter you give to the function will be alocated in the plave you define 2.- Yes, return y - x = return 3-4 3.- Because you wanted?
19th Jan 2017, 8:06 PM
Aldo Alexandro Vargas Meza
Aldo Alexandro Vargas Meza - avatar
+ 2
3 is y and 4 is x the operation was made cause of the return
19th Jan 2017, 8:07 PM
Kawaii
Kawaii - avatar
+ 2
<1>variables are the same as when you pass them as parameter, the only difference is that their value are defined when the function is called when they are used as parameter <3>the operation 3-4 is always -1, the fact that you could print it with cout is indeed thanks to the return (try removing it to see what happens :) ) <4> you should do what you need to do, so I do not really understand what you mean by this point sorry :/
19th Jan 2017, 8:08 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
x and y is the variable parameter of function operate. that is where you pass the value that you given and it is temporary container for your value that been passed, when the function return the value the temporary variable will destroy upon finishing his operation.
19th Jan 2017, 11:53 PM
Harvey S. Cajegas
Harvey S. Cajegas - avatar