Skip to main content

Call by Value and Call by Reference in C++ Function

2 min read Updated June 30, 2026
Share:
On this page (11sections)

Call By Value

Syntax

return_type function_name( param_1,param_2 ... param_n );

For example,

int swap(int x,int y)

Call By Value Example Program in C++

//Simple Call By Value Function Example Program in C++
//Function Example

#include<iostream>
#include<conio.h>

using namespace std;

void swap(int x, int y);

int main() {
   int a = 200, b = 100;
   cout << "Simple Call By Value Function Example Program\n";
   swap(a,b);
   cout << "\nValues (Main  )  a :"<<a<<" b:"<<b;

   getch();
}

// Call By Value Function
void swap(int x, int y) {
  int t;
  t = x;
  x = y;
  y = t;
  cout << "\nValues (Function) x :"<<x<<" y:"<<y;
}

Sample Output

Simple Call By Value Function Example Program

Values (Function) x :100 y:200
Values (Main  )  a :200 b:100

Call By Reference

Syntax

return_type function_name( *param_1,*param_2 ... param_n );

For example,

int swap(int *x,int *y)

Call By Reference Example Program in C++

//Simple Call By Reference Function Example Program in C++
//Function Example

#include<iostream>
#include<conio.h>

using namespace std;

void swap(int *x, int *y);

int main() {
   int a = 200, b = 100;
   cout << "Simple Call By Reference Function Example Program\n";
   swap(&a,&b);
   cout << "\nValues (Main  )  a :"<<a<<" b:"<<b;

   getch();
}

// Call By Reference Function
void swap(int *x, int *y) {
  int t;
  t = *x;
  *x = *y;
  *y = t;
  cout << "\nValues (Function) x :"<<*x<<" y:"<<*y;
}

Sample Output

Simple Call By Reference Function Example Program

Values (Function) x :100 y:200
Values (Main  )  a :100 b:200

How It Works

This C++ program demonstrates Call by Value and Call by Reference in Function. It first prepares the data it needs, then performs the core operation step by step, and finally prints the output shown in the Sample Output above.

  1. Declare the variables that hold the program’s data.
  2. Print the final result to the console so you can compare it with the sample output.

Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.

Continue learning with these related tutorials and programs:

Frequently Asked Questions

What does this C++ program do?
It is a C++ example program that demonstrates Call by Value and Call by Reference in Function, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses user-defined functions, illustrating a common pattern in C++ programming.

Related Tutorials

Search tutorials