Open In App

Passing Arrays as Function Arguments in Objective-C

Last Updated : 04 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 different types of elements in the same array means you can not store integers in the array of string types. This first element of the array is stored at the lowest address is 0 and the last element is stored at the highest address. In Objective-C, we are allowed to pass an array to the function. To pass an array(either single or multi-dimensional) we have to declare a formal parameter in the function. So to declare a formal parameter in the function we can use one of the following methods.

Method 1

To pass an array to the function we can declare a formal parameter as a pointer.

Syntax:

-(void) FunctionName(int*)parameterName{

// Body of the function

}

Example:

ObjectiveC




// Objective-C program to pass an array as a parameter in the function
#import <Foundation/Foundation.h>
  
// Creating an interface
@interface GFG:NSObject
  
// Function declaration 
-(int) StudentMarks:(int *)ExameMarks arrSize: (int) s;
  
@end
  
// implemeneting the interface
@implementation GFG
  
// Creating a function which takes array and its size as a argument
// Here we use pointer to pass array
-(int)StudentMarks:(int *)ExameMarks arrSize: (int) s{
  
    // Creating variables
    int result = 0;
    int i;
      
    // Finding the total sum of the marks
    for(i = 0; i < s; i++){
        result += ExameMarks[i];
    }
    return result;
}
  
@end
  
// Driver code
int main ()
{
  
    // Creating array of integer type
    int subjectmarks[6] = {45, 78, 56, 87, 56, 66};
    double totalMarks;
      
    // Creating object
    GFG *obj = [[GFG alloc]init];
      
    // Calling the function with array and size as a argument
    totalMarks = [obj StudentMarks:subjectmarks arrSize: 6];
      
    // Displaying the result
    NSLog(@"Total Marks: %f", totalMarks);
      
    return 0;
}


Output:

Total Marks: 388.000000

Explanation: In the above code, we find the total sum of the given marks. So to do this, first we declare a function named as StudentMarks in the GFG interface. This function takes an array as an argument, here the parameter is passed as a pointer. Then we implement the function and this function returns the total sum by adding all the marks of the given subjects (Sum = 45+78+56+87+56+66). Now in the main function, we call the StudentMarks function by passing the array named as subject marks in it and display the final result.

Method 2

To pass an array to the function we can declare a formal parameter as an array with the size of the array.

Syntax:

-(void) FunctionName(int[arraySize])parameterName{

// Body of the function

}

Example:

ObjectiveC




// Objective-C program to pass an array as a parameter in the function
#import <Foundation/Foundation.h>
  
// Creating an interface
@interface GFG:NSObject
  
// Function declaration 
-(int) StudentMarks:(int [6])ExameMarks;
  
@end
  
// implemeneting the interface
@implementation GFG
  
// Creating a function which takes array as a argument
-(int)StudentMarks:(int[5])ExameMarks{
  
    // Creating variables
    int result = 0;
    int i;
      
    // Finding the total sum of the marks
    for(i = 0; i < 6; i++){
        result += ExameMarks[i];
    }
    return result;
}
  
@end
  
// Driver code
int main ()
{
  
    // Creating array of integer type
    int subjectmarks[6] = {45, 78, 56, 87, 56, 66};
    double totalMarks;
      
    // Creating object
    GFG *obj = [[GFG alloc]init];
      
    // Calling the function with array as a argument
    totalMarks = [obj StudentMarks:subjectmarks];
      
    // Displaying the result
    NSLog(@"Total Marks: %f", totalMarks);
      
    return 0;
}


Output:

Total Marks: 388.000000

Explanation: In the above example, we are going to find the total sum of the given marks. So to do this, first we declare a function named as StudentMarks in the GFG interface. This function takes array as an argument. Then we implement the function and this function returns the total sum by adding all the marks of the given subjects (Sum = 45+78+56+87+56+66). Now in the main function, we call the StudentMarks function by passing the array named as subject marks in it and display the final result.

Method 3

To pass an array to the function we can declare a formal parameter as an array without the size.

Syntax:

-(void) FunctionName(int[])parameterName{

// Body of the function

}

Example:

C++




// Objective-C program to pass an array as a parameter in the function
#import <Foundation/Foundation.h>
  
// Creating an interface
@interface GFG:NSObject
  
// Function declaration 
-(int) StudentMarks:(int [])ExameMarks arrSize: (int) s;
  
@end
  
// implemeneting the interface
@implementation GFG
  
// Creating a function which takes array without size as a argument
-(int)StudentMarks:(int[])ExameMarks arrSize: (int) s{
  
    // Creating variables
    int result = 0;
    int i;
      
    // Finding the total sum of the marks
    for(i = 0; i < s; i++){
        result += ExameMarks[i];
    }
    return result;
}
  
@end
  
// Driver code
int main ()
{
  
    // Creating array of integer type
    int subjectmarks[6] = {45, 78, 56, 87, 56, 66};
    double totalMarks;
      
    // Creating object
    GFG *obj = [[GFG alloc]init];
      
    // Calling the function with array as a argument
    totalMarks = [obj StudentMarks:subjectmarks arrSize: 6];
      
    // Displaying the result
    NSLog(@"Total Marks: %f", totalMarks);
      
    return 0;
}


Output:

Total Marks: 388.000000

Explanation: In the above code, we are going to find the total sum of the given marks. So to do this, first we declare a function named as StudentMarks in the GFG interface. This function takes an array without size as an argument. Then we implement the function and this function returns the total sum by adding all the marks of the given subjects (Sum = 45+78+56+87+56+66). Now in the main function, we call the StudentMarks function by passing the array named as subject marks in it along with another argument that is arrSize. And display the final result which is 388.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads