Thread: a simple C++ question

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    a simple C++ question

    Anyone know what means if got a '&' after the variable
    in the function???
    when i read the book , it's said pass the variable by reference, but i still not understand that!!
    so anyone can explain to me and give me some example??
    Thanks............................

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    an & before a variable suggests its actual address.

    call by reference means that if any change in value is done to the variable passed in the functon then it will be a permanent change.

    eg

    void func1(int &a, int b)
    {
    a=a+1;
    b=b+1;
    }

    now if in main() you display value of a it will be +1 but b will remain as the original val.
    -

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    int main()
    {
    int A, B, C;
    A=2;
    B=4;
    C=6;
    mystery(B,C,A);
    cout<<A+B+C;
    }

    void mystery(int D,int& E, int C)
    {
    int A;
    A=C+1;
    E=A+C;
    C=C*2;
    }

    so is that A=2 (unchange)
    B=4 (unchange)
    C=5 (changed)
    and the output is 11

    is that correct??
    i just found the question from my tutorial!!
    Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM