Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function with multiple arguments

how to pass multiple arguments in a single function in Objective-C? I want to pass 2 integer values and the return value is also integer. I want to use the new Objective-C syntax, not the old C/C++ syntax.

like image 835
V.V Avatar asked Apr 01 '10 12:04

V.V


People also ask

Can a function take multiple arguments?

Functions can accept more than one argument. When calling a function, you're able to pass multiple arguments to the function; each argument gets stored in a separate parameter and used as a discrete variable within the function.

How do you make multiple arguments in Python?

In Python, by adding * and ** (one or two asterisks) to the head of parameter names in the function definition, you can specify an arbitrary number of arguments (variable-length arguments) when calling the function.

How many arguments 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.

Which inbuilt function takes multiple arguments?

Inside the function, the values that are passed get assigned to variables called parameters. Another built-in function that takes more than one argument is max.


2 Answers

In objective-c it is really super easy. Here is the way you would do it in C:

int functName(int arg1, int arg2) 
{
    // Do something crazy!
    return someInt;
}

This still works in objective-c because of it's compatibility with C, but the objective-c way to do it is:

// Somewhere in your method declarations:
- (int)methodName:(int)arg1 withArg2:(int)arg2
{
    // Do something crazy!
    return someInt;
}

// To pass those arguments to the method in your program somewhere:
[objectWithOurMethod methodName:int1 withArg2:int2];

Best of luck!

like image 188
Jay Avatar answered Sep 20 '22 13:09

Jay


Since this is still google-able and there are better solutions than the accepted answer; there's no need for the hideous withArg2 – just use colons:

Declaration:

@interface
-(void) setValues: (int)v1 : (int)v2;

Definition:

@implementation
-(void) setValues: (int)v1 : (int)v2 {
    //do something with v1 and v2
}
like image 35
ellmo Avatar answered Sep 21 '22 13:09

ellmo