Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Problems
  • C
  • C++
  • Java
  • Python
  • JavaScript
  • Data Science
  • Machine Learning
  • Courses
  • Linux
  • DevOps
  • SQL
  • Web Development
  • System Design
  • Aptitude
  • GfG Premium
Open In App
Next Article:
Function Call by Reference in Objective-C
Next article icon

Function Call by Reference in Objective-C

Last Updated : 20 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
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

Next Article
Function Call by Reference in Objective-C

N

neeraj3304
Improve
Article Tags :
  • Objective-C
  • Objective-C-Functions

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences