The Gateway to Computer Science Excellence
+2 votes
345 views

Consider the procedure declaration:

Procedure
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 (105k points) | 345 views
0
is it out of syllabus?
+1
Nopes. Parameter passing is in Compilers.

1 Answer

+1 vote
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 (425k 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,645 questions
56,559 answers
195,718 comments
101,599 users