Unit 3 PPL
Unit 3 PPL
Unit 3 PPL
SUBPROGRAM:
Features of Subprograms
Types of Subprograms
BEGIN
{instruction sequence}
END;
{end of procedure}
The declaration implies that a procedure has two parts as the specification and the body. The
procedure specification begins with the keyword PROCEDURE and end with the procedure
name or a parameter list. Parameter declarations are optional. Procedures that take no
parameters are written without parenthesis.
Unit -3
The procedure body begins with the keyword BEGIN and end with the keyword END
followed by an optional procedure name. The procedure body has three elements such as a
declarative part, an executable part, and an optional exceptional handling part.
……………….
……………….
instruction sequence
A function has two elements as the specification and the body. The function specification
begins with the Return type followed by name of the function and parameter list. The
function body begins with {and ends with}. The function body has three parts such as a
declaration part, an executable part, and an optional exceptional-handling part.
(1) A Subprogram is implemented using the Call & Return instructions in Assembly
Language.
(2) The Call Instruction is present in the Main Program and the Return(Ret) Instruction is
present in the subprogram itself.
(3) It is important to note that the Main Program is suspended during the execution of any
subprogram. Moreover, after the completion of the subprogram, the main program executes
from the next sequential address present in the Program Counter.
Unit -3
(4) For the implementation of any subprogram, a “Stack” is used to store the “Return
Address” to the Main Program . Here, Return Address means the immediately next
instruction address after the Call Instruction in the Main program. This Return Address is
present inside the Program Counter. Thus during the execution of the Call Instruction, the
Program Counter value is first pushed to the Stack as the Return Address and then the
Program Counter value is updated to the given address in the Call Instruction. Similarly,
during the execution of Return(Ret) Instruction, the value present in the stack is popped and
the Program Counter value is restored for further execution of the Main Program.
(5) The Main advantage of Subprogram is that it avoids repetition of Code and allows us to
reuse the same code again and again.
Scope of a variable
The scope of a vriable is the part of the program for which the declaration is in effect.
In Java, the scope of such a variable is from its declaration to the end of the method.
Lifetime of a variable
The lifetime of a variable is the time period in which the variable has valid memory.
In Java, the lifetime of a variable is the period of time beginning when the method is entered
and ending when execution of the method terminates.
The scope of a variable x in the region of the program in which the use of x refers to its
declaration. One of the basic reasons for scoping is to keep variables in different parts of the
program distinct from one another. Since there are only a small number of short variable
names, and programmers share habits about naming of variables (e.g., I for an array index), in
any program of moderate size the same variable name will be used in multiple different
scopes.
1. Static Scoping
2. Dynamic Scoping
Static Scoping:
Static scoping is also called lexical scoping. In this scoping, a variable always refers to its
top-level environment. This is a property of the program text and is unrelated to the run-time
call stack. Static scoping also makes it much easier to make a modular code as a programmer
can figure out the scope just by looking at the code. In contrast, dynamic scope requires the
programmer to anticipate all possible dynamic contexts.
In most programming languages including C, C++, and Java, variables are always statically
(or lexically) scoped i.e., binding of a variable can be determined by program text and is
independent of the run-time function call stack.
For example, the output for the below program is 10, i.e., the value returned by f() is not
dependent on who is calling it (Like g() calls it and has a x with value 20). f() always returns
the value of global variable x.
int x = 10;
// Called by g()
int f()
return x;
int g()
int x = 20;
return f();
int main()
printf("%d", g());
printf("\n");
return 0;
Output :
10
To sum up, in static scoping the compiler first searches in the current block, then in global
variables, then in successively smaller scopes.
Unit -3
Dynamic Scoping:
With dynamic scope, a global identifier refers to the identifier associated with the most recent
environment and is uncommon in modern languages. In technical terms, this means that each
identifier has a global stack of bindings and the occurrence of an identifier is searched in the
most recent binding.
In simpler terms, in dynamic scoping, the compiler first searches the current block and then
successively all the calling functions.
// scoping.
int x = 10;
// Called by g()
int f()
return x;
int g()
int x = 20;
return f();
}
Unit -3
main()
printf(g());
20
In most programming languages static scoping is dominant. This is simply because in static
scoping it’s easy to reason about and understand just by looking at code. We can see what
variables are in the scope just by looking at the text in the editor.
Dynamic scoping does not care about how the code is written, but instead how it executes.
Each time a new function is executed, a new scope is pushed onto the stack.
Perl supports both dynamic and static scoping. Perl’s keyword “my” defines a statically
scoped local variable, while the keyword “local” defines a dynamically scoped local variable.
$x = 10;
sub f
return $x;
sub g
# dynamic scoping.
local $x = 20;
Unit -3
return f();
print g()."\n";
Output : 20
Parameter-passing methods are the ways in which parameters are transmitted to and/or from
called subprograms.
Parameter passing depends on model of subprogram.There are two models for parameter
passing-
1. Semantics Models of Parameter Passing
2. Implementation Models of Parameter Passing
1. Semantics Models of Parameter Passing: Formal parameters are characterized by one of
three distinct semantics models:
Unit -3
1. They can receive data from the corresponding actual parameter, called in mode.
2. They can transmit data to the actual parameter, called out mode.
3. They can do both, called inout mode.
2. Implementation Models of Parameter Passing: This model consist the following ways
of parameter passing.
1. Pass by value
2. Pass by reference
3. Pass-by-Result
4. Pass-by-Value-Result
5. Pass-by-Name
1. Pass by value: Value of actual parameter in read only mode is transmitted to formal
parameters.
2. Pass by reference: Reference/address of actual parameter is transmitted to formal
parameters.
3. Pass-by-Result: When a parameter is passed by result, no value is transmitted to the
subprogram.
4. Pass-by-Value-Result: Pass-by-value-result is an implementation model for inout-mode
parameters. Pass-by-value-result is sometimes called pass-by-copy, because the actual
parameter is copied to the formal parameter at subprogram entry and then
copied back at subprogram termination.
5. Pass-by-Name: Pass-by-name is an inout-mode parameter transmission method. In
it parameters are passed by name. Implementing a pass-by-name parameter requires a
subprogram to be passed to the called subprogram to evaluate the address or value of the
formal parameter.
Program examples:
Call by value:
#include <iostream>
void show(int x)
{
cout<<x<<endl;
Unit -3
}
int main()
{
int age = 20;
show(age);
show(10);
return 0;
}
Call by reference:
#include <iostream>
using namespace std;
return 0;
}
OVERLOADED SUB-PROGRAMS
The meaning of a call to an overloaded subprogram is determined by the actual parameter list
(and/or possibly the type of the returned value, in the case of a function).
Overloaded subprograms have same name but not necessry have same process.
For example overloaded subprogram in C++:
#include <iostream>
using namespace std;
void show (int a)
{
cout << a ;
}
GENERIC SUBPROGRAMS
A generic subprograms is a subprogram which have parametric polymorphism.
A generic subprogram can accept different types of values of same single memory location.
Parametrically polymorphic subprograms are often called generic subprograms.
C++ provide a kind of compile-time parametric polymorphism.
For example Generic Functions in C++:
Unit -3
template <class Type>
void show(Type a) {
cout<<a;
}
void main()
{
show(10);
show(10.2);
show(‘c’);
}
You can redefine or overload the function of most built-in operators in C++. These operators
can be overloaded globally or on a class-by-class basis. Overloaded operators are
implemented as functions and can be member functions or global functions.
An overloaded operator is called an operator function. You declare an operator function with
the keyword operator preceding the operator. Overloaded operators are distinct from
overloaded functions, but like overloaded functions, they are distinguished by the number and
types of operands used with the operator.
Consider the standard + (plus) operator. When this operator is used with operands of different
standard types, the operators have slightly different meanings. For example, the addition of
two integers is not implemented in the same way as the addition of two floating-point
numbers. C++ allows you to define your own meanings for the standard C++ operators when
they are applied to class types. In the following example, a class called complx is defined to
model complex numbers, and the + (plus) operator is redefined in this class to add two
complex numbers.
#include <iostream>
class complx
{
Unit -3
double real,
imag;
public:
};
// define constructor
real = r; imag = i;
complx result;
return result;
int main()
complx x(4,4);
complx y(6,6);
COROUTINES
function.
Unit -3
Coroutines are generalizations of the subroutines.
A subroutine has the same starting point and the same endpoint all the time, while a coroutine
has multiple entry points for suspending and resuming execution. Coroutines are cooperative,
that means if a coroutine consume input data, another coroutine can consume it, and another
coroutine can be used to display the output.
Coroutines are nothing but cooperative functions.