Advertisement
ArtisOracle

Untitled

Oct 22nd, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int main()
  2. {
  3.     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.
  4.     int z = 2;
  5.     function2(z);   // Correct. In 'function2', 'a' will have the value that 'z' has right now. Which is 2.
  6. }
  7.  
  8. int function(int z) // z is a parameter to the function 'function'
  9. {
  10.     int x, y;   // x, y are local variables to the function 'function'
  11.  
  12.     x = y = 10;
  13.     z = x + y;
  14.     return z;   // returns the VALUE of z only.
  15. }  
  16. // x, y, and z no longer exist. They're gone. Only the VALUE of z is returned to the caller. z itself is destroyed.
  17.  
  18. int function2(int a)    // a is a parameter to the function 'function2'
  19. {
  20.     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.
  21.     x = y = 10;
  22.     a = x + y;
  23.     if (a > 10)
  24.     {
  25.         int b, c;   // b and c are visible INSIDE the if statement block (the curly brackets)
  26.         b = 10;
  27.         c = b;
  28.         x = b + c;  // x refers to the local variable inside function2.
  29.     }
  30.     // 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.
  31.     return a;   // returns the VALUE of a only.
  32. }  
  33. // 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