Unit III
Q1. Explain subprogram characteristics
• 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.
Q2. Derive Subprogram definitions. [NOV 2017]
A subprogram definition describes the interface to and the actions of the subprogram
abstraction
- In Python, function definitions are executable; in all other
languages, they are non- executable
Q3. What is subprogram Call
• A subprogram call is an explicit request that the subprogram be executed
Q4. What is subprogram Header
• A subprogram header is the first part of the definition, including the
name, the
kind of subprogram, and the formal 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
about:blank 1/32
,8/27/23, 2:16 PM UNIT III PPL with dates - 4th unit imp question answers4th unit imp questio…
Q5. What are the Subprograam parameters? [NOV 2017]
There are two ways that subprogrram can gain access to the data that it is to
process:
Direct access to nonlocal vvariables
Parameter passing
** Parameter passing is more flexxible than direct access to nonlocal variables
• A formal parameter is a dummy variable listed in the subprogram h header
and used in the subprogram
• An actual parameter reepresents a value or address used in the
subprogram call statement
• Positional parameters:is a m
method for binding actual parameter to formal param
meter, is done
by position
** ((the first actual parameters is bound to the first formal parameter and so forth
such parameters are called positioonal parameters ))
● Keyword parameters :
The name of the formal paramete r to which an actual parameter is to be bound is
specified with the actual parameteer
Advantage:
That they can appear in any orderr in the actual parameter list
Disadvantage:
That the user of the subprogram m
must know the names of formal parameters.
about:blank 2/32
,8/27/23, 2:16 PM UNIT III PPL with dates - 4th unit imp question answers4th unit imp questio…
Q6. Explain the term Procedures and Functions [NOV 2016]
There are two distinct categories of subprograms—procedures and functions
Procedures are collection of statements that define parameterized
computations.
o procedures have no return values
Functions structurally resemble procedures but are semantically
modeled on mathematical functions. Functions have return values
procedures are expected to produce no side effects
In practice, program functions have side effect.
Procedures define new statements. For example, if a particular language does not have a sort
statement, a user can build a procedure to sort arrays of data and use a call to that procedure
in place of the unavailable sort statement. In Ada, procedures are called just that; in Fortran,
they are called subroutines. Most other languages do not support procedures.
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 the transfer of data to the caller,
those parameters can be changed.
procedure example as follows
procedure ad d;
var x : integer ; y : integer ;
begin read ( x, y );
write ( x + y );
end
Procedures has 2 parts:
o The specification and the Body
The specification begins with the keyword PROCEDURE and
ends With the procedure name or parameter list.
Ex : procedure a_test(a,b : in integer ; c:out Integer)
Functions define new user-defined operators. For example, if a language does not have
an exponentiation operator, a function can be written that returns the value of one of its
parameters raised to the power of another parameter. Its header in C++ could be
about:blank 3/32
, 8/27/23, 2:16 PM UNIT III PPL with dates - 4th unit imp question answers4th unit imp questio…
float power(float base, float exp)
which could be called with result = 3.4 * power(10.0, x)
The standard C++ library already includes a similar function named pow.
Example
Void sort (int list[], int listlen); // function header
…
Sort(scores,100); // function call
Q7. What are the design Issues for Subprograms? [MARCH 2018]
Subprograms are complex structures in programming languages. An overloaded
subprogram is one that has the same name as another subprogram in the same
referencing environment. A generic subprogram is one whose computation can
be done on data of different types in different calls.
Are local variables static or dynamic?
Can subprogram definitions appear in other subprogram definitions?
What parameter passing methods are provided?
Are parameter types checked?
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 subprogram be generic?
Q8. Explain the term Local Referencing Environments
Local Variables
Subprograms can define their own variables, thereby defining local referencing
environments. 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 terminate
Advantages of stack dynamic variables
Support for recursion
Storage for locals is shared among some subprogram
Main disadvantages of stack dynamic local variables are:
Cost of time required to allocate, initialize and de-allocate for each activation
Accesses of stack dynamic local variables must be indirect(indirect addressing), where accesses to
about:blank 4/32