Docsity

Call by Value & Call by Reference, Cheat Sheet of Java Programming

Toggle sidebar
bg1
Call by Value & Call by Reference
Call by Value
In the call by value method of parameter passing, the values of actual parameters are copied
to the function's formal parameters. There are two copies of parameters stored in different
memory locations — one is the original copy, the other is the function copy. Any changes
made inside the function are NOT reflected in the actual parameter of the caller.
Call by Reference
In the call by reference method of parameter passing, the address of the actual parameter is
passed to the function as the formal parameter.
• Both the actual parameter and the function's formal parameter refer to the same
memory location.
• Any changes made inside the function ARE actually reflected in the actual parameter of
the caller.
Difference between Call by Value and Call by Reference
Call by Value Call by Reference
A way of passing arguments by copying the
actual values into formal parameters of the
function.
A way of passing arguments by copying the
addresses of the arguments to the formal
parameters of the function.
Does NOT affect the original values. DOES affect the original values.
Values of the actual parameter are copied to
the formal parameter of the function.
Addresses of the actual parameters are
copied to the formal parameter of the
function.
Calling function sends a copy of the data. Calling function sends the address of the
data.
The formal parameters are ordinary
variables.
The formal parameters are pointer variables.
At most only one value can be sent back to
the calling function.
Several results can be sent back to the
calling function.
Actual parameters are NOT affected by Direct changes are made to the actual
pf2

Partial preview of the text

Download Call by Value & Call by Reference and more Cheat Sheet Java Programming in PDF only on Docsity!

Call by Value & Call by Reference

Call by Value

In the call by value method of parameter passing, the values of actual parameters are copied

to the function's formal parameters. There are two copies of parameters stored in different

memory locations — one is the original copy, the other is the function copy. Any changes

made inside the function are NOT reflected in the actual parameter of the caller.

Call by Reference

In the call by reference method of parameter passing, the address of the actual parameter is

passed to the function as the formal parameter.

  • Both the actual parameter and the function's formal parameter refer to the same

memory location.

  • Any changes made inside the function ARE actually reflected in the actual parameter of

the caller.

Difference between Call by Value and Call by Reference

Call by Value Call by Reference A way of passing arguments by copying the actual values into formal parameters of the function. A way of passing arguments by copying the addresses of the arguments to the formal parameters of the function. Does NOT affect the original values. DOES affect the original values. Values of the actual parameter are copied to the formal parameter of the function. Addresses of the actual parameters are copied to the formal parameter of the function. Calling function sends a copy of the data. Calling function sends the address of the data. The formal parameters are ordinary variables. The formal parameters are pointer variables. At most only one value can be sent back to the calling function. Several results can be sent back to the calling function. Actual parameters are NOT affected by Direct changes are made to the actual

Call by Value Call by Reference changes made within the function. parameter. Duplicate copy of original parameter is passed. Actual copy of original parameter is passed. No effects on original value after modifying. Original value gets affected if value is modified. Actual & formal parameters are created in different memory locations. Actual & formal parameters are created in the same memory location. Values of variables are passed by simple technique. Pointer variables are necessary to define to store address values of variables. Preferred when passing small values of data to a function. Preferred when passing large amounts of data to a function. Cannot alter the values of actual variables through function calls. Can alter the values of actual variables through function calls. Actual arguments remain safe — cannot be modified accidentally. Pointers of variables are required to store the address of values of variables. Default in many languages: C++, PHP, VB, Basic.Net, C#. Supports languages like Java. Syntax: function_name(variable_name1, variable_name2, ...) Syntax: function_name(&variable_name1, &variable_name2, ...)