Function Call by Reference in Objective-C
Last Updated : 20 Dec, 2022
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
Similar Reads
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
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
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
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++ Functions - Pass By Reference In C++, there are different ways to pass data (or variables) to a function, with two common methods being Passing by Value and Passing by Reference. Passing by Reference lets a function modify a variable directly, without creating a copy. The variable and parameter share the same memory location, so
3 min read