Difference between Call by value Vs Call by reference

Call by ValueCall by Reference
In this case, a copy of actual data is created and passed to the function.In this case, address of actual data is passed to the function.
In this case, any changes in data modified by the function will not be reflected to actual data.Since, address of actual data is available with the function, hence any change in data will be reflected to actual data also.
Since in this case, copy of actual data is created, hence for large size of data its inefficient as it will need more memory and extra CPU cycles to copy all data.Since, in this case, only address of actual data is passed, hence it’s not dependent of data size as pointer size will always be equal to size of an integer.

For more information related to Call by value and Call by reference Click here.

Difference between Call by value Vs Call by reference Read More

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