PPL IV Unit-1
PPL IV Unit-1
Subprograms
A subprogram is a sequence of instructions whose execution is
invoked from one or more remote locations in a program, with the
expectation that when the subprogram execution is complete,
execution resumes at the instruction after the one that invoked the
subprogram.
In high-level languages, subprograms are also called subroutines,
procedures, and functions.
In object-oriented languages, they are usually called methods or
constructors.
In most modern high-level languages, subprograms can have
parameters, local variables, and returned values.
At the assembly language level, use of subprograms requires
subprogram linkage protocols.
These protocols involve designating registers for special purposes
and the use of special instructions for subprogram calls and returns.
Basic concept:
Two fundamental abstraction facilities can be included in a
programming language:
Process abstraction
Data abstraction.
Basic Definitions:
Function procedure
int sum(int x, int y) void sum(int x, int y)
{ {
return x+y ;
} }
}
Void main()
{
sum(2,3);
add(3,8);
Need of subprogram :
1. Increase Readability
2. Reuse of code
3. Saving coding time
4. Abstraction achieved
A subprogram header is the first line of the definition, serves several
definitions:
It specifies that the following syntactic unit is a subprogram
definition of someparticular kind.
The header provides a name for the subprogram.
May optionally specify a list of parameters.Consider the following
header examples:
Fortran
Subroutine Adder(parameters)
Ada
procedure Adder(parameters)
C
void Adder(parameters)
java
publicintaddNumbers(parameters)
Python
defmyFun(parameters)
Mainfun subfunction
• Implementations:
– Pass by Value (in)
– Pass by Result (out)
– Pass by Value-Result (inout)
– Pass by Reference (inout)
– Pass by Name (varies)
A=B*j+d;
* and+ are basically int and float data types based operations and if any
variable in this A=B*j+d;is of other than int and float then compiler will generate
type error.
{
double result, i;
…
result = f(i);
…}
function sub2( ) {
alert(x); // Creates a dialog box with the value of x
};
function sub3( ) {
varx;
x = 3;
sub4(sub2);
};
function sub4(subx ) {
varx;
x = 4;
subx( );
};
x = 1;
sub3( );
};
Variables that are defined inside subprograms are called local vars.
Local variables can be either static or stack dynamic “bound to
storage when the programbegins execution and are unbound when
execution terminates.”
Static variables
o Static variables are bound to their memory cells by the loader which
puts the program into RAM
o They remain bound to the same location until the program
terminates.
o The association of variable name and address is kept by the symbol
table.
o So this binding takes place before execution, and remains for the
entire time the program is executing.
o Static variables are very efficient, but have reduced flexibility.
o Languages that have only static variables cannot support recursion.
o They also do not allow storage to be shared.
o The lifetime of static variables is the duration of the execution of the
program.
intfun()
{
int x = 10; // stack dynamic
staticintcount = 5; //static
count++;
x++;
returnx++;
}
intmain()
{
Disadvantages:
a. Allocation/deallocation time.
b. Indirect addressing “only determined during execution.”
c. Subprograms cannot be history sensitive “can’t retain data values
of local varsbetween calls.”
Advantages of using static vars:
a. Static local vars can be accessed faster because there is no
indirection.
b. No run-time overhead for allocation and deallocation.
c. Allow subprograms to be history sensitive.
Disadvantages:
a. Inability to support recursion.
b. Their storage can’t be shared with the local vars of other
inactivesubprograms.
c. In C functions, locals are stack-dynamic unless specifically
declared to be static.
Ex:
int adder(int list[ ], intlistlen)
{
staticint sum = 0; //sum is static variable
int count; //count is stack-dynamic
for (count = 0; count <listlen; count++)
sum += list[count];
return sum;
}
GENERIC SUBPROGRAMS
Example in java:
classDemoClass {
public<T> void genericsMethod(T var1)
{
System.out.println("Generics Method:\n");
System.out.println("Data Passed: " + var1);
}}
class Main {
public static void main(String[] args) {
DemoClassdemo = new DemoClass();
demo.genericsMethod("Java Programming\n");
demo.genericsMethod(25);
}
}
Parameter Passing Method :
Pass-by-Value:
The values that are passed in the function call are called
the actual parameters.
Pass-by-Result:
Pass-by-Result is an implementation model for out-mode parameters.
intfunc( )
{
int a= 10, b=20;
a = a + b;
cout<<"In func, a = " << a << " b = "<< b
<<endl;
return b; //pass by result (out mode)
}
int main()
{
// Passing parameters
cout<< "func value:" <<func();
return 0;
}
Pass-by-Value-Result
It is an implementation model for inout-mode parameters in which
actual values arecopied.
It is a combination of pass-by-value and pass-by-result.
The value of the actual parameter is used to initialize the
corresponding formalparameter, which then acts as a local var.
At subprogram termination, the value of the formal parameter is
transmitted back tothe actual parameter.
It is sometimes called pass-by-copy because the actual parameter is
copied to theformal parameter at subprogram entry and then
copied back at subprogram termination.
{ a = a + b;
// Passing parameters
Int z= func(x, y); //pass by value (in mode)
Pass-by-reference
Advantages:
The passing process is efficient in terms of time and space.
Duplicate space isnot required, nor is any copying.
Disadvantages:
Access to the formal parameters will be slower than pass-by-value,
because ofadditional level of indirect addressing that is required.
Inadvertent and erroneous changes may be made to the actual
parameter.
Aliases can be created as in C++.
sets up parameters,
sets up a return address,
jumps to the subprogram start address, and
deals with the returned value.