简体   繁体   中英

Returning Multiple Values from a Function

Hey, so I'm making an iPhone app and in there is a common function that needs to be called. So I put it in it's own file and set it up, passing the parameters to that and all that. However, I'm not sure how best to return the values from the function. I read up trying to return the values in an array, but I'm not sure how to do it.

int EndOfTurn(int varTurns, int varFatness)
    {
        varTurns--;

        if (varTurns <= 0) {
            varFatness = varFatness - 5;
        }
        else {
            varFatness += 2;
        }
}

I need to return both varTurns and varFatness. However, this function will become more complicated, with me returning as many as 10 variables.

As of now, it is written in C, not Objective-C, (I just added a .c file to the Project), but I can change that. It needs to simply return all the updated values that I used in the function. If you could, write up the declaration of the function and the type:

TYPE_HERE EndOfTurn(int varTurns, int varFatness)

so I know exactly how to do it. Thanks, and I hope I gave enough info!

Your options are essentially the same in Objective-C as in conventional C:

  1. Use reference parameters, or
  2. Return some kind of data structure (a struct, a class instance) that encapsulates the collection of values you want to return.

For example:

void EndOfTurn(int* varTurns, int* varFatness) { ... }

or

typedef struct { int turns, int fatness } ReturnType;

ReturnType EndOfTurn(int varTurns, int varFatness) {
  ReturnType foo;
  foo.turns = varTurns-1;

  if (foo.turns <= 0) {
    foo.fatness = varFatness - 5;
  }
  else {
    foo.fatness = varFatness + 2;
  }
  return foo;
}

or

typedef struct { int turns, int fatness } ReturnType;

void EndOfTurn( ReturnType* param ) {
  param->turns--;

  if (param->turns <= 0) {
    param->fatness -= 5;
  }
  else {
    param->fatness += 2;
  }
}

I'd suggest that you find a good tutorial on pointers in C (perhaps this one or this one ?) and take some quality time reading over it. The concepts also apply to Objective-C, and are pretty fundamental to how both languages work. It's a bit beyond the scope of a Stack Overflow answer, and you'll really need to be comfortable with them.

Personally, I would do it the objective-c way, and pass an NSDictionary out of the function. Something like:

(NSDictionary *)EndOfTurn:(int)varTurns withFatness:(int)varFatness

    {
        varTurns--;

        if (varTurns <= 0) {
                varFatness = varFatness - 5;
        }
        else {
                varFatness += 2;
        }

    return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:varFatness],@"FATNESS", [NSNumber numberWithInt:varTurns],@"TURNS",nil];

}

It feels good as long as you keep your keys consistent. You access your returned dictionary like so:

int varTurns = [returnedDict objectForKey:@"TURNS"];

if you want an example in C you pass pointers to those two variables are parameters so your function signature would look something like this:

void EndOfTurn(int* varTurns, int* varFatness);

Then when modifying the values of those you just dereference them:

*varTurns = *varTurns - 5;

or whatever you need to do.

The original function call would look like this:

int otherFunctionVarTurns;
int otherFunctionVarFatness;

...

EndOfTurns(&otherFunctionVarTurns, &otherFunctionVarFatness);

Since you've added a C tag, here's how you could do it in plain C (though this may not be the most idiomatic way to do it in Objective-C):

struct turn_state {
    int varTurns;
    int varFatness;
};

void EndOfTurn(struct turn_state *state)
{
        state->varTurns--;

        if (state->varTurns <= 0) {
                state->varFatness -= 5;
        } else {
                state->varFatness += 2;
        }
}

Use a struct turn_state variable to store the current state, and call it like so:

struct turn_state current_state = { /* initial values */ };

/* ...code... */

EndOfTurn(&current_state);

Adding more variables is simple - just add them to the struct turn_state .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM