Passing Arrays as Function Arguments in Objective-C

If you want to pass a single-dimensional array as an argument in a function, you would have to declare function formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received. Similar way, you can pass multi-dimensional array as formal parameters.

Way-1

Formal parameters as a pointer as follows. You will study what is pointer in next chapter.

- (void) myFunction(int *) param {
.
.
.
}

Way-2

Formal parameters as a sized array as follows −

- (void) myFunction(int [10] )param {
.
.
.
}

Way-3

Formal parameters as an unsized array as follows −

-(void) myFunction: (int []) param {
.
.
.
}

Example

Now, consider the following function, which will take an array as an argument along with another argument and based on the passed arguments, it will return average of the numbers passed through the array as follows −