This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Returning multiple values from a function??

I want a function to return multiple values after doing data processing i.e. void test(int a, int b int c) whereas a, b and c have values set and control is returned to the caller with the values sent back.
Try as I may I haven't been able to return values from my function, furthermore I read from someone C doesn't support multiple returns - is this correct??

It seems such a stupid limitation when (supposedly) inferior programming languages like Pascal can support multiple returns in a function / procedure call.

What gives??

Is this just in Keil?? Can I get it to do what I want??

I really want to avoid using global declarations, records or structs if at all possible.

Parents
  • The "traditional" route, for people who don't see limitations but possibilities with the programming language, would probably look like:

    int my_function(my_struct* result) {
        if (!result) {
            return RES_NAUGHTY_TRICKSTER;
        }
    
        result->charlie = 1.6180339887;
        result->benny = 2.7182818;
        result->johnny = 3.14159265;
        return RES_OK;
    }
    


    So the "return" is if the function could perform or not, while the struct gets the result of the computation - but only valid if the function returned an OK.

Reply
  • The "traditional" route, for people who don't see limitations but possibilities with the programming language, would probably look like:

    int my_function(my_struct* result) {
        if (!result) {
            return RES_NAUGHTY_TRICKSTER;
        }
    
        result->charlie = 1.6180339887;
        result->benny = 2.7182818;
        result->johnny = 3.14159265;
        return RES_OK;
    }
    


    So the "return" is if the function could perform or not, while the struct gets the result of the computation - but only valid if the function returned an OK.

Children