views:

353

answers:

4

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!

+1  A: 

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);
ThePosey
Ahh, I think I may have gotten it. Thank you for the wonderful example. Let me test this out a little more first, but again, thank you.
Wayfarer
Hey, I'm trying to do the same thing, except instead of passing integers, I want to pass a string into the function. The function will change the string, and then return the new string. I'm wondering how I can do this with strings...
Wayfarer
Strings get more complicated because a) they're objects in Objective-C, not value types like int or struct, b) they're always pointers, c) they're normally immutable (though there is a mutable string type). Tackle pointers first, then read http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html
Sixten Otto
+7  A: 

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.

Sixten Otto
I'm a little confused as to how you would use the reference parameters. I understand how it works in the declaration, but how would you "return" the values (do you even use "return") and how would you set the variables again?Or are the arguments automatically updated? I'm a little confused...
Wayfarer
+1  A: 

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.

caf
Objective-C still uses plenty of basic C structs for value types like this, where defining a class is kind of overkill. For instance: CGPoint, CGSize, CGRect....
Sixten Otto
Or, return the pointer to the struct you malloced in your function...but dont forget to free that memory when is will never be used
Macroideal
+1  A: 

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"];
Kenny Winker