Pages

Tuesday, October 3, 2017

Easy tutorial for C / C ++ - Call by value and call by reference

Easy tutorial for C / C ++ - Call by value and call by reference

- Let's look at how to pass data to a function when calling functions. Typically, there are call by value, call by reference, and call by address.

Call by value :

- When calling a function in the way we used to before, it is not passing the variable itself, but only passing the value stored in the variable.

- Let's look at a simple example.

Example Code

#include <iostream>
using namespace std;

void Call_V(int);

int main() {
     int a = 5;
     Call_V(a);

     cout <<" In main function  : "<< "  a  = " << a << ", address of a = "<< &a << endl;
     return 0;
}

void Call_V(int a){
     a += 1;
     cout <<" Call by value     : "<< "  a  = " << a << ", address of a = "<< &a << endl;
}

- I declared Call_V function with one integer argument and without return value. This function adds 1 to the integer argument passed (a + = 1;) and assigns it again. Then we will print the value of 'a' and the address value.

- I declared integer variable 'a' in main function and substituted 5. And I gave 'a' as a parameter of Call_V function. Next, we print out the value of 'a' of the main function and the address value.

results :

Call by value     :   a  = 6, address of a = 0x7fffb09de18c
In main function  :   a  = 5, address of a = 0x7fffb09de1bc

- As a result of Call_V function, "Call by value: a = 6" is printed. We take the value 5 of the variable 'a' of the main function and assign 5 to the variable 'a' of the Call_V function. As a result, the value of 6 is printed. The address value of the variable 'a' of the Call_V function is printed  as "0x7fffb09de18c".

- The value of variable 'a' output from main function is 5. That is, the variable 'a' of the main function and the variable 'a' of the Call_V function are different independent variables. Also, if you check the address values of the two variables, you can see the difference.

Call by reference :

- This is the method used when you want to use the same variable as another name (alias). The usage is as follows.

usage : 

int a = 1;
int &b = a;

- The integer variable a is declared and is substituted to 1. Declare the reference variable b (& b) and specify a previously declared a. In the future it means that we will also use a as b.

- Let's look at a simple example.

Example Code

#include <iostream>
using namespace std;

void Call_R(int &);

int main() {

     int a ;
     a = 4;

     Call_R(a);
     cout <<" In main function  : "<< "  a  = " << a << ", address of a = "<< &a << endl;
     return 0;
}

void Call_R(int &b){
     b += 1;
     cout <<" Call by reference : "<< "  b  = " << b << ", address of b = "<< &b << endl;
}

- Call_R (int & b) In the function definition part, & b is specified as the argument type to be passed. In other words, we will use the parameter to be passed as a different name. Let's check the results.

results :

Call by reference :   b  = 5, address of b = 0x7fff49144fbc
In main function  :   a  = 5, address of a = 0x7fff49144fbc

- You can see that both 'b' of Call_R function and 'a' of main function have 5 values. You can also see that the address values are the same.

- In other words, in the Call_R function, the variable 'a' of the main function is used as the name of the variable 'b'.

No comments:

Post a Comment