Docsity

Understanding Parameter Passing in Programming: Value, Value-Result, and Reference, Study notes of Programming Languages

Toggle sidebar
bg1
Parameters
We have discussed different classes of variables so far:
-Global/static variables
- Function local or automatic variables
-Dynamic, heap allocated variables
However, one important class of variables is still missing Þparameters
Terminology: Example: Java, C, C++
int plus (int a, int b)
{
return a + b;
}
…
int x = plus(1,2);
…
Formal Parameters
Actual Parameters
Function
Definition
Function
Call
Function Body Observation: in function
definitions formal parameters
act as placeholders for the
values of actual parameters.
Chap 18
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Understanding Parameter Passing in Programming: Value, Value-Result, and Reference and more Study notes Programming Languages in PDF only on Docsity!

Parameters

We have discussed different classes of variables so far:

lobal/static variables

  • Function local or automatic variables
  • Dynamic, heap allocated variables

However, one important class of variables is still missing Þ parameters

Terminology: Example: Java, C, C++

int plus (int a, int b) { return a + b; } … int x = plus(1,2); … Formal Parameters Actual Parameters Function Definition Function Call Function Body Observation: in function definitions formal parameters act as placeholders for the values of actual parameters. Chap 18

Two Fundamental Questions

l How is the correspondence between

actual and formal parameters

established?

l How is the value of an actual parameter

transmitted to a formal parameter?

Correspondence

Some languages such as Ada provide keyword parameters.

Example: Ada

FUNCTION Divide(Dividend:Float, Divisor:Float) RETURN Float IS BEGIN RETURN Dividend/Divisor; END ... Foo = Divide(Divisor => 2.0, Dividend => 4.0); ... 2nd formal parameter becomes 2. 1st formal Parameter Becomes 4.

Parameter Value Transmission

l We look at three different techniques

l By value

l By value-result

l By reference

previous activation record return address a: 3 result:? current activation record previous activation record return address x: 3 b: 4 y: 4 z:? int plus(int a, int b) { a += b; return a; } void f() { int x = 3; int y = 4; int z = plus(x, y); }

When plus

is starting

By Value - Example

II. By Value-Result

l Also called ‘copy-in/copy-out’

l Actual must have an lvalue

For passing parameters by value-result, the formal

parameter is just like a local variable in the activation

record of the called method. It is initialized using the

value of the corresponding actual parameter, before

the called method begins executing. Then, after the

called method finishes executing, the final value of the

formal parameter is assigned to the actual parameter.

Lvalues are values that have addresses, meaning they are variables or dereferenced references to a certain place.

void plus(int a, by-value-result int b) { b += a; } void f() { int x = 3; plus(4, x); }

When plus is

ready to return

previous activation record return address a: 4 current activation record previous activation record return address x: 3 b: 7

By Value-Result - Example

void plus(int a, by-value-result int b) { b += a; } void f() { int x = 3; plus(4, x); }

When plus

has returned

previous activation record return address a: 4 current activation record previous activation record return address x: 7 b: 7

By Value-Result - Example

void plus(int a, by-reference int b) { b += a; } void f() { int x = 3; plus(4, x); }

When plus

is starting

previous activation record return address a: 4 current activation record previous activation record return address x: 3 b:

By Reference - Example

void plus(int a, by-reference int b) { b += a; } void f() { int x = 3; plus(4, x); }

When plus

has made the

assignment

previous activation record return address a: 4 current activation record previous activation record return address x: 7 b:

By Reference - Example