![]() |
Parameter Passing techniques |
Parameter passing is the technique in which the parameters are passed on from the calling function to the called function.
- Pass by Value
- Pass by Address
- Pass by Reference
Pass by Value / Call by Value - In pass by value method, a copy of the parameters is passed from the calling function to the called function. Any changes made to the formal parameters are not considered in the actual parameters.
Example -
Output -
![]() |
Pass by Value |
Use - This method is used when we have to return a value from a function.
Output -
![]() |
Pass by Reference |
Use - This method is used when we want the changes to be reflected in the actual parameters i.e. parameters in the calling function.
💡Note: If the addresses of formal parameters are the same as the addresses of their corresponding actual parameters, then any changes made to the formal parameters will also reflect in the actual parameters.
For Example, if you compare the output of pass by value with pass by reference, you can observe that the method in which the changes made to the formal parameters aren't reflected in the actual parameters is pass by value, and their parameters' address does not match, whereas the one in which they do, i.e. pass by reference, the changes are also reflected.
Pass by Address / Call by Address - In pass by address method, the formal parameters must be pointers, and addresses of the actual parameters are passed on to the formal parameters. Any changes made to the formal parameters are reflected in the actual parameters.
Example -
![]() |
Pass by Address |
Use - This method is used when we want the changes to be reflected in the actual parameters i.e. parameters in the calling function.
Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.
0 Comments