Thread: i dont understand passing function arugments by refrence ,

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    i dont understand passing function arugments by refrence ,

    Code:
    int Function(int &x, int &r, int &p);
    
    int Function(int &x, int &r, int &p)
    {
    //does this just mean 
    int A = x; //what ever is typed in to x in main is stored in A...
                    because if it is , then i can do this without int &x, and just do                
                    int x, in the parameters and A will still get the x value..
    return x + r + p;
    }
    
    int main()
    {
    Function(1,2,3);
    return 0;
    }

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Yes references are unnecessary in your example.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    show me a good example then with my code.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void changeName(string& Name, string newName) // A reference so you can change its value
    {
         Name = newName;
    }
    
    void printName(string Name) // You don't need to change it so it's not a reference
    {
         cout << Name << endl;
    }
    
    int main() 
    {
        string myName = "William";
        printName(myName);
        changeName(myName, "Bill");
        printName(myName);
    
        return 0;
    }
    Last edited by SlyMaelstrom; 03-18-2006 at 04:43 AM.
    Sent from my iPadŽ

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Here's an example that shows two functions with the same body but with one using a reference and the other simply a copy of the value.
    Code:
    #include <iostream>
    
    using namespace std;
    
    void change_value (int &num)
    {
        num = 563;
    }
    
    void not_changing_value (int num)
    {
        num = 432;
    }
    
    int main ()
    {
        int number = 1;
        cout << number << endl;
    
        change_value(number);
        cout << number << endl;
    
        not_changing_value(number);
        cout << number << endl;
    
        return 0;
    }
    A reference is simply a pointer that dereferences itself and can be used as if it was a "normal" variable.
    The function change_value could therefore be written with pointers as
    Code:
    void change_value (int *num)
    {
        *num = 563;
    }
    And the call to the function would have to be changed to
    Code:
    change_value(&number);
    in main.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    thanks alot i think i finally understand the concept

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    Code:
    #include <iostream>
    
    using namespace std;
    
    void R(int &R)
    {
    R = 900;
    }
    
    int main ()
    {
    
    int total; //total now equals 900
    
    R(total);   
        
    return 0;
    }
    
    i was always thinking varibles in functions are local and cant be accessed outside of it :o

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    and i cant change the value of total then the value of R will change will it?

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Anddos
    ,
    i was always thinking varibles in functions are local and cant be accessed outside of it
    You were thinking right. Your code has no local variables. R is a reference parameter and in your example it is just an alias for the variable total defined in main.
    Kurt

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Anddos
    and i cant change the value of total then the value of R will change will it?
    You can think of them beeing the same variable with different names.
    Kurt

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #include <iostream>
    
    using namespace std;
    
    void R(int &R)
    {
    R = 900;
    }
    
    int main ()
    {
    
    int total; //total now equals 900
    cout << total << endl;  // does it?
    
    R(total);   
    cout << total << endl; // Ah now it does.
    
    return 0;
    }
    Quote Originally Posted by Anddos
    and i cant change the value of total then the value of R will change will it?
    R only exist in the scope of the functions, so changing total does nothing to it because it doesn't exist. In fact, at no point do they coexist in the same scope. By passing a reference to an integer, you're now theoretically renaming the variable and placing it into the functions scope. It acts just like the variable in main and anything you do to it will return with it to main.
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    look at this //it gets the same address as well?
    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    void R(int &R)
    {
    R = 900;
    }
    
    int main ()
    {
    cout<<"using refrences"<<endl;
    int total = 700;
    cout<<"before you change total"<<endl;
    cout<<total<< "        " << &total <<endl;
    Sleep(5000);
    R(total); 
    cout<<"after we call the R function"<<endl;
    cout<<total<< "      "  << &total <<endl;
    
    
    return 0;
    }

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The address never changes in total so I don't understand what you're trying to show there. If you were to output the address of R in the function it would be the same as the address of total. What you have to understand is that memory is always in global scope. So when you pass the address to a function variable, it acts like a reference to the arguement. The same as if I were to pass the memory address into global scope:
    Code:
    #include <iostream>
    
    int *ptr;   // A global pointer
    
    void func() {
       int foo = *ptr;
       std::cout << foo;
    }
    
    int main() {
       int bar = 22;
       ptr = &bar;    // This gives a global reference to the data.
       func();        // In the function, foo is set to the value of that address
                      // and outputted.
       
       return 0;
    }
    Sent from my iPadŽ

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You can think of a reference as an alias or a nickname for another variable. In other words, the variable has two names: the original name and the reference name. So, if you do this:
    Code:
    int Robert = 10;
    int& Bobby = Robert;
    both Robert and Bobby refer to the same location in memory, and at that location is the integer 10. So if one name is used to change the value a that location, the other name accesses that same changed value.
    Code:
           int: 10
           /     \
          /       \
    Robert       Bobby
    If you set Bobby equal to 30, what will Robert equal?

    If you have a function defined like this:

    void myFunc(int& aNum);

    and you do call it like this:

    myFunc(Robert);

    then inside the function aNum will be an alias or nickname for Robert. It's as if you had done this:

    int& aNum = Robert;

    That means any changes made to aNum will be made to the memory location accessed by Robert.
    Last edited by 7stud; 03-18-2006 at 09:30 AM.

  15. #15
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Or, it can be seen as a means for extending the scope of a variable.
    For what it's worth; You cannot change what the reference points to after it has been initialized like you can with a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM