Open In App

Function Call by Value in Objective-C

Last Updated : 16 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 function or method.

In Objective-C, the type of an argument is specified in the function or method declaration, and this determines whether the argument is passed by value or by reference. For example, consider the following function declaration:

– (void)doSomethingWithInt:(int)x;

This function takes an integer argument, which is passed by value. This means that any changes made to the value of x within the function do not affect the original value of the argument outside of the function.

On the other hand, if we wanted to pass a reference to an integer rather than a copy of the integer, we could use a pointer:

– (void)doSomethingWithIntPointer:(int *)x;

Now, the function takes a pointer to an integer as an argument, and any changes made to the value of x within the function will be reflected in the original value outside of the function.

It’s important to note that not all types in Objective-C can be passed by reference. For example, primitive types like int and float are passed by value, while objects are passed by reference. This is because objects in Objective-C are implemented as pointers, so when an object is passed as an argument, it is actually a pointer to the object that is being passed.

In summary, call by value in Objective-C refers to the practice of passing a copy of a value as an argument to a function or method, rather than a reference to the original value. This means that any changes made to the value within the function or method do not affect the original value outside of the function or method. Whether an argument is passed by value or by reference depends on its type, with primitive types being passed by value and objects being passed by reference.

Example 1: 

ObjectiveC




// Objective-C program for call by value
#import <Foundation/Foundation.h>
  
@interface MyClass : NSObject
  
// Method to swap two numbers using call by value
- (void)swapNumbers:(int)a andNumber:(int)b;
  
@end
  
@implementation MyClass
  
// Method implementation
// Here we swap the value of M and N
- (void)swapNumbers:(int)M andNumber:(int)N {
    int temp = M;
    M = N;
    N = temp;
}
  
@end
  
int main(int argc, const char * argv[]) {
  
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    MyClass *myClass = [[MyClass alloc] init];
    int num1 = 5;
    int num2 = 10;
      
    // Calling the method which takes two integer
    // arguments and swaps the values inside the method
    [myClass swapNumbers:num1 andNumber:num2];
      
    // Since the arguments are passed by value,
    // the original values of the variables
    // remain unchanged
    NSLog(@"num1: %d, num2: %d", num1, num2); 
    [pool drain];
    return 0;
}


Output:

num1: 5, num2: 10

Example 2: 

ObjectiveC




// Objective-C program for call by value
#import <Foundation/Foundation.h>
  
// Function change the value of the given parameter
void callValue(int x) 
{
    x = x * 2;
}
  
int main(int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int num = 10;
    NSLog(@"Original Number: %d", num);
    callValue(num);
    NSLog(@"Value of number after calling the function: %d", num);
    [pool drain];
    return 0;
      
}


Output:

Original Number: 10
Value of number after calling the function: 10


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads