return command

This is a discussion on return command within the C++ Programming forums, part of the General Programming Boards category; How does the return command work? Thanks in advance for any help you guys can give me! =D...

  1. #1
    C++ and openGL Raeliean's Avatar
    Join Date
    Jul 2005
    Posts
    28

    return command

    How does the return command work?

    Thanks in advance for any help you guys can give me! =D
    Be inspired.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    it ends the execution of the function where it is inserted, evaluates the expression which holds, and sends it back to the caller

    Code:
    int foo(){
        return 4;
    }
    void bar(){
        int val;
        val = foo();
        // val has value 4
    }
    calling function foo will return 4 to its caller

  3. #3
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    also putting return 0; ast the end of the program means the program has ran successfullygenerally if you wanted to make a multiplication function

    Code:
    int a;
    int b;
    int c;
    
    mult( a, b ) {
    a * b = c;
    return c;
    }
    
    // this would multiply the vars passed iunto it and return the value of c
    // ex.
    int main() 
    {
    cout<<"enter the first number to be multiplied\n";
    cin>> a;  // put the uisers input into a
    cout<<"\n the second? \n";
    cin>> b; //put the users value into b
    cout<<"\n the calue of << a <<" x "<< b << is << mult( a, b )"\n";
    // this will return 4 if both numbers entered are 2
    }
    i think thats good enough :P
    Last edited by C+noob; 07-06-2005 at 10:42 PM. Reason: mistake

  4. #4
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    i did it real fast so there might be minor errors. hope it helps

  5. #5
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Also, you should note that you need to return the same type as the function.

    Code:
    int main()
    {
    
    return 0;
    
    } //0 is a valid integer
    
    float function()
    {
    
    return 3.14;
    
    }//3.14 is a valid float value
    
    istream& input( istream& input )
    {
    
    return input;
    
    }//input is an input stream

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 12:52 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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