Parameter Passing program foo() {a, b integer; procedure bar(integer x, integer
ID: 3803187 • Letter: P
Question
Parameter Passing program foo() {a, b integer; procedure bar(integer x, integer y) {z: integer; {z: integer; z = 3; x = x + y + z; y = 1;}//statement body of foo a = 2; b = 5; call bar(a, b); print a, b;} Use the RISC machine instructions loadI loadI, loadAI, storeAI, add to show the code that needs to be generated tor the body of procedure bar (statements {/*1*/through/*3*/)· Assume that Register r0 (contains the frame pointer (f_p) value. Formal parameter x is call-by-reference. and formal parameter y is call-by-value. Assume that bars parameters x and y have been correctly initialized as part, of the procedure call of bar. Use the slack frame layout as shown above. The figure shows the runtime stack when the program execution reaches program point./*0*/in procedure bar. What, values for a and b does the program print?Explanation / Answer
answer is
a =5
b=2.
While pass by value is suitable in many cases, it has a couple of limitations. First, when passing a large struct or class to a function, pass by value will make a copy of the argument into the function parameter. In many cases, this is a needless performance hit, as the original argument would have sufficed. Second, when passing arguments by value, the only way to return a value back to the caller is via the function’s return value. While this is often suitable, there are cases where it would be more clear and efficient to have the function modify the argument passed in. Pass by reference solves both of these issues.
Pass by reference
To pass a variable by reference, we simply declare the function parameters as references rather than as normal variables:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.