What is the diffence with these functions

This is a discussion on What is the diffence with these functions within the C++ Programming forums, part of the General Programming Boards category; have a queston that is confusing me....ok say we want to pass a call to a function by refence or ...

  1. #1
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    What is the diffence with these functions

    have a queston that is confusing me....ok say we want to pass a call to a function by refence or a pointer....like this


    Code:
    void squarebyRefernce( int &numberRef )
    {
    nuberRef *= numberRef;
    }
    
    //or this
    
    void squraebyReference( int *nPtr )
    {
    *nPtr = *nPtr * *nPtr;
    }

    My question is...what is the difference... and why would you chose one over the other?

    From what i gather they both do the same thing.
    big146

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    in squarebyRefernce( int &numberRef ) you're just passing a variable by reference; everything that will be done to numberRef in the scope of that function, will also be passed back to main.

    in the second function, you are simply passing in a pointer to an int...whatever happens to that pointer withing the scope of that function only happens there.

    so, the code below should give you an error about passing an int where a pointer is needed.

    Code:
    #include <iostream>
    using namespace std;
    
    
    void squarebyRefernceA( int &numberRef )
    {
    	numberRef *= numberRef;
    	cout << "A: " << numberRef << endl;
    }
    
    
    
    void squraebyReferenceB( int *nPtr )
    {
    	*nPtr = *nPtr * *nPtr;
    	cout << "B: " << nPtr << endl;
    }
    
    int main(){
    
    	int x = 4;
    	squarebyRefernceA( x );
    	squraebyReferenceB( x );
    
    
    
    	return 0;
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Im not sure I follow you....They both change the value of the caller in main right ?
    big146

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    yes big146, you are correct, Axon is wrong. The pointer changes to the integer pointed to by nPtr does NOT only occur within the scope of the function.

    The difference between the functions is actually only as far as the compiler is concerned. Internally, a reference and a pointer are the same.

    The differences between a reference and pointer as far as YOU (the coder) is concerned is that a reference can not be changed, where as a pointer can.
    You can also do pointer arithmetic, which you can't do with references.
    Also, a reference must be declared and initialized at the same time, where
    you can declare a pointer, and then later initialize it.

    Pointers are more flexible than references, but it also allows you to do some more mistakes. The general rule of thumb is to use references in your code, and only use pointers when it is necessary. Many people (myself included) oftentimes just use pointers, and done use references at all

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    What he is saying is that with the referance one you are actully changing the value of x with the pointer one your are just changing the value of the pointer to x
    Woop?

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    If you forget to clearly name the function ambiguity can arise
    consider
    func(variable) where func(int &var); it is unclear that variable is being modified by the caller
    func(&varibale) where func(int *var) clarifies that the calling function is changing the variable

    Strostrup discusses this on pg 99 of the C++ Programing Language as well. He also states that in many cases it is best to pass by value and return a value instead.

    consider this
    int x = 2;
    y = squarebyRefrence(x);//y=4
    z = y+x;// opps y= 8 when you wanted 6

    or this poly
    y=squarebyRefrence(x)+x;
    Last edited by curlious; 09-07-2004 at 11:06 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One other point, which was almost touched upon by bithub is that you are guarinteed that the first of the two will work, where as the second may crash your program:
    Code:
    void squarebyRefernce( int &numberRef )
    {
        nuberRef *= numberRef;
    }
    
    //or this
    
    void squraebyReference( int *nPtr )
    {
        *nPtr = *nPtr * *nPtr;
    }
    In the first example, numberRef will always be referring to some valid item. But, in the second, nPtr could very well be NULL, and as such, your program may do BadThings(TM) when you dereference it.

    With references, you are guarinteed to be referencing something when that point in the program is reached.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    when it is :
    (int &)
    it means that you have passed the address of an int to your function and when you work with the address (with the content of that address) it is natrual that the value will change .
    and when it is :
    (int *)
    it means that you have a pointer that points to the address of the int and you have passed this pointer to your function . therefore when you want to work with the int you must use the pointer (the pointer has the address of the int and for reaching the int we use the symbol '*' ) .
    therefore both of these ways are call by refrence .
    and it is depend on urself to choose which of them , but usually (int *) is used for array of int .

  9. #9
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Thanks for the reply's folks!That shed's some lite on it for me.So it basicly boils down to preference ? by using &var intstead of *var(which both basicly do the same thing) I am tying &var to an object and know that it is initailized with that object. Where using *var does'nt and could lead to a dangling pointer.
    big146

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    yep, pretty much. References are safer than pointers, and should probably be used when possible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21