PPL Notes Unit 3
PPL Notes Unit 3
● Subprograms are the fundamental building blocks of programs and are there-
fore among the most important concepts in programming language design.
● A subprogram definition describes the interface to and the actions of the sub-
program abstraction.
Eg : def adder(parameter)
Until the def function has been executed the function cannot be called.
● The parameter profile of a subprogram contains the number, order, and types of
its formal parameters.
PARAMETERS
● If data access is through nonlocal variables, the only way the computation
can proceed on different data is to assign new values to those nonlocal variables
between calls to the subprogram.
Formal parameters
The parameters in the subprogram header are called formal parameters.
They are sometimes thought of as dummy variables because they are not variables in
the usual sense: In most cases, they are bound to storage only when the subprogram is
called, and that binding is often through some other program Variables.
Actual parameters
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 actual parameters.
Positional parameter
In most programming languages, the correspondence between actual and formal
parameters or the binding of actual parameters to formal parameters is done by
position: The first actual parameter is bound to the first formal parameter and so
forth. Such parameters are called positional parameters.This is an effective and
safe method of relating actual parameters to their corresponding formal parameters, as
long as the parameter lists are relatively short.
Keyword parameter
The name of the formal parameter to which an actual parameter is to be bound is
specified with the actual parameter in a call.
There are at least two distinct ways in which local variables and nonlocal refer
ences to them can be implemented in a dynamic-scoped language: deep access
and shallow access.
DEEP ACCESS
If local variables are stack dynamic and are part of the activation records in a
dynamic-scoped language, references to nonlocal variables can be resolved by
searching through the activation record instances of the other subprograms that
are currently active, beginning with the one most recently activated.
The dynamic chain links together all subprogramactivation record instances in the
reverse of the order in which they were activated. Therefore, the dynamic chain is
exactly what is needed to reference nonlocal variables in a dynamic-scoped language.
This method is called deep access, because access may require searches deep into
the stack.
void sub3() {
int x, z;
x = u + v;
...
}
void sub2() {
int w, x;
...
}
void sub1() {
int v, w;
...
}
void main() {
int v, u;
...
}
Figure 10.11 shows the stack during the execution of function sub3 after this calling
sequence. Notice that the activation record instances do not have static links, which
would serve no purpose in a dynamic-scoped language. Consider the references to the
variables x , u , and v in function sub3 . The reference to x is found in the activation
record instance for sub3 . The reference to u is found by searching all of the activation
record instances on the stack, because the only existing variable with that name is in
main . This search involves following four dynamic links and examining 10 variable
names. The reference to v is found in the most recent (nearest on the dynamic chain)
activation record instance for the subprogram sub1 .
SHALLOW ACCESS
Another option for implementing shallow access is to use a central table that has
a location for each different variable name in a program. Along with each entry, a bit
called active is maintained that indicates whether the name has a current binding or
variable association. Any access to any variable can then be to an offset into the central
table. The offset is static, so the access can be fast.
PARAMETER PASSING METHODS
Implementation Models:
• Pass-by-value
• Pass-by-result
• Pass-by-value-result
• Pass-by-reference
• Pass-by-name
Pass-By-Value
● in mode
● Either by physical move or access path
Pass-By-Result
● out mode
● Local’s value is passed back to the caller
● Physical move is usually used
Disadvantages:
● If value is moved, time and space
● In both cases, order dependence may be a problem
● procedure sub1(y: int, z: int);
● ...
● sub1(x, x);
● Value of x in the caller depends on order of assignments at the return
Pass-By-Value-Result
● inout mode
● Physical move, both ways Also called pass-by-copy
Disadvantages:
● Those of pass-by-result
● Those of pass-by-value
Pass-By-Reference
● inout mode
● Pass an access path Also called pass-by-sharing
Advantage:
● passing process is efficient
Disadvantages:
● Slower accesses can allow aliasing: Actual parameter collisions: sub1(x, x);
Array element
● collisions: sub1(a[i], a[j]); /* if i = j */ Collision between formals and globals Root
cause of all of
● these is: The called subprogram is provided wider access to non locals than is
necessary
Pass-by-value-result does not allow these aliases (but has other problems!)
Pass-By-Name Example 1
procedure sub1(x: int; y: int);
begin
x := 1;
y := 2;
x := 2;
y := 3;
end;
sub1(i, a[i]);
Disadvantages of Pass-By-Name
● Very inefficient references
● Too tricky; hard to read and understand