Call By Value and Call By Reference in C++ Function
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
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Inline Function without Class Using C++ Programming
- Simple Stack Program in C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Addition ( Add Two Integers ) Example Program
- Factorial Using Loop Example Program In C++
- Factorial Using Function Example Program In C++
- Simple Program for Read user Input Using cin
- Simple Example Program For Constructor In C++
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Virtual Base Class Using C++ Programming
- Simple Program for Function Template Using C++ Programming
- Simple Program for Exception Handling Divide by zero Using C++ Programming
- Do While Loop Example Program In C++
