The results returned by function under value-result and reference parameter passing conventions
View worked solution
Answer: D
To understand why the results may differ, let us analyze how both parameter-passing conventions behave:
- Call-by-Reference: The formal parameter acts as an alias to the actual parameter (they share the same memory location). Any write operation to the formal parameter immediately updates the actual parameter.
- Call-by-Value-Result: The formal parameter is a local copy of the actual parameter. All operations during the function execution are performed on this local copy. The final value of the local copy is written back (copied) to the actual parameter only upon normal termination of the function.
If an exception occurs during the execution of the function:
- Under call-by-reference, any modifications made before the exception was thrown are already permanently reflected in the actual parameters.
- Under call-by-value-result, because the function terminates abnormally due to the exception, the copy-back step is typically not executed. Thus, the actual parameters retain their original values.
Hence, the results (the state of the actual parameters after the function call) may differ in the presence of an exception.
Free practice · Your answers on this page are not saved.
