stop using the same names for your function parameters and your variables and it will make your learning easier.
for instance don't do this:
int square(int x)
{
return x * x;
}
int main()
{
int x = 5;
int square = square(x);
}
instead do this:
int square(int number) // or value, arg, param, i or whatever
{
return number * number;
}
int main()
{
int value1 = 5;
int value2 = square(value1);
}
see in this version, the same word is never used for 2 different things, so you can read it and not get as confused.
In the real world, it is common for the variable and parameter names in your program to be the same many times, but once you learn more you won't be confused by it (you will be able to see which "thing" each word is refering to. Right now, avoid it ... I recommend avoiding it for 2-3 months.
But anyway, that said, here's some more info about it.
In C and C++ a variable name is looked up following a sequence of rules that normally follow some semblance of nesting.
here's a silly example to illustrate:
int x = 5; // defines a global variable x, with the intial value 5int square(int x) // declares a function parameter x{ int result; // declares a local variable result result = x * x; // uses the local variables result and x return result; // uses the local variable result}int cube(int x) // decalares a function paramter x, NO RELATION to the above x in square OR the global variable x{ int result; result = x * x * x; // uses the local variables return result;}int square_x() // doesn't take any arguments{ return x * x; // returns the square of the global variable x, which will be 5 * 5 IF noone has changed its value before calling this function. }int square_y() // doesn't take any arguments{ return y * y; // this will not compile, there is no local or global y to call. The y below in main cannot be seen directly by this function at all, this function can only see local variables, parameters, and global variables.}int main(){ int y = 3; // declares a local variable y, intially set to 3 int r1; int r2; r1 = square(x); // uses the global x, returns 25 r2 = cube(y); // uses the local y, returns 27}
what is MEANS when a function takes a parameter is that the compiler generates code to COPY the value of the object you pass into the function during the call.
So when we say that function "square" takes 1 argument, we mean that everybody who wants to call the function must give it a value, which will be copied onto the stack so the function can operate on it. The value the function operates on is not a specific variable or location in memory, it is a specific place on the stack ... lets imagine we are on an x86 machine without optimizations, it might likely be on a place in the stack might call -4, meaning 4 bytes from the current "top" of the stack. When the compiler generates code for "square(x)" what is does is copy the value of x to the top of the stack, adjust the pointer at the top of the stack (so that x will be at -4) and then "call" the function (transfer execution to the function) ... the function does not know what the "x" in main is .. it DOES however know that it has 1 argument, which is located on the stack, so it does math on that. Then when it has computed the results of that operation, it returns its result (on x86 this usually means it copies the result value into the AX register and then transfers control back to the function which called it).
So the "x" in the function is a alias for whatever happens to be located 4 bytes from the top of the stack, when the function begins executing.