Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method parameters (void) vs no void declaration (error from compiler)

Tags:

objective-c

Why the compiler gives an error in this case of method declaration -

-(void) someMethod (void);

But approves this -

 -(void) someMethod;

(SomeClass.h)

I've read that it is better to declare (void) in parameters than not declaring, but probalby I miss some point.

like image 587
Ilan Avatar asked Aug 17 '13 10:08

Ilan


People also ask

Is void parameter necessary?

It is also used as a generic pointer (e.g., void* ), although this usage is needed less often in C++ than in C. C++ does not require that void be used to indicate that there are no function parameters, but it is often used in this way for compatibility with C.

What does a void parameter mean in C?

When used as a function return type, the void keyword specifies that the function doesn't return a value. When used for a function's parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is "universal."

What happens if one of the argument names in a function declaration does not match that of the corresponding function definition?

It is an error if the number of arguments in a function definition, declaration, or call does not match the prototype. If the data type of an argument in a function call does not match the corresponding type in the function prototype, the compiler tries to perform conversions.

Is the use of void necessary inside a function definition that does not have any parameters?

If a function takes no parameters, the parameters may be left empty. The compiler will not perform any type checking on function calls in this case. A better approach is to include the keyword "void" within the parentheses, to explicitly state that the function takes no parameters.


1 Answers

You cannot do this for Objective-C.

In Objective-C, every parameter must be after : e.g.

- (void)someMethod:(int)i;
- (void)someMethod:(int)i withString:(NSString *)string;
- (void)someMethod:(int)i :(int)i2 :(int)i3; // you can do this but is bad style

and it does not make sense to make something like

- (void)someMethod:(void)what_goes_here;

so if you want a method without parameter:

- (void)someMethod;

However you can do it in C/C++

void someMethod(void);

And I didn't see any benefit of declare void parameters (explicitly declare things is not always good).

like image 153
Bryan Chen Avatar answered Nov 01 '22 20:11

Bryan Chen