PARAMETER PASSINGThere are two types of parameters:
- Formal parameters
- Actual parameters
The most common methods are:
1. Call by value:
- This is the simplest method of parameter passing.
- The actual parameters are evaluated and their r-values are passed to called procedure.
- The operations on formal parameters do not changes the values of actual parameter.
Example: Languages like C, C++ use actual parameter passing method.
2. Call by reference:
- This method is also called as call by address or call by location.
- The L-value, the address of actual parameter is passed to the called routines activation record.
- The value of actual parameters can be changed.
- The actual parameter should have an L-value.
3. Copy restore:
- This method is a hybrid between call by value and call by reference.
- This is also known as copy-in-copy-out or values result.
- The calling procedure calculates the value of actual parameters and it is then copied to activation record for the called procedure.
- During execution of called procedure, the actual parameters value is not affected.
- If the actual parameters have L-value then at return the value of formal parameter is copied to actual parameter.
4. Call by name:
- This is less popular method of parameter passing.
- Procedure is treated like macro.
- The procedure body is substituted for call in caller with actual parameters substituted for formals.
- The actual parameters can be surrounded by parenthesis to preserve their integrity.
- The locals name of called procedure and names of calling procedure are distinct.