Some help on a program

Started by
41 comments, last by Blckknight118 13 years, 6 months ago
Quote:Original post by KulSeran
First off, variable names only work in a scope. Function calls change scope. Function calls also remap variable names from the caller to the callee.
Secondly, functions take in parameter between (), and the number of parameters has to match exactly.
Lastly, the function return value comes back where the function call is as the "value" of that function.

Maybe the following working example will help you see some of this.
*** Source Snippet Removed ***


Wow.....I completely don't understand why its displaying 6,9 and 4. I don't understand when the numbers are being swapped.
Advertisement
Quote:Original post by Blckknight118
ely don't understand why its displaying 6,9 and 4. I don't understand when the numbers are being swapped.


Well the first output is from this line in 'foo'....

std::cout << y << std::endl;

Now what is 'y' defined as? Remember the 'y' in foo() is NOT the same as the y in main(). So where and how is this particular y defined?
Quote:Original post by empirical2
Quote:Original post by Blckknight118
ely don't understand why its displaying 6,9 and 4. I don't understand when the numbers are being swapped.


Well the first output is from this line in 'foo'....

std::cout << y << std::endl;

Now what is 'y' defined as? Remember the 'y' in foo() is NOT the same as the y in main(). So where and how is this particular y defined?


'Y' is definted as 4 in main, but the display says that it comes out as 6....
I know the Ys aren't the same but where the hell is the 'y' in foo getting the damn 6 from?
Quote:Original post by Blckknight118


'Y' is definted as 4 in main, but the display says that it comes out as 6....


Nope. I didn't ask about the 'y' in main(), I asked about the one in foo(), which is completely separate to the one in main(). They happen to have the same name 'y' but they are totally separate.

Where is the 'y' that is used in foo() defined?
Quote:Original post by empirical2
Quote:Original post by Blckknight118


'Y' is definted as 4 in main, but the display says that it comes out as 6....


Nope. I didn't ask about the 'y' in main(), I asked about the one in foo(), which is completely separate to the one in main(). They happen to have the same name 'y' but they are totally separate.

Where is the 'y' that is used in foo() defined?


Well, I know it's definted in the parameters of foo. That is, int y in foo's declaration. I just don't know where its getting the 6 from if its not in the function and main() only has two declarations.
Remove the variables from the equation...
#include <iostream>int foo( int x, int y, int z ){  std::cout << y << std::endl;  std::cout << z << std::endl;  return x;}int main( int argc, char **argv ){  std::cout << foo( 4, 6, 9 ) << std::endl;  return 0;}

Now do you see what happened?

or... maybe this could make it more clear that the names are totally arbitrary and only have to match within any one particular scope:
#include <iostream>// prototype defining foo() taking 3 intsint foo( int, int, int );// main function with 2 parametersint main( int argc, char **argv ){  std::cout << foo( 4, 6, 9 ) << std::endl;  return 0;}// definition of foo, naming the 3 input ints as x,y, and zint foo( int x, int y, int z ){  std::cout << y << std::endl;  std::cout << z << std::endl;  return x;}
Quote:Original post by Blckknight118

Well, I know it's definted in the parameters of foo. That is, int y in foo's declaration. I just don't know where its getting the 6 from if its not in the function and main() only has two declarations.


You are on the right track. It IS defined in the parameter list you are correct, specifically as the SECOND parameter. The first is x and last is z.

Now take a look at the call to foo() thats in main()

std::cout << foo( y, a, b ) << std::endl;

What is being passed as the SECOND parameter when foo() is called?
Quote:Original post by empirical2
Quote:Original post by Blckknight118

Well, I know it's definted in the parameters of foo. That is, int y in foo's declaration. I just don't know where its getting the 6 from if its not in the function and main() only has two declarations.


You are on the right track. It IS defined in the parameter list you are correct, specifically as the SECOND parameter. The first is x and last is z.

Now take a look at the call to foo() thats in main()

std::cout << foo( y, a, b ) << std::endl;

What is being passed as the SECOND parameter when foo() is called?


So 'y' is being specifically passed as 'a' since they are both the second parameters. And since 'y' is the first variable to be declared the display is y=a=6 since the a in main equals 6 and the a is being called a parameter. But the z isn't being called main...Never mind I got that. I just don't understand the return x; or how the last cout is showing 4.
Quote:Original post by Blckknight118
So 'y' is being specifically passed as 'a' since they are both the second parameters. And since 'y' is the first variable to be declared the display is y=a=6 since the a in main equals 6 and the a is being called a parameter. But the z isn't being called main...


No. 'a' is being passed. Its contents, '6', is then copied into the 'y' which is defined in foo(). Remember that the 'y' in foo() is NOTHING to do with the 'y' in main(). At all.

There is no 'z' in main() its true, but there IS in foo. The 'z' in foo() is the THIRD parameter.

What is being passed as the THIRD parameter in the line below?

std::cout << foo( y, a, b ) << std::endl;
Quote:Original post by empirical2
Quote:Original post by Blckknight118
So 'y' is being specifically passed as 'a' since they are both the second parameters. And since 'y' is the first variable to be declared the display is y=a=6 since the a in main equals 6 and the a is being called a parameter. But the z isn't being called main...


No. 'a' is being passed. Its contents, '6', is then copied into the 'y' which is defined in foo(). Remember that the 'y' in foo() is NOTHING to do with the 'y' in main(). At all.

There is no 'z' in main() its true, but there IS in foo. The 'z' in foo() is the THIRD parameter.

What is being passed as the THIRD parameter in the line below?

std::cout << foo( y, a, b ) << std::endl;


b is the third parameter. So does that mean that b is passing the value 9 to y. I understand that, but the last cout that has the whole function makes no sense.

This topic is closed to new replies.

Advertisement