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
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
C++ Function Call By Value
Next article icon

C++ Function Call By Value

Last Updated : 15 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 section of code that executes only when it is called, to put it simply.

Function in C++
Function in C++

Call by Value

When a function is called, new elements are created on the stack memory to store the essential information about the functions as well as allocated memory space for parameters and the return value.

The call-by-value method allows you to copy the actual parameter to a formal parameter. In this case, if we change the formal parameter then the actual parameter doesn't change.

In other words, the value of the parameter is duplicated into the memory location designated for the function's parameter. Consequently, two memory locations now hold the same value. C++ uses call by value method by default.

Upon exiting the function, the memory on the program stack is 'popped,' leading to the removal of all data associated with the function call. This includes the memory location allocated for the parameters used within the function. Consequently, any alterations made to values within the function's scope do not impact the values of variables outside the function.

Use of Call by Value

Call by value is used in certain conditions like:

  • When you do not want to change the actual parameters of the function.
  • When you want to make copies of the data instead of the actual data.
  • When space is not an issue.
  • Usually, when you do not deal with recursion or backtracking.

Limitations of using Call by Value

Although call by value is so useful for us while programming in C++, it has a few limitations too:

  • Data passed is then stored in temporary memory
  • Can't operate over the actual data rather need to work on temporary copy made of the data, because of which changes made in the data in the function is not reflected in main function.
  • Memory Space required while using string ,array , vector, etc can be huge.
  • Tackling Backtracking and recursion can be complex using call by values.

Example 1:

C++
// C++ Program to implement // Swapping using Call by function #include <iostream> using namespace std;  // Swap function to demonstrate // call by value method void swap(int x, int y) {     int t = x;     x = y;     y = t;     cout << "After Swapping in function x: " << x          << ", y: " << y << endl; }  // Driver Code int main() {     int x = 1, y = 2;      cout << "Before Swapping: ";     cout << "x: " << x << ", y: " << y << endl;      swap(x, y);      cout << "After Swapping: ";     cout << "x: " << x << ", y: " << y << endl;      return 0; } 

Output
Before Swapping:  x: 1, y: 2 After Swapping in function x:2, y:1 After Swapping:  x: 1, y: 2

As, you can see we pass two values in the swap functions x and y i.e., 1 and 2 before and after swapping them through a call-by-value method, if, we print the value of x and y then it is not changed coz the function swap create copies of x and y and swap them and their scope is inside that function only and the actual x and y values have not been modified by the swap function that why it is called call by value. If you want that the changes also reflect in the main function then you have to call it by reference and for that, you have to append the '&' symbol before x and y in the formal argument.

Example 2:

C++
// C++ Program to demonstrate // Call by value #include <iostream> using namespace std;  // Function to change value of x int fun(int x) {     x += 1;     return x; }  // Driver Code int main() {     int x = 3;      cout << "Value of X before modifying: " << x << endl;      x = fun(x);     cout << "Value of X after modifying: " << x << endl;      return 0; } 

Output
Value of X before modifying: 3 Value of X after modifying: 4

Time complexity: O(1).
Space complexity: O(1).

In the above example, if you want to change the value of a variable through a function using call by value then it is only changed in that function only and the actual value of the variable where it is defined is not changed. But, if we want to use the value from the function return the value to the main function.


Next Article
C++ Function Call By Value

A

akashjha2671
Improve
Article Tags :
  • Technical Scripter
  • C++
  • Technical Scripter 2022
  • CPP-Functions
Practice Tags :
  • CPP

Similar Reads

    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
    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
    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
    atexit() function in C/C++
    The function pointed by atexit() is automatically called without arguments when the program terminates normally. In case more than one function has been specified by different calls to the atexit() function, all are executed in the order of a stack (i.e. the last function specified is the first to b
    3 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