Objective-C function with default parameters?

Brian Postow picture Brian Postow · Mar 12, 2010 · Viewed 58.3k times · Source

Possible Duplicates:
Optional arguments in Objective-C 2.0?
Objective-C Default Argument Value

I'm writing a C function in Objective-C. I want a default value for my last parameter.

I've tried:

foo(int a, int b, int c = 0);

but that's C++.

I've also tried:

foo(int a, int b, int c)
{
    ...
}

foo(int a, int b)
{
   foo(a, b, 0);
}

But that's also C++.

Is there a way to do this in Objective-C instead?

Answer

kennytm picture kennytm · Mar 12, 2010

There's no default parameters in ObjC.

You can create 2 methods though:

-(void)fooWithA:(int)a b:(int)b c:(int)c {
  ...
}
-(void)fooWithA:(int)a b:(int)b {
  [self fooWithA:a b:b c:0];
}

For C : there's nothing special added to the C subset by using ObjC. Anything that cannot be done in pure C can't be done by compiling in ObjC either. That means, you can't have default parameters, nor overload a function. Create 2 functions instead.