Open In App

Return Array From a Function in Objective-C

Last Updated : 21 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An array is a form of data structure that has a homogenous collection of data in a fixed size. In a nutshell, an array is a group of variables of the same type. For instance, integers cannot be stored in an array of string types. The lowest address of the first element of the array is 0, while the highest address is reserved for the last element. All the elements from the first to the last can be accessed using its index number ranging from 0 to n-1, where n denotes the size of the array. Like other programming languages, Objective-C also allows to return array from user-defined functions. To return an array (either single or multi-dimensional) from a function, we have to create a function that returns a pointer. So to declare a pointer-returning function, we can use one of the following methods.

Method 1

To return an array from a function, we can pass an array to the function that is to be returned as an argument.

Syntax:

data_type * returnArray (data_type *a) {

// function snippet code

}

Example:

ObjectiveC




// Objective-C program to return array from function
#import <Foundation/Foundation.h>
  
// Function to return a pointer value  
int *returnArray(int *a)
{
  
    // Updating an array value
    a[0] = 6;
      
    // Returning the array  
    return a;  
}
  
/* 
main function declared to call the 
above user-defined function 
*/
int main() 
{  
    // Declaring the pointer in which 
    // value is to be returned
    int *p;
    int i;
    int a[5] = {1,2,3,4,5};  
      
    // Returning the pointer value in the 
    // declared pointer variable
    p = returnArray(a);
      
    for(i = 0; i < 5; i = i + 1)
    {
        // Printing the updated array
        NSLog(@"Value of the *p variable is %d.\n", p[i]);
    }
    return 0;  
}


Output:

Value of the *p variable is 6.

Value of the *p variable is 2.

Value of the *p variable is 3.

Value of the *p variable is 4.

Value of the *p variable is 5.

Explanation: The above code updates the first value of an array and then displays the array elements. We initialize a pointer p and an array of finite elements in the main program. A function is created by passing the given array as an argument which returns an array after its execution. This returned value is assigned to pointer p which stores the result in the form of an updated array. In the user-defined function, we update the first value by assigning a different value to it. The array is returned from the function and by default, the first memory location is stored by the pointer p. This creates an array of pointers where p[i] denotes an array element for an index i.

Method 2

To return an array from a function, we can make use of static arrays. In Objective-C, we cannot return the address of locally declared arrays. That’s why we create static arrays to do the same.

Syntax:

data_type * returnArray () {

// function snippet code

}

Example:

ObjectiveC




// Objective-C program to return array from function
#import <Foundation/Foundation.h>
  
/* Function to return a pointer value */
int *returnArray()
{
    // Creating a static array
    static int a[3] = {1, 2, 3};
      
    // Returning the array  
    return a;  
}
  
/* 
main function declared to call the 
above user-defined function 
*/
int main() 
{
  
    // Declaring the pointer in which 
    // value is to be returned
    int *p;
    int i;
      
    // Returning the pointer value in
    // the declared pointer variable
    p = returnArray();  
      
    for(i = 0; i < 3; i = i + 1)
    {
        // Printing the array
        NSLog(@"Value of the *p variable is %d.\n", p[i]);
    }
    return 0;  
}


Output:

Value of the *p variable is 1.

Value of the *p variable is 2.

Value of the *p variable is 3.

Explanation: The above code creates an initialised array and then returns it with the help of an user-defined function. We initialise a pointer p and assign it the returned value of a user-defined function that takes no parameters. In the function, a static array is created and initialised with some values. This array is returned from the function and the first memory location of the array gets stored inside our pointer p. The array values are printed with the help of this array of pointers.

Method 3

To return an array from a function, we can also make use of global arrays. We can declare a global array at top of our program and then return it from the user-defined function. The array that belongs to the global scope and the function that we are creating to return that array must have same data type.

Syntax:

data_type array_name[array_size] = {};    //global declaration

data_type * returnArray () {

// function snippet code

return array_name;

}

Example:

ObjectiveC




// Objective-C program to return array from function
#import <Foundation/Foundation.h>
  
// Declaring global array
int a[4] = {1, 2, 3, 4};
  
/* Function to return a pointer value  */
int *returnArray()
{
    // Updating the array value
    a[0] = 10;
  
    // Returning the array  
    return a;  
}
  
/* 
main function declared to call the 
above user-defined function 
*/
int main() 
{  
    // Declaring the pointer in which 
    // value is to be returned
    int *p;
    int i;
      
    // Returning the pointer value in
    // the declared pointer variable
    p = returnArray();  
      
    for(i = 0; i < 4; i = i + 1)
    {
        // Printing the updated array
        NSLog(@"Value of the *p variable is %d.\n", p[i]);
    }
    return 0;  
}


Output:

Value of the *p variable is 10.

Value of the *p variable is 2.

Value of the *p variable is 3.

Value of the *p variable is 4.

Explanation: The above code updates an initialised array and returns it with the help of a user-defined function. We declare a pointer p in the main program and assign to it the returned value of the user-defined function. In the function, we update the first value of the globally declared array. This updated array is returned from the function and by default, the first memory location of the array gets stored inside the pointer p. This results in an array of pointers with the help of which we print the array elements.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads