Shop OBEX P1 Docs P2 Docs Learn Events
Propeller C Multiple return — Parallax Forums

Propeller C Multiple return

MJFMJF Posts: 19
edited 2013-09-27 08:05 in Learn with BlocklyProp
Can anyone help with multiple returns from a function in C.
I am looking to pass one piece of data into a function and get two pieces of data returned.


Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-09-20 15:28
    What sorts of values do you want to return? Any version of C can only return one value, but you can pack several values into a 32-bit long or 16-bit word and return that. You can also the address of an array to the function and return values by storing them into the array. You can use the dynamic memory allocation routines to provide the address of a small area of memory and return that address from the function ... all sorts of techniques.
  • MJFMJF Posts: 19
    edited 2013-09-20 15:36
    Mike

    it would be to integer values a=1234 b=4567.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-09-20 15:46
    Given the values you listed, two of them would fit into a 32-bit long. If x and y are those values, you could declare the function as one that returns a long and do a return(x<<16+y). You'd store this returned value in a temporary variable z and then you could write z>>16 to get x and z&0xFFFF to get y. This would work for x and y values from 0 to 32767.
  • MJFMJF Posts: 19
    edited 2013-09-20 15:50
    Thanks Mike,

    Don't completely understand , but I will give it a try.
  • jazzedjazzed Posts: 11,803
    edited 2013-09-20 16:09
    A C function can:

    1. Return a pointer which could be a pointer to a struct with multiple values.
    2. Take a struct parameter and fill multiple values in that.
    3. Take multiple pointer parameters and fill values for them.
    4. Variations of 1 through 3.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-09-20 16:23
    Another option would be to use a couple global variables. (I'm not sure if that's what jazzed #2 does.)

    I haven't used C much lately, but I thought this was something C and Spin had in common.
  • SRLMSRLM Posts: 5,045
    edited 2013-09-20 20:28
    A google search for "C return multiple values" has some good results. From the top of the list: http://stackoverflow.com/questions/2620146/how-do-i-return-multiple-values-from-a-function-in-c

    Since Propeller GCC is just C targeted to the Propeller all of the generic C aspects translate. This is one such case.
  • David BetzDavid Betz Posts: 14,516
    edited 2013-09-21 07:25
    jazzed wrote: »
    A C function can:

    1. Return a pointer which could be a pointer to a struct with multiple values.
    2. Take a struct parameter and fill multiple values in that.
    3. Take multiple pointer parameters and fill values for them.
    4. Variations of 1 through 3.
    You can also return a structure that contains the two values.

    This works fine:
    #include <stdio.h>
    
    typedef struct {
       int value1;
       int value2;
    } ValuePair;
    
    ValuePair foo(int a, int b)
    {
        ValuePair result;
    
        result.value1 = a;
        result.value2 = b;
    
        return result;
    }
    
    int main(void)
    {
        ValuePair x;
    
        x = foo(1, 2);
    
        printf("value1 %d\n", x.value1);
        printf("value2 %d\n", x.value2);
    
        return 0;
    }
    
    I wouldn't recommend using this technique to return large structures but it is certainly okay for small ones like in this example.
  • photomankcphotomankc Posts: 943
    edited 2013-09-27 07:54
    David Betz wrote: »
    You can also return a structure that contains the two values.
    ....
    I wouldn't recommend using this technique to return large structures but it is certainly okay for small ones like in this example.


    That would be my suggestion. Anytime I need to pass around multiple related values a structure pops straight to mind. Mike's packing method will work too and is compact but the structure is more clear in my opinion.
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-09-27 08:05
    The easiest method would be to use jazzed's option #3 -- just pass pointers to two variables that will be filled in by the routine. Here's an example of option #3:
    #include <stdio.h>
    
    void TestSub(int parm, int *retval1, int *retval2)
    {
        *retval1 = parm + 1234;
        *retval2 = parm + 5678;
    }
    
    int main(void)
    {
        int a, b;
    
        TestSub(0, &a, &b);
        printf("a = %d, b = %d\n", a, b);
        return 0;
    }
    
Sign In or Register to comment.