Site icon TestingDocs.com

Call-by-Value and Call-by-Reference Example

Overview

In this post, we will see how to pass variables to a function in the C++ program, using call-by-value and call-by-reference methods.

Call-by-value

In this method, we pass the values of the actual parameters to the function of formal parameters. So any changes made to the function parameters in the function body do not reflect the actual parameters of the caller function. Let’s understand using a small program.

Code Listing

#include <iostream>

using namespace std;

//Function prototype
void add(int x,int y);


/***************************************************
*  function
***************************************************/
void add(int x, int y)
{
    cout << "add() |---------------------" << endl;
    cout << "x = :" << x << endl;
    cout << "y = :" << y << endl;
    cout << "x + y = :" << x + y << endl;
    cout << "add() |Changing values of x and y ---" << endl;
    x=30;
    y=40;

    cout << "x = :" << x << endl;
    cout << "y = :" << y << endl;
    cout << "x + y = :" << x + y << endl;
}


/***************************************************
* main function
***************************************************/
int main()
{
    int a= 10;
    int b= 20;
    cout << "main() | Before Function Call:--" << endl;
    cout << "a = :" << a << endl;
    cout << "b = :" << b << endl;

    // Passing a and b to function call
    add(a,b);
    cout << "main() | After Function Call add():--" << endl;
    cout << "a = :" << a << endl;
    cout << "b = :" << b << endl;

    return 0;
}

Output

main() | Before Function Call:–

a = :10

b = :20

add() |———————

x = :10

y = :20

x + y = :30

add() |Changing values of x and y —

x = :30

y = :40

x + y = :70

main() | After Function Call add():–

a = :10

b = :20

 

 

You can see that the values of a and b that we passed in the main to the add function are not modified even though we modified the values of x and y in the add function. In the next section, we will another variation of passing the address of the parameters to the function which is called call-by-reference.

Call-by-reference

#include <iostream>
using namespace std;

//Function prototype
void add(int *x,int *y);
/***************************************************
* function
***************************************************/
void add(int *x, int *y){
cout << "add() |---------------------" << endl;
cout << "x = :" << x << endl;
cout << "y = :" << y << endl;
cout << "add() |Changing values of x and y ---" << endl;
*x=30;
*y=40;
cout << "Address of x = :" << x << endl;
cout << "Address of y = :" << y << endl;
cout << "Value of x = :" << *x << endl;
cout << "Value of y = :" << *y << endl;
}

/**************************************************** 
main function
***************************************************/
int main(){
int a= 10;
int b= 20;
cout << "main() | Before Function Call:--" << endl;
cout << "Address of a = :" << &a << endl;
cout << "Address of b = :" << &b << endl;
cout << "a = :" << a << endl;
cout << "b = :" << b << endl;

// Passing a and b to function call
add(&a,&b);cout << "main() | After Function Call add():--" << endl;
cout << "Address of a = :" << &a << endl;
cout << "Address of b = :" << &b << endl;
cout << "a = :" << a << endl;
cout << "b = :" << b << endl;
return 0;
}

    Output:

main() | Before Function Call:–

Address of a = :0x6dfeec

Address of b = :0x6dfee8

a = :10

b = :20

add() |———————

x = :0x6dfeec

y = :0x6dfee8

add() |Changing values of x and y —

Address of x = :0x6dfeec

Address of y = :0x6dfee8

Value of x = :30

Value of y = :40

main() | After Function Call add():–

Address of a = :0x6dfeec

Address of b = :0x6dfee8

a = :30

b = :40  

 

  In this case, we have passed the address of variables a and b in the main function to the add function. The add function modified the values preset at the address of the variables. In the output, you see that the values of and b are modified after invoking the add function.

—-

The IDE used in the tutorial is Code:: Blocks. To download and install Code Blocks follow the link: https://www.testingdocs.com/download-and-install-codeblocks/

For more information on Code Blocks IDE, visit the official website of Code blocks IDE: http://www.codeblocks.org/

Exit mobile version