0% found this document useful (0 votes)
51 views9 pages

PPL Notes Unit 3

Subprograms are essential components in programming, defined by their interface and actions, with two main types: procedures and functions. Parameters can be passed through various methods, including positional and keyword parameters, and the semantics of parameter passing can vary, affecting how data is received or transmitted. Dynamic scoping can be implemented through deep or shallow access, each with distinct methods for managing local and nonlocal variable references.

Uploaded by

anju0102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views9 pages

PPL Notes Unit 3

Subprograms are essential components in programming, defined by their interface and actions, with two main types: procedures and functions. Parameters can be passed through various methods, including positional and keyword parameters, and the semantics of parameter passing can vary, affecting how data is received or transmitted. Dynamic scoping can be implemented through deep or shallow access, each with distinct methods for managing local and nonlocal variable references.

Uploaded by

anju0102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

FUNDAMENTALS OF SUB PROGRAM (important)

● 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.

● A subprogram call is the explicit request that a specific subprogram be


executed.

● The two fundamental kinds of subprograms, procedures and functions A


subprogram header, which is the first part of the definition, serves several
purposes. First, it specifies that the following syntactic unit is a
subprogramdefinition of some particular kind. 1 In languages that have more than
one kind of subprogram, the kind of the subprogram is usually specified with a
special word. Second, if the subprogram is not anonymous, the header provides
a name for the subprogram. Third, it may specify a list of parameters.

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.

● The protocol of a subprogram is its parameter profile plus, if it is a function, its


return type. In languages in which subprograms have types, those types are
defined by the subprogram’s protocol.

● In the case of subprograms, it is the type of the parameters that must be


checked. Function declarations are common in C and C++ programs, where they
are called prototypes.

PARAMETERS

● Subprograms typically describe computations.


● There are two ways that a non-method subprogram can gain access to the data
that it is to process: throughdirect access to nonlocal variables or through
parameter passing.
● Data passed through parameters are accessed using names that are local to
the subprogram. Parameter passing is more flexible than direct access to
nonlocal variables.

● 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.

Procedures and Functions


There are two categories of subprograms
● Procedures are collection of statements that define parameterized computations
● Functions structurally resemble procedures but are semantically modeled on
mathematical functions
● They are expected to produce no side effects
● In practice, program functions have side effects
Implementing of Dynamic scoping

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;
...
}

This program is written in a syntax that gives it the appearance of a program


in a C-​­based language, but it is not meant to be in any particular language.
Suppose the following sequence of function calls occurs:
Main calls sub1
sub1 calls sub1
sub1 calls sub2
sub2 calls sub3

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

Shallow access is an alternative implementation method, not an alternative semantics.


As stated previously, the semantics of deep access and shallow access are identical. In
the ­shallow-​­access method, variables declared in subprograms are not stored in
the activation records of those subprograms. Because with dynamic scoping there
is at most one visible version of a variable of any specific name at a given time, a very
different approach can be taken. One variation of shallow access is to have a separate
stack for each variable name in a complete program. Every time a new variable with a
particular name is created by a declaration at the beginning of a subprogram that has
been called, the variable is given a cell at the top of the stack for its name. Every
reference to the name is to the variable on top of the stack associated with that name,
because the top one is the most recently created. When a subprogram terminates, the
lifetimes of its local variables end, and the stacks for those variable names are popped.

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

Semantics Models of Parameter Passing


Formal parameters are characterized by one of three distinct semantics
models:(1) They can receive data from the corresponding actual parameter; (2)
they can transmit data to the actual parameter; or (3) they can do both. These
models are called in mode, out mode, and inout mode, respectively. For example,
consider a subprogram that takes two arrays of int values as ­parameters—​­ list1 and
list2 . The subprogram must add list1 to list2 and return the result as a revised version
of list2 . Furthermore, the subprogram must create a new array from the two given
arrays and return it. For this subprogram, list1 should be in mode, because it is not to be
changed by the subprogram. list2 must be inout mode, because the subprogram needs
the given value of the array and must return its new value. The third array should be out
mode, because there is no initial value for this array and its computed value must be
returned to the caller.

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

Disadvantages of access path method:


Must write-protect in the called subprogram, Accesses cost
more (indirect addressing)

Disadvantages of physical move:


● Requires more storage
● Cost of the moves

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 multiple modes By textual substitution ,Formals are bound to an access


method at the time of the call, but actual binding to a value or address takes place at the
time of a reference or assignment

Purpose : flexibility of late binding


Resulting semantics:
● If actual is a scalar variable, it is pass-by-reference
● If actual is a constant expression, it is pass-by-value
● If actual is an array element, it is like nothing else
● If actual is an expression with a reference to a variable that is also accessible in
the program, it is also like nothing else

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

You might also like