call by referencepassing variablesparameters to a


Call by Reference

Passing variables(parameters) to a function in C can be done in two ways - pass by value, also called as call by value and pass by address or also known as call by address. C++ though , gives one more way to call a function - call by reference. In call by value , a copy of the actual contents of the variables is passed to the function. In this case, alters made to the formal variable , as they are called, are not reflected back in the calling function. Passing an address permits changes to be made directly to the memory, which are occupied by the actual variable.

Though, a call by address requires the formal variable to be considered as pointer and therefore the indirection operator has to be used with them. A reference on the other hand, is another name, for a previously explained variable. In other words, after a reference is explained for a particular variable, using its original name as well as the reference can refer to the variable. Hence, a reference is nothing but an alias.

 Thus not passing a copy (call by value) saves time and is efficient. It does not have to make a temporary variable /. And not passing the address(call by address), eliminates the use of pointers in the called functions. But, at the same time, the changes made to a formal variable , using its reference or alias can be reflected back in the calling function.

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: call by referencepassing variablesparameters to a
Reference No:- TGS0309415

Expected delivery within 24 Hours