The Gateway to Computer Science Excellence
+2 votes

Consider the procedure declaration:

P (k: integer)

where the parameter passing mechanism is call-by-value-result. Is it correct if the call, P (A[i]), where A is an array and i an integer, is implemented as below.

  1. create a new local variable, say z;
  2. assign to z, the value of A [i];
  3. execute the body of P using z for k;
  4. set A [i] to z;

Explain your answer. If this is incorrect implementation, suggest a correct one.

in Compiler Design by Veteran (106k points) | 383 views
is it out of syllabus?
Nopes. Parameter passing is in Compilers.

1 Answer

+2 votes
The given implementation is correct.

In call-by-value-result parameter passing mechanism, formal parameter gets its initial value from the actual parameter being passed. During execution of the function, there is no effect on the actual argument but once the function is returned, the final value of the parameter is copied back to the actual parameter. And this is exactly what the given implementation does.

This is different from call-by-reference in the sense that there whatever modification happens to the parameter in a function, it must reflect in the actual argument too. These two can produce different outputs if a global variable is being used as a parameter.
by Veteran (434k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,833 questions
57,713 answers
107,658 users