PPL Unit-3 Material
PPL Unit-3 Material
Fundamentals of Subprograms
Each subprogram has a single-entry point. The calling program is suspended during execution of
the called subprogram.
•Control always returns to the caller when the called subprogram ‘s execution terminates
Basic Definitions
•A subprogram definition describes the interface to and the actions of the subprogram abstraction
•A subprogram call is an explicit request that the subprogram be executed
•A subprogram header is the first part of the definition, including the name, the kind of
subprogram, and the formal parameters
•The parameter profile (aka signature) of a subprogram is the number, order, and types of its
parameters
•The protocol is a subprogram‘s parameter profile and, if it is a function, its return type
•Function declarations in C and C++ are often called prototypes
•A subprogram declaration provides the protocol, but not the body, of the subprogram
•A formal parameter is a dummy variable listed in the subprogram header and used in the
subprogram
•An actual parameter represents a value or address used in the subprogram call statement
–Disadvantages
Pass-by-Value (In Mode) •The value of the actual parameter is used to initialize the
corresponding formal parameter
–Normally implemented by copying
–Can be implemented by transmitting an access path but not recommended (enforcing write
protection is not easy)
–When copies are used, additional storage is required
–Storage and copy operations can be costly
Fortran
–Always used the inout semantics model
–Before Fortran 77: pass-by-reference
–Fortran 77 and later: scalar variables are often passed by value-result
C
–Pass-by-value
–Pass-by-reference is achieved by using pointers as parameters
C++
–A special pointer type called reference type for pass-by-reference
Java
–All parameters are passed are passed by value
–Object parameters are passed by reference
Ada
–Three semantics modes of parameter transmission: in, out, in out; in is the default mode
–Formal parameters declared out can be assigned but not referenced; those declared in can be
referenced but not assigned; in out parameters can be referenced and assigned
C#
–Default method: pass-by-value
–Pass-by-reference is specified by preceding both a formal parameter and its actual parameter with
ref
Perl: all actual parameters are implicitly placed in a predefined array named @_
Relatively new languages Perl, JavaScript, and PHP do not require type checking
Multidimensional Arrays as Parameters
If a multidimensional array is passed to a subprogram and the subprogram is separately compiled,
the compiler needs to know the declared size of that array to build the storage mapping function
Solution: pass a pointer to the array and the sizes of the dimensions as other parameters; the user
must include the storage mapping function in terms of the size parameters
Pascal
–Not a problem; declared size is part of the array‘s type
Ada
–Constrained arrays - like Pascal
–Unconstrained arrays - declared size is part of the object declaration
Overloaded Subprograms
An overloaded subprogram is one that has the same name as another subprogram in the same
referencing environment
•In Ada, the return type of an overloaded function can be used to disambiguate calls (thus two
overloaded functions can have the same parameters)
•Ada, Java, C++, and C# allow users to write multiple versions of subprograms with the same
name
Generic Subprograms
A generic or polymorphic subprogram takes parameters of different types on different activations.
Overloaded subprograms provide ad hoc polymorphism. A subprogram that takes a generic
parameter that is used in a type expression that describes the type of the parameters of the
subprogram provides parametric polymorphism
The above template can be instantiated for any type for which operator > is defined
int max (int first, int second) {
return first > second? first : second;
}
Design Issues for Functions
Are side effects allowed?
–Parameters should always be in-mode to reduce side effect (like Ada)
What types of return values are allowed?
–Most imperative languages restrict the return types
–C allows any type except arrays and functions
–C++ is like C but also allows user-defined types
–Ada allows any type
–Java and C# do not have functions but methods can have any type
An Ada example
Coroutines
A coroutine is a subprogram that has multiple entries and controls them itself
Also called symmetric control: caller and called coroutines are on a more equal basis
Executions of a coroutine often begin at points other than its beginning. Because of this, the
invocation of a coroutine is called a resume rather than a call.
Coroutines repeatedly resume each other, possibly forever
Coroutines provide quasi-concurrent execution of program units (the coroutines); their
execution is interleaved, but not overlapped.
sub co1(){
. . .
resume co2();
. . .
resume co3();
. . .
}
A coroutine often has a loop containing a resume. Above figure shows the execution sequence of this scenario. In this
case, A is started by the master unit. Inside its main loop, A resumes B, which in turn resumes A in its main loop