57

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?

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Brian Postow
  • 11,709
  • 17
  • 81
  • 125
  • 1
    Duplicate of http://stackoverflow.com/questions/871796/objective-c-default-argument-value and/or http://stackoverflow.com/questions/561185/optional-arguments-in-objective-c-2-0, I believe. – Michael Myers Mar 12 '10 at 21:39
  • 1
    The first one was less helpful because everyone either answered for method call, or an incorrect statement that you can have two foo functions. The second is all method calls... – Brian Postow Mar 12 '10 at 22:19

4 Answers4

130

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.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • @Kenny, yeah but this doesn't really belong to any class. it really is a stand alone C function... – Brian Postow Mar 12 '10 at 22:20
  • Then as Seva sugest, try Objective-C++. With that, you will be able to mix C++ and Obj-C code, calling one from the other, etc... : ) – Macmade Mar 12 '10 at 22:36
  • I have changed header file extension to `hh`, source file extension to `mm` and In project settings it is set `compile source as according to file type extension` to compile it as `objective-C++`. but still it says `C does not support default arguments`. what is the correct extension for `objective-C++` header files? – Necktwi Apr 06 '17 at 11:13
  • @neckTwi `mm`. However, the error may happen if the `hh` is included by non-ObjC++ files. – kennytm Apr 06 '17 at 11:14
  • I have a C++ function `NormalSmooth` declared in file `nautical.hpp` and defined in `nautical.cpp` which I have renamed to `nautical.hh` and `nautical.mm` respectively. Now I am using the function `NormalSmooth` in `GameViewController.m`. What should I do to avoid the above error? Thank you. – Necktwi Apr 06 '17 at 14:44
  • @neckTwi change GameViewController to `mm` as well. (Consider [create a new question post](http://stackoverflow.com/questions/ask)) – kennytm Apr 06 '17 at 18:01
  • Done. Please look into http://stackoverflow.com/questions/43268483/how-to-add-c-files-to-the-objective-c-c-opengl-es-project thank you – Necktwi Apr 07 '17 at 02:03
  • 2
    God forbid!!! making it Objective-C++ you'll bare the curse of C++ forever. E.g. you cannot deploy your binary to others, because they'll need to compile their own code EXACTLY the same way you did. Same compiler, runtime-libs and settings. C++ has no standard/consistent ABI. The strongest reason OBJ-C is "pure C" - is to have a "compatible-forever" binary, that runs everywhere. You only use Obj-C++ when you need to interface C++ code from Obj-C. Certainly not to save a few simple-to-understand code-lines. If you're all for elegance - do Swift instead (and enjoy same perils as C++) – Motti Shneor Feb 21 '19 at 07:59
7

No, objective-c does not support default parameters. See similar question

Community
  • 1
  • 1
Vladimir
  • 170,431
  • 36
  • 387
  • 313
3

You can write a C function with a variable length argument list. You can use '...' as the data type for one of your function's declared parameters to specify where in the parameter list the variable argument list begins. (That allows you to have one or more required arguments before the start of the list.)

printf() is an example of a function that is written using this facility (known as varargs).

printf(const char *restrict format, ...);

Here, the first argument is required, and then can be followed by zero or more additional arguments.

If you wrote your function this way, it could supply a default value for the missing parameter.

jlehr
  • 15,557
  • 5
  • 43
  • 45
2

For a C function - no. For an Objective C class method - yes, you just do two methods, one of them one parameter short, calling the other method.

Or you can rename your sources to .mm and C functions magically become C++.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • That's an interesting possibility... I'll have to figure out the cost of that in other parts of my integration... – Brian Postow Mar 12 '10 at 22:21
  • Note that this changes global name decoration. You won't be able to call such a function externally from a .m/.c source; if there's a way to declare a function as "extern C++", so to say, I'm not familiar with it. In other words, if you're going ObjC++, you better do this throughout the project. – Seva Alekseyev Mar 14 '10 at 02:00