Theory of Programming Languages
Theory of Programming Languages
1
Subprograms
2
Topics
1. Introduction
2. Fundamentals of Subprograms
3. Design Issues for Subprograms
4. Parameter-Passing Methods
3
1. Introduction
4
2. Fundamentals of Subprograms
5
Basic Definitions
7
Procedures and Functions
9
10
Procedures and Functions
2. Procedures :
– A procedure is a method in procedural programming
which does not return a value to main or any other
method.
– Parameters can also be passed to a procedure.
– Like a function these parameters can also be used in
the body of the method.
– Procedures can also be called as many times you
need them.
11
Procedures and Functions
2. Procedures :
– The syntax for a procedure is :
modifier void Procedure-Name( Parameter-list )
{
body of procedure
}
12
Actual / Formal Parameter Correspondence
• Positional Parameters
– The binding of actual parameters to formal parameters is
by position: the first actual parameter is bound to the first
formal parameter and so forth
– Safe and effective
Example
xyz(a, b, c);
. . .
void xyz (int x, int y, int z) {
. . .
}
13
Actual / Formal Parameter Correspondence
• Keyword Parameters
– The name of the formal parameter to which an actual
parameter is to be bound is specified with the actual
parameter
– Advantage: Parameters can appear in any order, thereby
avoiding parameter correspondence errors
– Disadvantage: User must know the formal parameter’s
names
• Example :
xyz ( x = b, z = a, y = c );
14
Formal Parameter Default Values
17
Design Issues for Subprograms
18
Models of parameter passing
19
Semantic models of parameter passing
20
Conceptual models of transfer
21
Parameter passing methods
22
Pass-by-Value ( In Mode)
23
Pass-by-Result ( Out mode )
24
Pass-by-Result ( Out mode )
static public void Main()
{
int c; // Value is not assigned to variable c
Fun1(out c); // variable c is passed to the function using out keyword
// Function in which out parameter is passed and this function returns the value
of
// the passed parameter
public static void Fun1(out int c)
{
c = 10; Result
c = c + c; The value is 20
}
25
C# Example
• Output:
a = 35
26
Pass-by-Value-Result ( In out mode)
27
Pass by Reference (In out mode)
30