0% found this document useful (0 votes)
19 views11 pages

PPL Unit3

The document discusses subprograms, including procedures and functions, and outlines design issues such as parameter passing, return values, and exception handling. It also covers parameter passing methods, method overloading, and generics in Java, along with design considerations for functions. Additionally, it explains the semantics of call and return, nested subprograms, activation records, and the differences between lexical and dynamic scoping.

Uploaded by

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

PPL Unit3

The document discusses subprograms, including procedures and functions, and outlines design issues such as parameter passing, return values, and exception handling. It also covers parameter passing methods, method overloading, and generics in Java, along with design considerations for functions. Additionally, it explains the semantics of call and return, nested subprograms, activation records, and the differences between lexical and dynamic scoping.

Uploaded by

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

Unit-3

1)Subprogram
A Subprogram is a program inside any larger program that can
be reused any number of times.
There are two types of subprograms:
• Procedures: Collections of statements that define
parameterized computations
• Functions: Structurally similar to procedures, but semantically
modeled on mathematical functions

2) Design issues in Subprogram:


1. Parameter Passing:
Determining how parameters are passed to subprograms is a
crucial design consideration. The two common methods are:
- Pass-by-value: Copies the value of the actual parameter to the
formal parameter.
- Pass-by-reference: Passes a reference (memory address) of the
actual parameter to the formal parameter, allowing modifications to
the original variable.
2. Return Values:
It is essential to decide how subprograms communicate results
back to the calling program. Common approaches include:
- Return value: The subprogram returns a value to the caller.
- Output parameters: The subprogram modifies one or more
parameters to return values.
- Global variables: The subprogram updates global variables to
communicate results.
3. Lifetime and Scope of Variables:
Determining the visibility and lifespan of variables within
subprograms is crucial. Design choices include:
- Local variables:
- Static variables:
- Global variables:
4. Exception Handling:
Handling exceptional situations, errors, and abnormal
conditions is an important consideration. Design choices include:
- try,catch,throw
5. Recursion:
Deciding whether to allow subprograms to call themselves
(recursion) or restrict it. Recursive subprograms can simplify certain
algorithms, but they require careful design and may consume
significant memory.
6. Overloading:
Determining whether subprograms can have the same name
but different parameter lists (overloading).
7. Access Modifiers:
Defining access modifiers to control the visibility of
subprograms. Public, private, and protected modifiers determine
which parts of a program can access the subprogram.
3) Local referencing environment
It is created when a subprogram defines its own
variables. Variables defined inside subprograms are called local
variables.
Local Reference:
1.Stack local reference[normal declaration][memory allocated
at compile time]
2.Static local reference[declared as static variable]

4) PARAMETER PASSING METHODS


Parameter-passing methods are the ways in which
parameters are transmitted to and/or from called subprograms.
Let us assume that a function B() is called from another
function A().
In this case:
A is called the “caller function”
B is called the “called function or callee function”.
Also, the arguments which:
A sends to B are called actual arguments &
The parameters of B are called formal arguments.
Modes:
• IN: Passes info from caller to the callee.
• OUT: Callee writes values in the caller.
• IN/OUT: The caller tells the callee the value of the variable,
which the callee may update.
Methods of Parameter Passing
1. Pass By Value
In call by value method, the value of the actual parameters is
copied into the formal parameters.
Changes made to formal parameters do not get transmitted
back to the caller.
In call by value method, we can not modify the value of the
actual parameter by the formal parameter.
This method is also called call by value.
Example:
#include <stdio.h>
int add(int x, int y)
{
int z = x + y;
return z;
}
int main()
{
int a = 10, b = 20;
int c = add(a, b);
printf("Addition: %d", c);
}
Output: 30
2. Pass by Pointers
This technique uses a pointer. In function we pass memory
address (pointer) of a variable rather than passing the actual value of
variable.
This passing technique allows the function to access and modify
the content at that particular memory location.
Example:

Other Methods of Parameter Passing


1. Pass by Result
This method uses out-mode semantics. The pass-by-result technique
is implemented by copying.
2. Pass by Value-Result
This method uses in/out-mode semantics.
Pass-by-Value + Pass-by-Result.

3. Pass by Name
This technique is used in programming languages such as Algol.
In this technique, the symbolic “name” of a variable is passed.

5)Method Overloading:
Method overloading is that having two or more methods
(functions) in a class with the same name but different arguments.
It can be with a different number of arguments or different data
types of arguments.
Method overloading in Java is also known as Compile-time
Polymorphism, Static Polymorphism, or Early binding.
In Method overloading compared to the parent argument, the
child argument will get the highest priority.
Example:
public class Sum
{
public int sum(int x, int y)
{
return (x + y);
}
public int sum(int x, int y, int z)
{
return (x + y + z);
}
public double sum(double x, double y)
{
return (x + y);
}
public static void main(String args[]) {
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5)); }}
Output:
31
60
31.0
Different Ways of Method Overloading in Java
• Changing the Number of Parameters.
• Changing Data Types of the Arguments.
• Changing the Order of the Parameters of Methods

6) Generics in Java
Generics means parameterized types. The idea is to allow a
type (like Integer, String, etc., or user-defined types) to be a
parameter to methods, classes, and interfaces.
Using Generics, it is possible to create classes that work with
different data types.
Syntax:
public <T> void methodName(T parameter)
{
// method implementation
}
Here <T> denotes generic type parameter.

Types of Java Generics


• Generic Method:
• Generic Classes:
7) Design Issues of Functions:
Functions are an essential part of programming as they allow
for code reuse and modularization.
However, there are several design issues that need to be
considered when designing functions. These issues include:
1. Function Cohesion: Cohesion refers to how closely the
operations within a function are related to each other. A well-
designed function should have high cohesion.
2. Function Length: The length of a function refers to the number
of lines or statements it contains. Functions that are too long
can be difficult to understand and maintain.
3. Function Naming: Choosing appropriate and descriptive names
for functions is important for code readability and
maintainability.
4. Function Parameters: Functions often require input parameters
to perform their tasks. It is important to carefully choose the
number and types of parameters
5. Function Return Values: Functions may or may not return
values. It is important to define and document the expected
return values of functions
6. Function Side Effects: Side effects occur when a function
modifies variables or has an impact on the state of the program
outside of its return value.
7. Function Error Handling: Functions should handle errors and
exceptions gracefully.
8) Semantics of call and return
The semantics of call and return, also known as subprogram
linkage, refers to the operations involved in calling and returning
from a subprogram in a language.
• Parameter passing: Methods for passing in and in-out
arguments
• Local variable allocation: Whether to use stack or dynamic
allocation
• Execution status: Saving the execution status of the calling
program
• Control transfer: Transferring control and setting up for the
return to the call point
• Nonlocal variable access: If subprogram nesting is supported,
arranging access to nonlocal variables

9) Nested Subprograms
•Some non-C-based static-scoped languages (e.g., Fortran 95,
Ada, JavaScript) use stack-dynamic local variables and allow
subprograms to be nested
•All variables that can be non-locally accessed reside in some
activation record instance of enclosing scopes in the stack
•A reference to a non-locally variable in a static-scoped
language with nested subprograms requires a two step access
process:
1.Find the correct activation record instance
2.Determine the correct offset within that activation record
instance.
Activation Record:
An Activation Record is a region of memory allocated during a
procedure call to store control information, parameter values, local
variables, and other necessary data specific to that procedure
invocation.

10.

Lexical(Static) Scoping Dynamic Scoping

In this variable refers to top In this variable is associated to


level environment most recent environment

It is easy to find the scope by In this programmer has to


reading the code anticipate all possible contexts

It is dependent on how code is It is dependent on how code is


written executed

Structure of program defines Runtime state of program stack


which variable is referred to. determines the variable.
Lexical(Static) Scoping Dynamic Scoping

It is property of program text


It is dependent on real time stack
and unrelated to real time
rather than program text
stack.

It provides less flexibility It provides more flexibility

Accessing to nonlocal Accessing to nonlocal variables in


variables in lexical scoping is dynamic scoping takes more
fast. time.

Local variables can be There is no way to protect local


protected to be accessed by variables to be accessed by
local variables. subprograms.

You might also like