Call by value and Call by reference explained with simple example

This is one of very basic concept of C/C++ programming language which is kind of building block of any program or software. Often unknowingly programmers make mistakes in these concept and introduce bugs in softwares 🙂

Basically in C, there are two ways to pass data to a function:

1) Call by value: In this case only value will be passed to the function and the function will store this value in its own local variable. This function will work on this copied data (not original) and set some new value based on some operations. But since this function is working on copied data and not on original data, hence it can’t update the original data in this method.

2) Call by reference: In this case address of the data is passed to the function and function will define a pointer to access this memory location. Since, this function has direct access to the original data memory location, hence it can update the value of original data. Thus, this method is called Call/Pass by reference as we are passing reference of the memory instead of direct data.

(more…)

Call by value and Call by reference explained with simple example Read More