Call by Value & Call by Reference using Function in C++


Example Program : 1

This is a C++ program that demonstrates the concept of pass by value. The program initializes a variable data to 3, prints its initial value to the console, passes it to a function called change(), and prints its value again after the function call. The change() function takes an integer parameter called data. Inside the function, the value of data is changed to 5 and its new value is printed to the console.

In the main() function, the value of data is printed to the console before the function call. However, when the change() function is called, a copy of the data variable is passed to it. Therefore, any changes made to the data variable inside the change() function will not affect the original data variable in the main() function. As a result, after the change() function call, the value of data in the main() function remains unchanged at 3, and the program prints the same value again to the console.

Source Code

#include <iostream>
using namespace std;
void change(int data);
int main()
{
    int data=3;
    cout<<"Value of the data is: "<<data<<endl;
    change(data);
    return 0;
}
void change(int data)
{
    data=5;
    cout<<"After changing :"<<endl;
    cout<<"Value of the data is: "<<data<<endl;
}
To download raw file Click Here

Output

Value of the data is: 3
After changing :
Value of the data is: 5


Example Program : 2

This is a C++ program that demonstrates how to swap two variables using pointers. The program defines a function called swap() that takes two integer pointers as parameters, swaps the values they point to, and returns nothing. In the main() function, two integer variables x and y are initialized to 500 and 100 respectively, and their initial values are printed to the console using cout.

The swap() function is then called with the addresses of x and y passed as arguments using the address-of operator &. Inside the swap() function, the values of x and y are swapped using a temporary variable swap.

After the swap() function call, the new values of x and y are printed to the console using cout. As a result of the swap, the value of x is now 100 and the value of y is now 500. Using pointers to swap values is a useful technique in programming as it allows you to modify the values of variables outside of the function where they were originally defined.

Source Code

#include<iostream>
using namespace std;
void swap(int *x, int *y)
{
    int swap;
    swap=*x;
    *x=*y;
    *y=swap;
}
int main()
{
    int x=500, y=100;
    cout<<"\nValue of x is: "<<x<<endl;
    cout<<"\nValue of y is: "<<y<<endl;
    cout<<"\nAfter swapping :";
    swap(&x, &y);
    cout<<"\nValue of x is: "<<x<<endl;
    cout<<"\nValue of y is: "<<y<<endl;
    return 0;
}
To download raw file Click Here

Output

Value of x is: 500

Value of y is: 100

After swapping :
Value of x is: 100

Value of y is: 500

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples