Return Array From a Function in Objective-C
Last Updated :
26 Apr, 2025
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
#import <Foundation/Foundation.h>
int *returnArray( int *a)
{
a[0] = 6;
return a;
}
int main()
{
int *p;
int i;
int a[5] = {1,2,3,4,5};
p = returnArray(a);
for (i = 0; i < 5; i = i + 1)
{
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
#import <Foundation/Foundation.h>
int *returnArray()
{
static int a[3] = {1, 2, 3};
return a;
}
int main()
{
int *p;
int i;
p = returnArray();
for (i = 0; i < 3; i = i + 1)
{
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
#import <Foundation/Foundation.h>
int a[4] = {1, 2, 3, 4};
int *returnArray()
{
a[0] = 10;
return a;
}
int main()
{
int *p;
int i;
p = returnArray();
for (i = 0; i < 4; i = i + 1)
{
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.
Similar Reads
Return Pointer From Functions in Objective-C
A pointer is like a variable or constant that stores the address of another variable. It must be declared before the address value of a variable is to be stored in it. The main advantage of using a pointer is that it frees up the program memory and provides direct access to memory locations. It also
5 min read
C++ Return 2D Array From Function
An array is the collection of similar data-type stored in continuous memory. And when we are storing an array inside an array it is called 2 D array or 2-dimensional array. To know more about arrays refer to the article Array in C++. When there is a need to return a 2D array from a function it is al
4 min read
Function Call by Reference in Objective-C
Just like other languages in objective-C also a function is used to perform some specific task. In objective-C, we can call a function by value or by reference. So, in this article, we will talk about the call by reference in Objective-C. The call-by-reference is a process of passing arguments to a
3 min read
How to return a json object from a Python function?
The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json and this module can be used to convert a python dictionar
2 min read
Function Call by Value in Objective-C
In Objective-C, "call by value" refers to the practice of passing a copy of a value, rather than a reference to the original value, as an argument to a function or method. This means that any changes made to the value within the function or method do not affect the original value outside of the func
3 min read
Passing Arrays as Function Arguments in Objective-C
Every programming language has a data structure that is used to store elements. Similarly, Objective-C also supports data structure which is known as an array. An array is used to store elements of the same data types in a fixed-size sequential collection. In an array, you are not allowed to store d
5 min read
Functions in Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs), Cocoa and
8 min read
How a Function Returns an Object in JavaScript ?
JavaScript Functions are versatile constructs that can return various values, including objects. Returning objects from functions is a common practice, especially when you want to encapsulate data and behavior into a single entity. In this article, we will see how a function returns an object in Jav
3 min read
Passing Structures as Function Arguments in Objective-C
Objective-C is a high-level programming language that is commonly used for developing applications for Apple's Mac OS X and iOS operating systems. It is an object-oriented language that is designed to be easy to use and read, and it provides a powerful set of tools for building complex applications.
6 min read
Arrays in Objective-C
An Array is a data structure that holds similar types of data in contiguous memory. The Objective-C array is a single variable array that is used to store different types of elements in the array. Also, we can access the objective-c array using an index. It is quite similar to the C programming lang
5 min read