0% found this document useful (0 votes)
28 views20 pages

PPL-Unit 3 Part 1

Uploaded by

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

PPL-Unit 3 Part 1

Uploaded by

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

Principle of programming languages

Unit-III: Subprograms: Fundamentals of subprograms,


design issues for subprograms, local referencing
environments, parameter passing methods
Fundamentals of
subprograms
•A Subprogram is a program inside any larger
program that can be reused any number of times.
General Subprogram 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.
Basic Definitions
•A subprogram definition describes the interface to
and the actions of the subprogram abstraction.
• A subprogram call is the explicit request that a
specific subprogram be executed.
•A subprogram is said to be active if, after having
Fundamentals of
subprograms
•A subprogram header, which is the first part of the
definition
•In C, the header of a function be as follows:
void adder (parameters)
The reserved word void in this header indicates that
the subprogram does not return a value.
•The header of a Python subprogram be as follows:
def adder parameters):

Parameters:
The parameters in the subprogram header are called
formal parameters. They are sometimes thought of
as dummy 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
Fundamentals of
subprograms
The binding of actual parameters to formal parameters
done by position: The first actual parameter is bound to
the first formal parameter and so forth. Such
parameters are called positional
Parameters
The name of the formal parameter to which an actual
parameter is to be bound is specified with the actual
parameter in a call. Such parameters are called
keyword parameters
Ada, Fortran 95+ and Python allow positional
parameters,Keyword parameters
Example in python
sumer(length = my_length,list = my_array,sum =
my_sum)
where the definition of sumer has the formal
parameters length, list, and sum.
Fundamentals of
subprograms
In Python, Ruby, C++, Fortran 95+ Ada, and PHP,
formal parameters can have default values. A default
value is used if no actual parameter is passed to the
formal parameter in the subprogram header.
Python function header:
def compute_pay(income, exemptions = 1,
tax_rate)
consider the following call:
pay = compute_pay(20000.0, tax_rate = 0.15)
In C++,The default parameters must appear
last,because parameters are positionally associated.
Fundamentals of
subprograms
Procedures and Functions:
•There are two distinct categories of subprograms.
They are procedures and functions.
•All subprograms are collections of statements that
define parameterized computations.
•Functions return values and procedures do not.
•procedures are supported by Ada ,In Fortran,they are
called subroutines. Functions are supported by C based
languages.
•Procedures can produce results in the calling program
unit by two methods:
(1)If there are variables that are not formal parameters
but are still visible in both the procedure and the
calling program unit, the procedure can change
them.
(2) if the procedure has formal parameters that allow
Fundamentals of
subprograms
•Functions are called by appearances of their names in
expressions, along with the required actual
parameters. The value produced by a function’s
execution is returned to the calling code
•Header in C++ could be
float power(float base, float exp)
•which could be called with
result = 3.4 * power(10.0, x)
Design Issues for
Subprograms
Design Issues:
•Are local variables statically or dynamically allocated?
• Can subprogram definitions appear in other
subprogram definitions?
• What parameter-passing method or methods are
used?
• Are the types of the actual parameters checked
against the types of the formal parameters?
• If subprograms can be passed as parameters and
subprograms can be nested,what is the referencing
environment of a passed subprogram?
• Can subprograms be overloaded?
• Can subprograms be generic?
• If the language allows nested subprograms, are
closures
supported?
Local Referencing
Environments
Local Variables:
Variables that are defined inside subprograms are
called local variables, because their scope is usually
the body of the subprogram in which they are defined.
local variables can be either static or stack dynamic.
If local variables are stack dynamic, they are bound to
storage when the subprogram begins execution and
are unbound from storage when that execution
terminates.
The advantages of stack-dynamic local variables,
•The flexibility, It is essential that recursive
subprograms have stack-dynamic local variables.
•stack-dynamic locals is that the storage for local
variables
in an active subprogram can be shared with the local
variables in all inactive subprograms.
Local Referencing
Environments
Disadvantages of stack-dynamic local variables:
•The cost of the time required to allocate and
deallocate such variables for each call to the
subprogram.
•Accesses to stack-dynamic local variables must be
indirect, whereas accesses to static variables can be
direct.
•when all local variables are stack dynamic,
subprograms cannot be history sensitive
advantage of static local variables:
•More efficient because,they require no run-time
overhead for allocation and deallocation.
•They allow subprograms to be history sensitive.
Disadvantage of static local variables
•Inability to support recursion.
• Storage cannot be shared with the local variables of
Local Referencing
Environments
In C and C++ functions, locals are stack dynamic
unless specifically declared to be static.
For example
int adder(int list[], int listlen) {
static int sum = 0;
int count;
for (count = 0; count < listlen; count ++)
sum += list [count];
return sum;
}
In the above C (or C++) function, the variable sum is
static and count is stack dynamic.
P Parameter-Passing Methods
Parameter-passing methods are in which parameters
are transmitted to and or from called subprograms.
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 These models are called in mode
(2)They can transmit data to the actual
parameter .These models are called out mode
(3) They can do both. These models are called inout
mode
P Parameter-Passing Methods
Implementation Models of Parameter Passing:
The implementation of the three basic parameter
transmission modes.
P Parameter-Passing Methods
Types of parameter passing methods:
There are five types of parameter passing
methods. They are
1.Pass-by-Value
When a parameter is passed by value, the value of
the actual parameter is used to initialize the
corresponding formal parameter, which then acts as a
local variable in the subprogram, thus implementing in-
mode semantics.
Consider the following C function:
void swap1(int a, int b) {
int temp = a;
a = b;
b = temp;
}
Suppose this function is called with
P Parameter-Passing Methods
•The advantage of pass-by-value is that for scalars it is
fast, in both linkage cost and access time
•The main disadvantage of the pass-by-value method if
copies are used is that additional storage is required for
the formal parameter
•The storage and the copy be costly if the parameter is
large operation scan
2)Pass-by-Result
•Pass-by-result is an implementation model for out-
mode parameters.
•When a parameter is passed by result, no value is
transmitted to the subprogram.
•The corresponding formal parameter acts as a local
variable, but just before control is transferred back to
the caller, its value is transmitted back to the caller’s
actual parameter
P Parameter-Passing Methods
One additional problem with the pass-by-result model
is that there can be an actual parameter collision
For example, consider the following C# method
void Fixer(out int x, out int y) {
x = 17;
y = 35;
}
...
f.Fixer(out a, out a);
If, at the end of the execution of Fixer, the formal
parameter x is assigned to its corresponding actual
parameter first, then the value of the actual parameter
a in the caller will be 35. If y is assigned first, then the
value of the actual parameter a in the caller will be 17.
P Parameter-Passing Methods
3)Pass-by-Value-Result:
•Pass-by-value-result is an implementation model for
inout-mode parameters in which actual values are
copied.
• It is in effect a combination of pass-by-value and pass-
by-result.
•The value of the actual parameter is used to initialize
the
corresponding formal parameter, which then acts as a
local variable. In fact, pass-by-value-result formal
parameters must have local storage associated with
the called subprogram.
•At subprogram termination, the value of the formal
parameter is transmitted back to the actual parameter.
•Pass-by-value-result is sometimes called pass-by-
copy, because the actual parameter is copied to the
P Parameter-Passing Methods
Pass-by-value-result shares with pass-by-value and
pass-by-result the disadvantages of requiring multiple
storage for parameters and time for copying values
Pass-by-Reference
Pass-by-reference is a second implementation model
for inout-mode parameters.
Rather than copying data values back and forth,
however, as in pass-byvalue- result, the pass-by-
reference method transmits an access path, usually
just an address, to the called subprogram
Thus, the called subprogram is allowed to access the
actual parameter in the calling program unit. In effect,
the actual parameter
is shared with the called subprogram.
The advantage of pass-by-reference is that the passing
process itself is efficient, in terms of both time and
P Parameter-Passing Methods
Disadvantages of the pass-by-reference
•access to the formal parameters will be slower than
pass-by-value parameters, because of the additional
level of indirect addressing that is required
•if only one-way communication to the called
subprogram is required,inadvertent and erroneous
changes may be made to the actual parameter
•Another problem of pass-by-reference is that aliases
can be created.
In C pass-by-reference:
void swap2(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
swap2 can be called with
P Parameter-Passing Methods
Pass-by-Name:
•Pass-by-name is an inout-mode parameter
transmission method.
•When parameters are passed by name, the actual
parameter is, in effect, textually substituted for the
corresponding formal parameter in all its occurrences
in the subprogram.
•A pass-byname formal parameter is bound to an
access method at the time of the subprogram call, but
the actual binding to a value or an address is delayed
until the formal parameter is assigned or referenced.
•Pass-by-name parameters are both complex to
implement and inefficient.
•They also add significant complexity to the program,
•thereby lowering its readability and reliability.

You might also like