PPL Unit3
PPL Unit3
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
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.
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.