0% found this document useful (0 votes)
10 views29 pages

SEN308 Lecture 6

Uploaded by

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

SEN308 Lecture 6

Uploaded by

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

LECTURE 6

SUBPROGRAMS

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Topics
• Fundamental characteristics of subprograms
• Parameters
• Functions and procedures
• Implementation method of parameter passing
• Type checking parameters
• Design consideration of parameter passing
• Overloaded subprogram
• Generic subprogram

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Fundamental Characteristics of Subprograms

• A subprogram has a single entry point. The caller is


suspended during the execution of the called
subprogram
• Control always returns to the caller when the called
subprogram’s execution terminates

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Basic definitions:

• A subprogram definition is a description of the


actions of the subprogram abstraction
• A subprogram call is an explicit request that the
subprogram be executed
• A subprogram header is the first line 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 of a subprogram is its parameter profile
plus, if it is a function, its return type

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Example of a subprogram header
• Consider the following header examples:
def adder (parameters):
• This is the header of a Python subprogram
named adder. Ruby subprogram headers also
begin with def. The header of a JavaScript
subprogram begins with function.
• In C, the header of a function named adder
might be as follows:
void adder (parameters)
• The reserved word void in this header indicates
that the subprogram does not return a value
Concepts of Programming Languages - NUN 2024 Austin Olom Ogar
Subprogram body

• The body of subprograms defines its actions. In the


C-based languages (and some others—for example,
JavaScript) the body of a subprogram is delimited by
braces. In Ruby, an end statement terminates the
body of a subprogram.
• As with compound statements, the statements in the
body of a Python function must be indented and the
end of the body is indicated by the first statement
that is not indented.

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Basic definitions:

• A subprogram declaration provides the protocol, but


not the body, of the subprogram

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Parameter
• A formal parameter is a dummy variable listed in the subprogram
header and used in the subprogram used in the subprogram call
statement
• Actual/Formal Parameter Correspondence:
1. Positional
2. Keyword
e.g. SORT(LIST => A, LENGTH => N);
• Advantage: order is irrelevant
• Disadvantage: user must know the formal parameter’s names

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Functions and Procedures
• Procedures provide user-defined statements
• Functions provide user-defined operators
• Design Issues for Subprograms
• What parameter passing methods are provided?
• Are parameter types checked?
• Are local variables static or dynamic?
• What is the referencing environment of a passed subprogram?
• Are parameter types in passed subprograms checked?
• Can subprogram definitions be nested?
• Can subprograms be overloaded?
• Are subprograms allowed to be generic?
• Is separate or independent compilation supported?

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Local referencing environments
• If local variables are stack-dynamic:
• Advantages:
• Support for recursion
• Storage for locals is shared among some subprograms
• Disadvantages:
• Allocation/deallocation time
• Indirect addressing
• Subprograms cannot be history sensitive
• Static locals are the opposite
• Language Examples:
1. C - both (variables declared to be static are)
(default is stack dynamic)
2. Pascal, Modula-2, and Ada - dynamic only

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Parameters and Parameter Passing

• Semantic Model: in mode, out mode, InOut mode


• Conceptual Models of Transfer:
1. Physically move a value
2. Move an access path

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Implementation Models:
1. Pass-by-value (in mode)
• Either by physical move or access path
• Disadvantages of access path method:
• Must write-protect in the called subprogram
• Accesses cost more (indirect addressing)
• Disadvantages of physical move:
• Requires more storage
• Cost of the moves

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Implementation Models:
2. Pass-by-result (out mode)
• Local’s value is passed back to the caller
• Physical move is usually used
• Disadvantages:
• If value is passed, time and space
• In both cases, order dependence may be a problem
• e.g.
procedure sub1(y: int, z: int);
...
sub1(x, x);
• Value of x in the caller depends on order of assignments at the return

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Implementation Models:
3. Pass-by-value-result (inout mode)
• Physical move, both ways
• Also called pass-by-copy
• Disadvantages:
• Those of pass-by-result
• Those of pass-by-value

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Implementation Models:
4. Pass-by-reference (inout mode)
- Pass an access path
- Also called pass-by-sharing
- Advantage: passing process is efficient
- Disadvantages:
a. Slower accesses
b. Can allow aliasing:
Actual parameter collisions:
e.g.
procedure sub1(a: int, b: int);
...
sub1(x, x);

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Implementation Models:
5. Pass-by-name (multiple mode)
• By textual substitution
• Formals are bound to an access method at the time of the call, but actual
binding to a value or address takes place at the time of a reference or
assignment
• Purpose: flexibility of late binding
• Resulting semantics:
• If actual is a scalar variable, it is pass-by-reference
• If actual is a constant expression, it is pass-by-value

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Implementation Models:
• Disadvantages of pass by name:
• Very inefficient references
• Too tricky; hard to read and understand
• Language Examples:
1. FORTRAN
• Before 77, pass-by-reference
• 77 - scalar variables are often passed by value-result
2. ALGOL 60
• Pass-by-name is default; pass-by-value is optional
3. ALGOL W - Pass-by-value-result

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Implementation Models: Language examples
4. C - Pass-by-value
5. Pascal and Modula-2
- Default is pass-by-value; pass-by-reference is optional
6. C++
- Like C, but also allows reference type actual
parameters.
7. Java - Like C, except references instead of pointers

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Type checking parameters

• Now considered very important for reliability


• FORTRAN 77 and original C: none
• Pascal, Modula-2, FORTRAN 90, Java, and Ada: it is always required
• C++: choice is made by the user

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Implementing Parameter Passing

• ALGOL 60 and most of its descendants use the run-time stack


• Value - copy it to the stack; references are indirect to the stack
• Result – same as value
• Reference - regardless of form, put the address in the stack
• Name - run-time resident code segments or subprograms evaluate the address of the
parameter; called for each reference to the formal; these are called thunks
• Very expensive, compared to reference or value-result

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Design Considerations for parameter passing

1. Efficiency
2. One-way or two-way
• These two are in conflict with one another!
• Good programming => limited access to variables, which means one-way
whenever possible
• Efficiency => pass by reference is fastest way to pass structures of significant
size
• Also, functions should not allow reference parameters

Concept of Programming Languages


Overloaded subprogram
• An overloaded subprogram is one that has the same name as another
subprogram in the same referencing environment.

• C++ and Ada have overloaded subprograms built-in, and users can
write their own overloaded subprograms

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Generic Subprograms

• A generic or polymorphic subprogram is one that takes parameters of


different types on different activations
• Overloaded subprograms provide ad hoc polymorphism

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Independent/Separate compilation

• Independent compilation is compilation of some of the units of a program


separately from the rest of the program, without the benefit of interface
information
• Separate compilation is compilation of some of the units of a program separately
from the rest of the program, using interface information to check the
correctness of the interface between the two parts.
• Language Examples:
• FORTRAN II to FORTRAN 77 - independent
• FORTRAN 90, Ada, Modula-2, C++ - separate
• Pascal - allows neither

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Functions

• Design Issues:
1. Are side effects allowed?
2. What types of return values are allowed?

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Functions

• Design Issues:
1. Are side effects allowed?
a. Two-way parameters (Ada does not allow)
b. Nonlocal reference (all allow)
2. What types of return values are allowed?
3. How many values can be returned?

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Functions (continued)

• Language Examples (for possible return types):


1. FORTRAN, Pascal, Modula-2 - only simple types
2. C - any type except functions and arrays
3. C++ and Java - like C, but also allow classes to
be returned

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Accessing Nonlocal Environments

• The nonlocal variables of a subprogram are those that are visible but not declared
in the subprogram
• Global variables are those that may be visible in all of the subprograms of a
program

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Questions?

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar

You might also like