Open In App

Function Call by Reference in Objective-C

Last Updated : 20 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 function that copies the address of an argument into the formal parameter. In a function, the address is generally used to access the actual parameter used in the function call. Or we can say that the changes made to the argument will affect the passed argument in the function.

Example

// Define the Variable with value 

int a = 5;

int b = 20 ;

// Calling the Function passing the address of variable as argument

[sampleClass  swap:&a num2:&b];

To pass the value by reference, You need to pass the address of the variable in the function. You need to declare the function parameter to the pointer. In Program One, we will call the function by call by reference and print the value in the console.

Example 1:

In this example, first, we define the function name and formal parameter of the function. Then in the main method, we call the CallByReference Method by passing the address of the variable. And  we will print the values 

ObjectiveC




// Objective-C program to Call By Reference
#import <Foundation/Foundation.h>
 
// Declare Function
@interface SampleClass:NSObject
- (void) CallByReference:(int *)num1 andNum2:(int *)num2;
@end
 
// Define Function
@implementation SampleClass
-(void) CallByReference:(int*)num1 andNum2:(int *)num2{
 
    // Here we print the values
    NSLog(@"Variable a = %d and Variable b = %d", *num1, *num2);
    return;
}
@end
 
// Driver code
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     
    int a = 5, b = 10;
    SampleClass *sampleClass = [[SampleClass alloc]init];
     
    // Calling the function with Call By Reference
    [sampleClass CallByReference:&a andNum2:&b];
          
    return 0;
}


Output:

Variable a = 5 and Variable b = 10

Example 2:

In this program, we swap two numbers using call by Reference. So first, we define the swap method with two formal pointer parameters that can store the address of another variable address. In the main method, we have two variables first variable holds 5, and the second variable holds 10. And we call the swap function passing the variable with values. In the last line, we print the values of the “a” and “b” variable

ObjectiveC




// Objective-C program to swap two numbers using call by reference
#import <Foundation/Foundation.h>
 
// Declare the Function
@interface SampleClass:NSObject
- (void) SwapUsingCallByReference:(int *)num1  andNum2:(int *)num2;
@end
 
// Define the Function to swap two numbers
@implementation SampleClass
-(void) SwapUsingCallByReference:(int*)num1  andNum2:(int *)num2{
       int temp;
       temp = *num1;
       *num1 = *num2;
       *num2 = temp;
       return;
}
@end
 
// Driver code
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int a = 5,b = 10;
     
    SampleClass *sampleClass = [[SampleClass alloc]init];
    [sampleClass SwapUsingCallByReference:&a andNum2:&b];
     
    // After Swapping print the a and b value
    NSLog(@" Swapping Two Numbers a = %d and b = %d", a, b);    
    return 0;
}


Output:

Swapping Two Numbers a = 10 and b = 5


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads