Hello all!
I am having trouble with the following code:
I started playing around with the code to get an idea of how it worked only to find the c++ rug underneath my feet slip away.Code:#include <iostream> using namespace std; void changeArgument (int x) { x = x + 5; } int main() { int y = 4; changeArgument( y ); // y will be unharmed cout << y; // still prints 4 }
I omitted the variable declaration (y) passing an integer 4 as the argument, but the compiler is complaining that there were "too few arguments to function void changeArgument(int)."
I also changed "cout <<y;" to "cout << changeArguement ( 4 );"
Same error....
Does anyone know why this is happening? I was under the impression that I could pass arguments of type integer to this function for it to work.
Why is it required that in main() one needs to declare an additional variable, and assign it a value? As the x variable is already declared locally inside the function, I thought that one would only need to pass an argument of its type for it to return a value. Despite the fact that changeArguments() return type is void, running the above code Does return a value.
Any help would be immensely appreciated.
Thank you so much for your time!
spark*



2Likes
LinkBack URL
About LinkBacks




.