Subprograms
• Subprogram parameters
• Design issues
• Semantic models of parameter passing
• Parameter passing methods:
Call by value
Call by result
Call by reference
Call by value result
Call by name
Pilani Campus
BITS Pilani, Hyderabad Campus
Subprogram
• Fundamental building blocks of programs, collection of
statements that can be reused.
Characteristics:
• Each subprogram has a single entry point.
• The calling program unit is suspended during the
execution of the called subprogram, which implies that
there is only one subprogram in execution at any given
time.
• Control always returns to the caller when the
subprogram execution terminates.
BITS Pilani, Hyderabad
Pilani Campus
Campus
Subprogram parameters
• Parameters in a subprogram are called as formal
parameters. They are dummy variables (or placeholders
for actual parameters to be supplied in a function call)
and they are bound to storage only when the
subprogram is called and that binding is often through
other program variables.
• Subprogram call statements must include the name of
the subprogram and a list of parameters to be bound to
the formal parameters of the subprogram. These
parameters are called as actual parameters.
BITS Pilani, Hyderabad
Pilani Campus
Campus
Design issues for
subprograms
• Are local variables statically or dynamically allocated?
• Can subprogram definitions appear in other subprogram
definitions?
• What parameter passing methods are used?
• Are the types of the actual parameters checked against
the types of the formal parameters?
• If subprograms can be passed as parameter?
• Can subprograms be overloaded?
BITS Pilani, Hyderabad
Pilani Campus
Campus
Parameter passing methods
• Ways in which parameters are transmitted to and from
the called subprograms.
Semantics models of parameter passing:
• Formal parameters can receive the data from the
corresponding actual parameter. (in mode)
• They can transmit the data to the actual parameter. (out
mode)
• Or they can do both. (in-out mode)
BITS Pilani, Hyderabad
Pilani Campus
Campus
Models of Parameter Passing
BITS Pilani, Hyderabad
Pilani Campus
Campus
Example
• Consider a subprogram that takes 2 int arrays as
parameters, list1 and list2. The subprogram must add
list1 to list2 and return the result as a revised version of
list2. The subprogram must create a new array from the
2 given arrays and return it.
• list1 – in mode
• list2 – in-out mode
• list3 –out mode
BITS Pilani, Hyderabad
Pilani Campus
Campus
Implementation models of parameter
passing
Physically move a value
Move an access path to a value
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass-by-Value (In Mode)
• The value of the actual parameter is passed to the
corresponding formal parameter. Formal parameter is a
local variable to the function.
– Normally implemented by copying
– Disadvantages (if by physical move): additional storage is
required (stored twice) and the actual move can be costly (for
large parameters), formal parameters will be stored as local
variables in the stack of the function.
BITS Pilani, Hyderabad
Pilani Campus
Campus
Example of pass by value
void swap1(int a, int b)
{
int temp=a;
a=b;
b=temp;
}
swap1(c,d);
Pass by value:
a=c;
b=d;
temp=a;
a=b;
b=temp;
• a has d’s value and b has c’s value but changes are not passed back to the calling
function so values of c and d are unchanged.
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass-by-Result (Out Mode)
• When a parameter is passed by result, no value is
transmitted to the subprogram; the corresponding formal
parameter acts as a local variable; its value is
transmitted to caller’s actual parameter when control is
returned to the caller, by physical move
– Require extra storage location and copy operation
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass-by-result
• There can be an actual parameter
collision but the formal parameters can • The order in which the actual
have different names and therefore they parameters are copied determines
can have different values. their value
• In this case, whichever of the two is • Left to right
copied to the actual parameter in the • Right to left
last, that becomes the value of the
actual parameter ‘a’. • Output:
• Value of a= 35 (if x is assigned to
Void Fixer(int x, int y) actual parameter first i.e. left to
{ right)
x= 17;
y= 35; • Output:
} • Value of a= 17 (if y is assigned to
Consider the call: actual parameter first i.e. right to
left)
Fixer(a,a);
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass-by-result
• Binding of the address of the actual
parameter can be done at the time Binding of actual parameter to an address:
of the call or at the time of the At the time of the call:
return. • FP1=x FP2= index
• The address of list[sub] changes • AP1=list[21] AP2=sub
between the beginning and end of
the method. Output:
Void doit(int x, int index) • List[21]=17 and sub=42
{
x=17;
index=42;
} FP: Formal parameter
AP: Actual parameter
Consider the call:
sub=21;
doit(list[sub],sub);
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass-by-result
Void doit(int x, int index)
At the end of the call:
{
•FP1=x FP2= index
x=17; •AP1=list[42] (in case of right-to-left) and
index=42; list[21] (in case of left-to-right)
} • AP2=sub
Output:
Consider the call: •List[42]=17 and sub=42(in case of Right-left)
sub=21; •List[21]=17 and sub=42(in case of left-right)
doit(list[sub],sub);
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass-by-Value-Result (inout Mode)
• A combination of pass-by-value and pass-by-result: Actual values
are copied to formal parameters which then acts as local variables in
the called subprogram. At program termination, the value of the
formal parameter is copied back to the actual parameter.
• Sometimes called pass-by-copy
• Disadvantage: Require extra storage location and copy operation
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass by value result (Ada
example)
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass-by-Reference (Inout Mode)
• Formal parameters store the location of actual parameter. In this method, an access
path is passed to the called subprogram (location)
• Advantage: Passing process is efficient (no copying and no duplicated storage)
Disadvantages
– Slower accesses (compared to pass-by-value) to formal parameters because
indirect addressing
– If one-way communication is required to the called subprogram, then erroneous
changes might occur in the actual parameters.
– Unwanted aliases (access broadened): Access path is available to the called
subprograms thus providing access to non-local variables. Examples:
void fun (int &first, int &second) , fun(total, total)
int *global;
void main () {
sub(global);
}
void sub(int *param)
{ …}
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass by reference
void swap2(int *a, int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
swap2(&c, &d);
Pass by reference:
a=&c;
b=&d;
temp=*a;
*a=*b;
*b=temp;
• c and d values are interchanged.
BITS Pilani, Hyderabad
Pilani Campus
Campus
Example
If pass by value result is used, i = 3
If pass by reference is used, i = 5
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass-by-name
• Pass-by-name is inout-mode parameter transmission method.
• In this, actual parameter textually substitutes the formal parameter.
Procedure swap(a,b)
• Effect of the Call swap(x,y) int a,b,temp;
temp = x; Begin
temp = a;
x =y;
a = b;
y = temp; b = temp;
• Output: End;
x=7 x=5,y=7;
y=5 swap(x,y);
BITS Pilani, Hyderabad
Pilani Campus
Campus
Implementing Parameter-Passing Methods
• In most languages parameter communication takes
place through the run-time stack
• Pass-by-reference are the simplest to implement; only
an address is placed in the stack
BITS Pilani, Hyderabad
Pilani Campus
Campus
Implementing Parameter-Passing Methods
Function header: void sub(int a, int b, int c, int d)
Function call in main: sub(w, x, y, z)
(pass w by value, x by result, y by value-result, z by reference)
BITS Pilani, Hyderabad
Pilani Campus
Campus
Parameter passing: Call by
value
int x=1; // global x
void func1 (int a)
{
// location 2 location 2: a is 1, x is 1
x=2;
// location 3 location 3: a is 1 , x is 2
a=5;
// location 4 location 4: a is 5, x is 2
}
void main() {
// location 1 location 1: a undefined, x is 1
func1(x);
// location 5 location 5: x is 2
}
BITS Pilani, Hyderabad
Pilani Campus
Campus
Parameter passing: Call by
reference
int x=1; // global x
void func1 (int a)
{
// location 2 location 2: a is 1, x is 1
x=2;
// location 3 location 3: a is 2 , x is 2
a=5;
// location 4 location 4: a is 5, x is 5
}
void main() {
// location 1 location 1: a undefined, x is 1
func1(x);
// location 5 location 5: x is 5
}
BITS Pilani, Hyderabad
Pilani Campus
Campus
Parameter passing: Call by
value-result
int x=1; // global x
void func1 (int a)
{
// location 2 location 2: a is 1, x is 1
a=3;
// location 3 location 3: a is 3 , x is 1
x=4;
// location 4 location 4: a is 3, x is 4
}
void main() {
// location 1 location 1: a undefined, x is 1
func1(x);
// location 5 location 5: x is 3
}
BITS Pilani, Hyderabad
Pilani Campus
Campus
Example
int n;
void p(int k) Output:
Call-by-value: 1 1
{ Call-by-reference: 5 5
n=n+1; Call-by-value-result: 1 4
k=k+4;
printf(“%d”, n);
}
main()
{
n=0;
p(n);
printf(“%d”, n);
}
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass by name: Example
function (int a, int b, int c)
{ 49 49 64 49 15
b = b + 5;
b = a + c + 4;
printf (“%d %d %d”, a, b, c); Formal arguments will be
} textually replaced with
main ( ) actual arguments i.e. a
{ will be replaced with j, b
int j = 10; will be replaced with j
ink k = 15; and c will be replaced
function (j, j, j + k); with (j + k)
printf (“%d %d”, j, k);
}
BITS Pilani, Hyderabad
Pilani Campus
Campus
Pass by name: Example
Procedure swap(a,b)
• Effect of call swap(i, x[i])
int a,b,temp;
temp = i;
Begin i = x[i];
temp = a; x[i] = temp;
a = b;
b = temp; • Value of i=5
End; • Value of x[0]=1
• Value of x[1]=3
• Value of x[2]=5
• Value of x[3]=7
i=2 • Value of x[4]=9
X ={1,3,5,7,9,11} • Value of x[5]=2
swap(i, x[i]);
BITS Pilani, Hyderabad
Pilani Campus
Campus