Thread: Confused with call by reference...

  1. #1
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131

    Confused with call by reference...

    I am confused how another method of function call by reference works...withought using the pointer....
    the method uses reference parameters in function header....as
    int anyfunction( int &var)
    and simply variable name is used for all work in the function..
    as for this function header the var can be used as
    var = var*var;

    can any one explain why this can be done...?
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    call by reference like
    Code:
    int func (int& var) { var = var + 1; }
    and call by reference like
    Code:
    int func (int* var) { *var = *var + 1; }
    are essentially the same. the difference lies not in the program executable in the end, but in the compiler. C++ allows the use of variables with references pointing to previous variables only in the scope of a function. this is an improvement on C, in an effort by the creators of C++ to avoid excessive pointer use in functions.

    in other words, this is a handy feature included in C++ to make writing programs easier, but it doesn't compile to anything different than by using pointers instead.

  3. #3
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    Can I ask which method is frequently... used.. by good programmers....
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by jawwadalam
    Can I ask which method is frequently... used.. by good programmers....
    the C method of passing a pointer to simulate call by refrence is considered depreciated in C++. pointers still have legit uses, but this is no longer one of them.
    hello, internet!

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Note: it's not which is common... it's when do you need to use it, calling by refrence prevents the creating a copy of the variables when you call by value, and it's useful when you pass larg objects.
    if you want to pass by refrence
    Code:
    int func (const int& var)
    so if so make any changes the compiler will give you a compile time error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Using call by reference in this program
    By BurleMD in forum C Programming
    Replies: 3
    Last Post: 10-28-2006, 12:23 PM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  4. call by reference question
    By staticalloc in forum C Programming
    Replies: 2
    Last Post: 09-09-2004, 12:58 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM