Call by Value and Call by Reference
Functions are fundamental elements in programming that allow you to perform specific tasks or actions.
Functions are essential in programming for several reasons:
Modularization of Code:
Functions play a crucial role in breaking down extensive and intricate code into smaller, reusable modules. Consider a scenario where you have thousands of lines of code. It becomes challenging to manage and comprehend such a large codebase. Functions provide a way to compartmentalize these segments of code, making it easier to work with and maintain.Enhancing Readability:
By incorporating functions into your code, you significantly enhance its readability, especially for individuals who need to review or collaborate on the code. Using descriptive function names helps in clearly defining the purpose and functionality of each segment, making the code more understandable and easier to follow.Code Reusability:
One of the significant advantages of functions is their ability to promote code reusability. By encapsulating a specific block of code within a function, you can utilize it multiple times throughout your program without the need to duplicate the code. This not only saves time and effort but also ensures consistency and efficiency in your codebase.
call Values and References in Functions
call by Value
When passing a value to a function, it works on a duplicate of the initial data, keeping the original data intact.
In the call by value method of passing parameters, the values of the real parameters are duplicated into the function's formal parameters.
Two copies of parameters are stored in separate memory locations.
One copy is the original, and the other is the function's copy.
Modifications made inside functions do not affect the actual parameters of the caller.
call by Reference
Passing by reference enables a function to alter the original data directly. In the call by reference method of parameter passing, the function receives the memory address of the actual parameters as the formal parameters. In languages like C, pointers are utilized to achieve call-by-reference.
When passing by reference:
Both the actual and formal parameters point to the same memory locations, ensuring direct access to the original data.
Modifications made within the function are mirrored in the actual parameters of the caller, allowing for seamless updates to the original data without the need for returning values explicitly.
Difference between the Call by Value and Call by Reference
The following table lists the differences between the call-by-value and call-by-reference methods of parameter passing.
Call By Value | Call By Reference |
While calling a function, we pass the values of variables to it. Such functions are known as “Call By Values”. | While calling a function, instead of passing the values of variables, we pass the address of variables(location of variables) to the function known as “Call By References. |
In this method, the value of each variable in the calling function is copied into corresponding dummy variables of the called function. | In this method, the address of actual variables in the calling function is copied into the dummy variables of the called function. |
With this method, the changes made to the dummy variables in the called function have no effect on the values of actual variables in the calling function. | With this method, using addresses we would have access to the actual variables and hence we would be able to manipulate them. |
In call-by-values, we cannot alter the values of actual variables through function calls. | In call by reference, we can alter the values of variables through function calls. |
Values of variables are passed by the Simple technique. | Pointer variables are necessary to define to store the address values of variables. |
This method is preferred when we have to pass some small values that should not change. | This method is preferred when we have to pass a large amount of data to the function. |
Call by value is considered safer as original data is preserved | Call by reference is risky as it allows direct modification in original data |