Call by value and call by reference in C++ programming language

 Call by value and call by reference in C++ programming language

Today we are going to talk about functions in C++ programming language. We will cover the concept of call by value and call by reference in C++ program. Before diving deep into this topic we will look at little introduction of functions in C++ program.

By now we know that functions are one of the most important aspect of any programming language and the same is true with respect to C++ program. They are very important building block of C++ program. With the help of functions programmers and developers can divide a program into smaller and manageable parts.

With in functions there is a concept of call by value and call by reference. Now we will understand and have a look on this important concept regarding functions.

In functions we have studied about that how we can call a function in the main() method. In C++ we use arguments and parameters to send and receive data during function call respectively. We use arguments to pass data to a function. There are two ways to pass data with arguments i.e, 

1) Call by value

2) Call by reference

These methods decide how data can be passed to a function.


Basic syntax  of call by value and call by reference in C++ program



In the above coding example we are looking at the syntax of call by value. We have created a function named as change() with function type as void like this,

void change();

and in the function we have added a parameter value i.e, 

void change(int value); 

and with in function body we simply added value = 5; 

Now in the main method we have declared and initialized a variable value1 = 3 and after that we just called the function with value1 variable as argument like this change(value1). Now always remember we pass an argument variable only with its name i.e, change(value1) this is called call by value. This is the basic syntax of call by value.



In the call by reference syntax example the code is almost the same but during the function call we have used the and operator (&) with argument like this 

change(&value1)

this is the syntax of call by reference and when we use and operator (&) it means that we are passing the address of that variable.


Basic working of call by value and call by reference in C++ program

Now we will look at the basic working of call by value and call by reference in C++ programming language. So how this whole concept works basically it starts at the point of function call and the logic for that function call. The most important point of this concept is that when we use call by value method the value of argument variable or original variable will not be changed. And when we use call by reference method the value of original variable will be changed with in the main() method.

What does it mean when we say that value of original variable is not changed during call by value method. And value of original variable will be changed during call by reference method. let us look at the coding examples with respect to both call by value and call by reference.


Basic programming examples of call by value and call by reference in C++ programming language



In the above coding image shown we are implementing call by value method. In this piece of code we have a method named as change(int value2) with parameter name value2 of type int. In this method we are assigning the value of 5 to value2 variable i.e, 

value2 = 5;

Now  in the main() method we have declared and initialized a variable named as value1 = 3. After that we simply call the change(value1) method with value1 as an argument. And after that we simply print the value of value1 variable which contains the value of 3. Now when we call the function change() with in this change function we are assigning the value of 5 to value2 variable. Keep in mind that during the function call we have used call by value method like this 

change(value1)

And when we print the value of value1 variable which is original variable it will print 3 on the console screen as it is call by value method. So the value of value1 variable is not changed. This is the example of call by value.



In the above coding image we have implemented call by reference method. The basic logic of the program is same. We have the same function named as change() but this time the parameter is a pointer variable which holds address of other variables as its value. At this point think of pointer as a variable that holds address of other variables. There is a reason to use pointer variable in change() method, because in the main() method during the function call we are passing the address of value1 variable as an argument to the change() method or function like this,

change(&value1);

As mentioned earlier that when we use and operator(&) with variable name we point to the memory address of that variable. And this is known as call by reference method.

Now when we pass the address as argument to the change() method the address of value1 variable is hold by value2 pointer variable and when we assign value = 5 to the pointer variable like this,

*value2 = 5;

then this change in value will be reflected in the main() method, and changes the value of value1 variable to 5. Remember when we use asteric(*) operator with pointer variable, then we can assign values to the pointer variable.

And thus when we print value1 variable with in the main() method it will print value 5 instead of 3, because this was call by reference method.

Congratulations like always we have successfully implemented call by value and call by reference in C++ programming language.

Comments

Popular posts from this blog

How to run c project in visual studio community 2022

Nested Loops In C++ Programming Language

While Loop in C++ Programming Language