3_Passing and returning arrays with function 

Passing array

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 ways

  • Formal parameters as a pointer
- (void) myFunction(int *) param{}
  • Formal parameters as a sized array.

– (void) myFunction(int [10]) param{}

  • Formal parameters as an unsized array

– (void) myFunction(int []) param{}

Return array

Note : Objective-C programming language does not allow to return an entire array as an argument to a function.

However, you can return a pointer to an array by specifying the array’s name without an index.

If you want to return a single-dimensional array from a function, you would have to declare a function returning a pointer

int * myFunction(){}

Second point to remember is that Objective-C does not advocate to return the address of a local variable to outside of the function so you would have to define the local variable as static variable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: