C++ 41: Call By Value and Call By Refrence

These concepts are used to check that the function can change the value of the actual arguments or not And what are result behind the result.



Call By Value:

  1. Calling a function by passing the variable or value is called as call by value.
  2. In call by value the copy of the value of the actual arguments is passed as formal argument to the function.
  3. The formal arguments are local variable so they created each time when functipn is called and gtet's destroyed as soon as function is destroyed.
  4. The function performs operation on the formal arguments. That means function cannot perform any operation on actual argument.
  5. Hence, in Call by Value function cannot change the actual argument.

Example:
#include <iostream>
using namespace std;

void square(int z)
{
    z = z * z;
    cout << "z = " << z << endl;
}
int main()
{
    int z = 5;
    cout << "z = " << z << endl;
    square(z);

    cout << "z = " << z << endl;
    return 0;
}

Output:

z = 5
z = 25
z = 5

Call by reference / Call by Address / Pass by pointers

  1. Calling a function by passing the address of the variable is called as pass by reference.
  2. In call by reference the address of the variable is transferred as formal argument to the function.
  3. The address of each variable is unique. That means two variable cannot have same address.
  4. As the function receives address of variable as arguments so, the function can directly access the actual argument and can change value of the actual argument.
  5. Hence in call by reference function can change the value of actual arguments.
Example:

#include <iostream>
using namespace std;

void square(int *p)
{
    (*p) = (*p) * (*p);
    cout << "*p = " << *<< endl;
}
int main()
{
    int z = 5;
    cout << "z = " << z << endl;
    square(&z); //PAssing value as call by refrence

    cout << "z = " << z << endl;
    return 0;
}

Output:

z = 5
*p = 25
z = 25


Pass by Reference

  • If the formal arguments of a function are reference variable then a new storage location is not created for these formal arguments.
  • The reference variables represent the same memory location as the actual arguments passes while calling the function.
  • As the reference represents to the actual arguments hence in pass by refrence the function can change the value of actual arguments. 
Example:
Design a function exchange() which will exchange the values of two variables which are passed as arguments.

#include <iostream>
using namespace std;

void exchange(int *p, int *q)
{
    int t = *p;
    *= *q;
    *= t;
}
int main()
{
    int a = 5b = 7;
    cout << "Before exchange: ";
    cout << "a = " << a;
    cout << "\t b = " << b << endl;
    exchange(&a&b);
    cout << "After exchange: ";
    cout << "a = " << a;
    cout << "\t b = " << b << endl;

    return 0;
}

Output:

Before exchange: a = 5   b = 7
After exchange: a = 7    b = 5


#include <iostream>
using namespace std;

int main()
{
    int a = 5;
    int &b = a;
    int &c = b;
    cout << "Address of a = " << &a << endl;
    cout << "Address of b = " << &b << endl;
    cout << "Address of c = " << &c << endl;

    cout << a << "\t" << b "\t" << c << endl;
    a++;
    b++;
    c++;
    cout << a << "\t" << b "\t" << c << endl;

    return 0;
}

Output:

5       5       5
8       8       8






















Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected