Thread: how to return more then one value in a function?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    how to return more then one value in a function?

    i know that each function can only return one value like integer or string,
    but if i have a function to process data but i want to return astring and an integer, how to do it?

    i am thinking of making a class....but....can a second function get any variable return in function 1?
    e.g

    funtion1 :

    c= a+b;
    d= c+50;
    return d;

    function2 :

    return c;

    can it be done if all the variable is define in the class as public variable?

    please help....thank you!

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you can "return" things via pointers and references.

    for instance:

    int Func(char *buffer, int & somethingelse)
    {
    somethingelse = 10;
    strcpy(buffer, "blah");
    return strlen(buffer);
    }

    somethingelse, buffer, and the return value are all going to be accessable after the function call
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    do you mean i dun need to define a class?

    see the example code first...
    Code:
    int function1()
    {
    int a = 2;
    int b = 3;
    c= a+b;
    d=b-a;
    return d;
    }
    
    int function2(int x)
    {
       return x+2;
    }
    do you mean that i can do the following in the int main()??
    Code:
    int main()
    {
    
    int m,n;
    m = function1;
    n = function2(c);
    
    return 0;
    }

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I'm not sure what you're asking. I thought you wanted to know how to make a function return more than one value.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    int function1()
    {
       int a = 2;
       int b = 3;
       c= a+b;
       d=b-a;
       return d;
    }
    First of all, where did the variables c and d come from? They're not parameters to function1, and you didn't declare them in the function, so function1() won't even compile.

    Since it would only take 3 minutes to test whether you can do the things you want to do with the above function--why don't you?

    (Hint: you need to learn about passing-by-value and passing-by-reference.)
    Last edited by 7stud; 05-02-2003 at 01:56 PM.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    sorry for typing the code wrongly

    mmmm....i think i know what you mean fillyourbrain.

    and i will try that out~~~

    thank you all~~~~

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    you could use a struct, but chances are, thats not what you want.

    Code:
    struct testing
    {
        int x;
        float y;
    };
    
    testing maketest(int x, float y)
    {
         testing test1;
         test1.x = y;
         test1.y = x;
         return test1;
    }
    
    #include <iostream.h>
    
    int main()
    {
         int number = 500;
         float decimal = 2.4;
    
         testing test1 = maketest(number,decimal);
         // should swap the two variables... rounding the decimal
    
         cout << test1.x << endl << test1.y << endl;
    
         return 0;
    }
    Last edited by Trauts; 05-02-2003 at 07:01 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int returnmany( void )
    {
        return 1,2,3,4,5,6,7;/*;)*/
    }


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM