Thread: Multiple returns from one function?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    5

    Multiple returns from one function?

    I need to return three integers from one function to main, but I'm not sure how to specify where to return the variables to main..

    What i need to know is how to format the return statement in the function, how to format the function prototype to accept multiple returns, and how to specify where the returned variables are placed in the function call.

    I cannot find information on this anywhere. Any help would be greatly appreciated.

  2. #2
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    it wouldn't be a function then

    A function, by definition, can only return one value.
    If you need to retain additional information used in the function, supply pointers in the parameter field or use static/global variables (when appropriate).
    Last edited by Dave Jay; 03-21-2002 at 08:56 PM.

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I think you can return a structure. SO just wrap up the values in a structure variable and return that.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Or give pass a struct to the function, let and let the function fill the struct with data.

    Code:
    #include <stdio.h>
    
    struct data_s
    {
        int data1;
        int data2;
        int data3;
    };
    
    void function (struct data_s *data)
    {   
        data->data1 = 1;
        data->data2 = 2;
        data->data3 = 3; 
    }
                
    int main () 
    { 
        struct data_s data;
        
        function (&data);
        
        printf ("%d %d %d\n", data.data1, data.data2, data.data3);
    
        return 0; 
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    There are three ways:
    1. Use parameters and pass them by reference (& in front of them).
    2. Use parameters and make them pointers.
    3. Use Nutshell's idea and return a structure of three integers.

    Here's an example of method 1.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void calculate(int a, int b, int &sum, int &diff, int &prod, int &quo);
    int main(void)
    {
       int a = 10;
       int b = 5;
       int sum, diff, prod, quot;
    
       calculate(a,b,sum,diff,prod,quot);
       printf("%d+%d=%d\n",a,b,sum);
       printf("%d-%d=%d\n",a,b,diff);
       printf("%d*%d=%d\n",a,b,prod);
       printf("%d/%d=%d\n",a,b,quot);
       return 0;
    }
    
    void calculate(int a, int b, int &sum, int &diff, int &prod, int &quo)
    {
       sum = a + b;
       diff = a - b;
       prod = a * b;
       quo = a / b;
    }
    Last edited by swoopy; 03-22-2002 at 01:04 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Swoopy, you're confused here. You cannot pass by reference in C. That is a C++ feature. This is not valid C code:
    Code:
    void myfun( int &x ) { x = 20; }
    int main( void ) {
        int x = 0;
        myfun( x );
        return printf("%d", x ); //x will NOT equal 20
    }
    This would change the value of x in C++, but it does not in C.

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

  7. #7
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17

    Parameters

    You may want to review formal and actual parameters. You use 1 function and multiple function calls. I am still new myself but I believe this is a viable solution. Shiro gives a good example of what I am trying to say. Good Luck.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Thanks Quzah. I did not know c did not have this feature.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'll repost my example with pointers, since references aren't c.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void calculate(int a, int b, int *sum, int *diff, int *prod, int *quo);
    int main(void)
    {
       int a = 10;
       int b = 5;
       int sum, diff, prod, quo;
    
       calculate(a,b,&sum,&diff,&prod,&quo);
       printf("%d+%d=%d\n",a,b,sum);
       printf("%d-%d=%d\n",a,b,diff);
       printf("%d*%d=%d\n",a,b,prod);
       printf("%d/%d=%d\n",a,b,quo);
       return 0;
    }
    
    void calculate(int a, int b, int *sum, int *diff, int *prod, int *quo)
    {
       *sum = a + b;
       *diff = a - b;
       *prod = a * b;
       *quo = a / b;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM