Functions &passing parameters by reference

This is a discussion on Functions &passing parameters by reference within the C++ Programming forums, part of the General Programming Boards category; Hi, how do u write a prototype of a function that passes by reference Code: //function definition int playerLoss(int &playerMoney) ...

  1. #1
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26

    Functions &passing parameters by reference

    Hi,

    how do u write a prototype of a function that passes by reference
    Code:
    //function definition
    
    int playerLoss(int &playerMoney)
    {
       int x=45;
       return playerMoney-=x;
    }
    and at the moment i have the definition as
    Code:
    int playerLoss( int  & a );
    I keep getting an error message saying:cannot convert parameter 1 from 'int *__w64 ' to 'int &'

    Im using msvc express...

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The error is not in the declaration/definition of the function. It's the way you call it.
    Show the code.
    Kurt

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Code:
    void foo (int& my_int)
    {
    	my_int = 50;
    }
    
    int main
    {
    	int a = 10;
    	cout << a << endl; // 10
    	foo(a); // OK
    	foo(&a); // ERROR
    	cout << a << endl; // 50
    }
    In other words, don't put & before the variable you pass.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    thanx elysia that worked.
    So You only use '&' in the function prototype/definition and not actually when u make the function call

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Correct. & is used to get the address of a variable - thus only required when using pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    makes sense now.Thanks again

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    & has several meanings.
    In variable declarations (such as your function prototype) it means declaring a reference.
    Before a variable (but not following a type-name) it is the address-of operator (to obtain a pointer to that variable).
    And finally it is the bitwise AND operator (between two variables/literals).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions which return a reference
    By Stonehambey in forum C++ Programming
    Replies: 10
    Last Post: 07-05-2008, 02:43 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 06:56 AM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 05:42 PM
  5. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 01:00 AM

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