Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main()
- {
- function(z); // WRONG. You will get a compiler error. Even though 'z' is a parameter to 'function', it needs something fed into it. It needs a declared, initialized variable.
- int z = 2;
- function2(z); // Correct. In 'function2', 'a' will have the value that 'z' has right now. Which is 2.
- }
- int function(int z) // z is a parameter to the function 'function'
- {
- int x, y; // x, y are local variables to the function 'function'
- x = y = 10;
- z = x + y;
- return z; // returns the VALUE of z only.
- }
- // x, y, and z no longer exist. They're gone. Only the VALUE of z is returned to the caller. z itself is destroyed.
- int function2(int a) // a is a parameter to the function 'function2'
- {
- int x, y; // x, y are local variables to the function 'function2'. They are NOT, I repeat, NOT the same x and y as in 'function'. These variables are now ENTIRELY DIFFERENT EVEN IF THEY HAVE THE SAME NAME, because they are in a different set of { }s.
- x = y = 10;
- a = x + y;
- if (a > 10)
- {
- int b, c; // b and c are visible INSIDE the if statement block (the curly brackets)
- b = 10;
- c = b;
- x = b + c; // x refers to the local variable inside function2.
- }
- // b and c are no longer accessible. They've been destroyed. If you try to use them now you will get an undeclared identifier error.
- return a; // returns the VALUE of a only.
- }
- // a, x, and y no longer exist to us anymore. They're gone. Only the VALUE of a is returned to the caller. a itself is destroyed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement