Scope of the variables

Discussion in 'Computer Science & Culture' started by Gunjan, Jul 23, 2003.

Thread Status:
Not open for further replies.
  1. Gunjan Registered Member

    Messages:
    12
    I have problems with 'call by value' and 'call by reference'.What is the exact difference and in which cases which one we use?
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. wesmorris Nerd Overlord - we(s):1 of N Valued Senior Member

    Messages:
    9,846
    Pass by value means "use this here value right here"

    Pass by reference means "here is a pointer to the value I'm talking about"

    If you call a procedure that adds two numbers, call by value would require that you call it like: Add(1,3) whereas call by reference would be Add(x,y) or whatever you have pointing to the numbers you want added.

    Now in C I believe it can get complicated if you're using data structures and stuff, I've used it, but I'm really just a VB programmer. You can probably see though how sometimes it can be programatically advantageous to use either method depending on the need of the function. I believe that in general it is better form to use pass by reference.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    VB?!?!?! Whatever crown of nerdicity you had is GONE Wes!!!

    Please Register or Log in to view the hidden image!

    Just kidding...I still bow down to your throne

    Please Register or Log in to view the hidden image!



    Basically you have to think of it like this. Pass by value literally copies the value of whatever you pass to a function. Example

    Code:
    void myfunction(int a, int b)
    {
         int c;
         c = a + b;
         a = 2;
         b = 3;
    }
    
    void main()
    {
         int x, y;
         x = 9;
         y = 32;
         myfunction(x,y);
    }
    
    This function is call by value. When you call myfunction it copies the values of x and y which are 9 and 32 into a and b so that now a is 9 and b is 32. But a and b have NO knowledge of x and y. When we get back to main() x will still be 9 and y will still be 32.

    Now in call by reference something else happens.

    Code:
    void myfunction(int *a, int *b)
    {
         int c;
         c = *a + *b;
         *a = 2;
         *b = 3;
    }
    
    void main()
    {
         int x, y;
         x = 9;
         y = 32;
         myfunction(&x,&y);
    }
    
    Now when we call my function instead of passing in an int we pass in the address of an int (& means address of). And if you look at the function myfunction instead of taking in an int it now says int* which means it takes in a pointer to an int. Inside the function where we say *a = 2; What we mean is that since a is really a pointer, we want to change whatever a points to. Since a points to x we are changing the ACTUAL value of x. This is how we change the value of variables in different functions from within other functions. If there is any more confusion, just ask.

    -AntonK
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. wesmorris Nerd Overlord - we(s):1 of N Valued Senior Member

    Messages:
    9,846
    It's about necessity. I do a fair amount of VBA programming to support my network of databases of this that and the other that I've built at work. If Access have VCA instead.. I'd relearn C.

    Please Register or Log in to view the hidden image!



    And as always, I admire the loyal subjects. You may kiss my decoder ring. Hehe.
     
  8. okinrus Registered Senior Member

    Messages:
    2,669
    The C language passes all variables, even pointers, by value. However tn C++ you can pass by referance by doing something like

    Code:
    void A(int& a)
    {
           a++;
    }
    
    This of course changes a. This is basically what referances allow you to do. Changing the value of the formal varibles of the functions changes the value physical varibles. In c this code is written like

    Code:
    void A(int* pa)
    {
           (*pa)++;
    }
    
    And probably both codes compile to the same assembly code.
    So a referance in C++ can be thought of as a pointer that is always dereferanced.
     
  9. Gunjan Registered Member

    Messages:
    12
    Thanks a lot.I could understand your easy expanations.Now,let me see how well I can master or how long I can remember it!
     
  10. S.Tarafdar Registered Senior Member

    Messages:
    35
    would you please extend the idea to '&' as reference and to arrays?
     
  11. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    I would...except that I hate that idea in C++. My belief is that it has always confused people more than help them and because of this, I may say something that confuses you, and I don't want to do that.

    You say that all variables are passed by value in C, and while technically that is true, since C came out way before C++ and people called it by reference and by value then. Therefore, I call them by reference and by value.

    -AntonK
     
  12. malkiri Registered Senior Member

    Messages:
    198
    "If you call me by name, it is 'virt.' If you call me by value, it is 'worth.'" - Niklaus Wirth, on the common European and American pronunciations of his name
     
    Last edited: Jul 29, 2003
  13. okinrus Registered Senior Member

    Messages:
    2,669
    Yes your right about some C programmers call by referance is usually used to mean pointers. Referances are used in pascal and probably in Algol way before C. Language theorist, who deal with many languages, must use exact definitions of call by value and call by referance.

    In C and C++ when you pass an array it decays into a pointer. Therefore a referance to an array is a referance to a pointer. The only problem is that if you write something like
    Code:
    int main() { int A[20]; return 0; }
    
    You cannot change the value of A which points to the beginning of the array. So my bet is that you'd have to pass it by constant referance.
     
  14. malkiri Registered Senior Member

    Messages:
    198
    Here's another example:

    Code:
    int a[20];
    // This
    int* ptr1 = a;
    // results in the same as this
    int* ptr2 = &a[0];
    That is, specifying the name of the array without subscripts gives you a pointer to the first element of the array.
     
  15. cjard Registered Senior Member

    Messages:
    125
    by the way.. pass by reference or alue, isnt really a "scope of the variables" issues.. scope applies to how long a variable will last for. in most programming languages, a scope is defined with { } and a variable declared inside a particular { } scope, will cease to exist when the program execution leaves those brackets..

    pass by value and pass by reference is a bit like this:

    pass by value = your mum gives you 10 dollar note to go to the shops
    pass by reference = your mum gives you her ATM card and says "go get 10 dollars and go to the shops" ..

    that atm card is a link to 10 dollars, but it is not 10 dollars. someone else could take all the money out the account before you got there.. but if you ahve 10 dolalrs in your hand, it is die hard 10 dollars..

    the important difference here is what you do with the value.. if you get a pen out and draw "20" on your 10 dollar note, you changed that note.. but you didnt change the 10 dollar note your brother has in his pocket, nor any of the 10 dollar notes your mum has in her pocket. but if you contacted the bank and got them to draw on all the 20 dollar notes in the atm, well anyone with an atm card will now get 20 dollar notes..

    same in your program. if you have a function (you) that recieves a pass-by-value (10 dollar note), it can change it, but other people sstill dont have access to it to use it as a 20 dollar note for their own use.. in this case if you want your mum to have access to a 20 dollar note, you have to pass it back (by value).

    but if you get a pass by reference (atm card) you can alter that thing (the bank balance) and anyone else who has a link to it (your dad has a duplicate atm card for example) will access the altered value, without needing to pass it back

    in java, which probably aint your language.. this is all dead easy.. primitives (int, byte, boolean...) are passed by value.. objects (String, File, Window...) are passed by reference.

    heres another example:

    passing by reference is a bit like your parent giving you a copy key to the car.. you can now take the car, if you bang it (alter it), and put it back on the drive, your parent has access to a banged car too.. the car key is your reference to the car; it allows you to get access to the car.. but it is not a car itself
     
Thread Status:
Not open for further replies.

Share This Page