Function Call by Value in Objective-C
Last Updated :
24 Apr, 2025
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
Similar Reads
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
C++ Function Call By Value
A function is a collection of statements that accept inputs, carry out certain calculations, and output the results. The concept is to group similar or often performed actions into a function so that we may call the function rather than writing the same code again for various inputs. A function is a
4 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
C++ Function Call By Pointer
Several ways exist in which data (or variables) could be sent as an argument to a function. Two of the common ones are Passing by Value and Passing by Reference. Example: C++ // C++ Program to demonstrate // Pass by value and // Pass by reference #include <iostream> using namespace std; // Pas
3 min read
Return Array From a Function in Objective-C
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 h
6 min read
Type Casting in Objective-C
Objective-C is a programming language that was created in the 1980s and is widely used for developing software for the macOS and iOS platforms. One of Objective-C's key features is its ability to perform typecasting. Type casting enables programmers to convert one data type to another, which is usef
4 min read
Scala | Functions Call-by-Name
In Scala when arguments pass through call-by-value function it compute the passed-in expression's or arguments value once before calling the function . But a call-by-Name function in Scala calls the expression and recompute the passed-in expression's value every time it get accessed inside the funct
3 min read
Data Encapsulation in Objective-C
Encapsulation is an Object-Oriented Programming concept that binds together the data and functions that manipulate the data and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding. Encapsulation in Objective-C is vital for pro
6 min read
Passing Pointers to Functions in Objective-C
Pointers are a crucial aspect of any programming language and passing pointers to functions in Objective-C is no exception. In Objective-C, pointers are used to pass values between functions and to manipulate memory in a more flexible manner. Pointers can be passed as arguments to functions and can
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