0% found this document useful (0 votes)
25 views30 pages

Theory of Programming Languages

This document discusses subprograms in programming languages. It covers the fundamentals of subprograms including definitions, procedures versus functions, and parameter passing. The key parameter passing methods are pass-by-value, pass-by-result, pass-by-value-result, and pass-by-reference. Each method transfers parameters differently, with trade-offs around efficiency and ability to modify caller values. Design issues for subprograms include parameter types and passing style.

Uploaded by

AHMAD Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
25 views30 pages

Theory of Programming Languages

This document discusses subprograms in programming languages. It covers the fundamentals of subprograms including definitions, procedures versus functions, and parameter passing. The key parameter passing methods are pass-by-value, pass-by-result, pass-by-value-result, and pass-by-reference. Each method transfers parameters differently, with trade-offs around efficiency and ability to modify caller values. Design issues for subprograms include parameter types and passing style.

Uploaded by

AHMAD Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 30

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

• Two fundamental abstraction facilities


– Process abstraction
• Emphasized from early days
– Data abstraction
• Emphasized in the1980s

4
2. 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

5
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 of a subprogram is the number,
order, and types of its parameters
• The protocol (aka signature) is a subprogram’s parameter
profile and, if it is a function, its return type
6
Basic Definitions (Cont.)

• 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

7
Procedures and Functions

• There are two categories of subprograms


1. Functions :
– A Function is a method in procedural programming
that returns a value.
– Functions are separated from the main method and
can be called from any other method.
– They can also have parameters passed to them to use
in the body of the function.
– Once a function is created , it can be called as many
times as required.
8
Procedures and Functions

The general syntax for a function is :

modifier return-type function-name(list of parameters)


{
body of function
return value statement
}

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

• In certain languages (e.g., C++, Python, Ruby, Ada, PHP),


formal parameters can have default values (if no actual
parameter is passed)
e.g. void fun(int a=1, int b = 2) { } // definition
…….
void fun(); // function call

– In C++, default parameters must appear last because


parameters are positionally associated.
– e.g. void fun(int a, int b , int c=3) { }
• ………
void fun(1,2);
15
• Variable numbers of parameters
– C# methods can accept a variable number of parameters
as long as they are of the same type—the corresponding
formal parameter is an array preceded by params

• Variable numbers of parameters


– In Ruby, the actual parameters are sent as elements of a
hash literal and the corresponding formal parameter is
preceded by an asterisk.
– In Python, the actual parameter is a list of values and the
corresponding formal parameter is a name with an
asterisk
16
C# Example

public void DisplayList(params int[ ] list) {


foreach (int next in list) {
Console.WriteLine(“Next value {0}”, next);
}
}
-----------------
Myclass myObject = new Myclass();
int[ ] myList = new int[6] {2, 4, 6, 8, 10, 12};
-----------------
myObject.DisplayList(myList);
myObject.DisplayList(2, 4, 3 * x – 1, 17);

17
Design Issues for Subprograms

• What parameter passing methods are provided?


• Are parameter types checked (actual vs. formal)?
• Are local variables static or dynamic?
• Can subprogram definitions appear in other
subprogram definitions (nested)?
• Can subprograms be passed as parameters?
• Can subprograms be overloaded?
• Can subprogram be generic?

18
Models of parameter passing

19
Semantic models of parameter passing

• Formal parameters are characterized by one of these


distinct semantics models

In mode : They can receive data from the corresponding


actual parameter.
Out mode : They can transmit data to the actual parameter
Inout mode : They can do both.

20
Conceptual models of transfer

• There are two conceptual model of how data transfers


take place in parameter transmission:
• Either an actual value is physically moved (to the
caller, to the callee, or both ways) or an access path
is transmitted.

21
Parameter passing methods

• Ways in which parameters are transmitted to and/or


from called subprograms
– Pass-by-value
– Pass-by-result
– Pass-by-value-result
– Pass-by-reference

22
Pass-by-Value ( In Mode)

• The value of the actual parameter is used to initialize the


corresponding formal parameter.
• It implements in-mode semantics.
– Normally implemented by copying
– Can be implemented by transmitting an access path but
not recommended.
– When copies are used, additional storage is required
– Storage and copy operations can be costly when data is
large

23
Pass-by-Result ( Out mode )

• When a parameter is passed by result, no value is


transmitted to the subprogram; the corresponding formal
parameter acts as a local variable; its value is
transmitted to caller’s actual parameter when control is
returned to the caller
– Require extra storage location and copy operation
– One additional problem with the pass-by-result model
is that there can be an actual parameter collision.

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

// Display the value c


Console.WriteLine("The value is: {0}", c);
}

// 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

void Fixer (out int x, out int y) {


x = 17;
y = 35;
}
...
f.Fixer(out a, out a); // Calling Fixer function
Console.WriteLine(“a = “ + a);

• Output:
a = 35

26
Pass-by-Value-Result ( In out mode)

• A combination of pass-by-value and pass-by-result


• Sometimes called pass-by-copy
– because the actual parameter is copied to the formal
parameter at subprogram entry and then copied back
at subprogram termination.
• Formal parameters have local storage
• Disadvantages:
– Those of pass-by-result
– Those of pass-by-value

27
Pass by Reference (In out mode)

• transmits an access path, usually just an address, to the


called subprogram.
• the called subprogram is allowed to access the actual
parameter in the calling program unit
• Also called pass-by-sharing
• Passing process is efficient (no copying and no duplicated
storage)
• Disadvantages
– Slower accesses (compared to pass-by-value) to formal
parameters because of the additional level of indirect
addressing that is required
– Potentials for un-wanted side effects
28
Pass by Reference (In out mode)
– Un-wanted aliases (access broadened): harmful to
reliability and readability
• There are several ways pass-by-reference parameters can
create aliases.
• First, collisions can occur between actual parameters.
Consider a C++ function that has two parameters that are to
be passed by reference, as in
• void fun(int &first, int &second)
If the call to fun happens to pass the same variable twice, as in
fun(total, total)
then first and second in fun will be aliases
29
• Second, collisions between array elements can also
cause aliases.
• For example, suppose the function fun is called with
two array elements that are specified with variable
subscripts, as in
fun(list[i], list[j])
• If these two parameters are passed by reference and i
happens to be equal to j, then first and second are again
aliases.

30

You might also like