Thread: Return Values

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    38

    Return Values

    I was told that when returning a value from a function, you can only return one. So why does this code work? And can you explain what happens in the functions? thanks

    Their is a structure called Distance that includes distances in feet and inches. the user enters 2 distances (feet,inches) and stores them in 2 type Distance variables. then this function is involked to return the larger of the 2 Distances;
    Code:
    Distance bigengl(Distance dd1, Distance dd2)
    	{
    	if(dd1.feet > dd2.feet)       //if feet are different, return
    		return dd1;                // the one with the largest feet
    	if(dd1.feet > dd2.feet)
    		return dd2;
    	if(dd1.inches > dd2.inches)   //if inches are different,
    		return dd1;                //return one with largest
    	else                          //inches, or dd2 if equal
    		return dd2;
    	}
    #include <Jesus.h>
    It will save your life

  2. #2
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    the return type depends on what kind of function you use..

    int main for example return ant int (duh!) 0 for all ok everything else somethin went wrong..

    void no return at all

    char returns char..
    double returns double..
    etc..

    Code:
    Distance bigengl(Distance dd1, Distance dd2)
            {
            if(dd1.feet > dd2.feet)       //if feet are different, return
                    return dd1;                // value in dd1
    if(dd1.feet > dd2.feet)
                    return dd2;               // return value in dd2
            if(dd1.inches > dd2.inches)   //if inches are different,
                    return dd1;                //return value of dd1
    else                          //inches, or return value of dd2 if equal
                    return dd2;
            }
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    If think you are confused by all the return statments in your function. You can have as many return statments as you want, but I would advise you to have as few as possible (there is a little performence hit if you have many return statements). Only one return statement is executed and therefore only one object (a Distance object in your case) is returned.

    And can you explain what happens in the functions?
    It returns the largest Distance object

    Well I think the line
    Code:
    if(dd1.feet > dd2.feet)//second if clause
    should be
    Code:
    if(dd1.feet < dd2.feet)
    or else it donīt makes any sense.

    I donīt know if you have covered references yet but if you have I would advise you to have const refernces as your argument (dd1, dd2). Use this to avoid unneccesary copies of object(s). When object(s) gets rather big you will get a BIG perfomance hit.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    #include <Jesus.h>
    It will save your life
    I like! I like! Very original.

  5. #5
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28
    yes, the function is returning only one variable. But it depends on the case which one to return. It will return only the one where the IF operator is true or if no IFs are tru, then it will return the last return;

    P.S.: You can return more than one variable from a function using pointers...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM