Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method with multiple input parameters

I understand how to create my own methods that accept input parameters in objective-c but I have never actually created a method with more than one input parameter!

From methods I have used with multiple input parameters each has a name along the lines of

first:second:third:

and look like

- (void)first:(NSString *)fname second:(NSString *)mname third:(NSString *)lname;

my question is when creating your own method with multiple input parameters do you have to create a name like first:second:third or can you just have something like C++ where you have the one name followed by a list of input parameter types followed by the parameter names... if I remember correctly.

fullName:(NSString, NSString, NSString) fname, mname, lname; 
like image 837
C.Johns Avatar asked Jul 05 '11 23:07

C.Johns


People also ask

Can a method accept multiple parameters?

Luckily, you can write functions that take in more than one parameter by defining as many parameters as needed, for example: def function_name(data_1, data_2):

How do you pass multiple parameters in a method?

Parameters and Arguments Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

Can a method have 3 parameters?

There is no standard limit on the number of parameters you can specify in Java, but according to "Code Complete" (see this post) you should limit the amount of parameters to about 7, any more and it will have a negative effect on the readability of your code.

How many input parameters can a function have?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.


2 Answers

No. A method must have the format as you described:

- (void)first:(NSString *)fname second:(NSString *)mname third:(NSString *)lname; 
like image 102
PengOne Avatar answered Sep 16 '22 14:09

PengOne


You have to have the parameters interleaved with the method signature. It's ok because xcode has code completion and it can give you nice descriptive names about what your method is doing and what it requires.

e.g.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

In the example above without even looking at the API for UIViewController you can get a pretty good understanding of how this method works and what it's params are. It is good practice to name your methods well to describe what they do (it can remove the need for most commenting if done well).

You may well of course see a method written like this

- (void)myMethodThatAcceptsARectangle:(float)x :(float)y :(float)w :(float)h; 

But this will not be very clear in use as to what the parameters relate to:

[self myMethodThatAcceptsARectangle:1.0f :1.0f :1.0f :1.0f]; 

So you should avoid this (I added it incase you ever see this and wonder what's happening).

like image 25
Paul.s Avatar answered Sep 19 '22 14:09

Paul.s