You are on page 1of 19

ICS 313: Programming Language Theory

Module 12: Subprograms

(1)

Objectives
Understand design issues in subprograms: parameter passing type checking overloading and parameterized procedures

(2)

Basic Subprogram Definitions


Subprogram definition: -Describes the actions of the subprogram. Subprogram call: -Explicit request that the subprogram be executed. Subprogram header: -specifies name, parameters, return type. Parameter profile: -number, order, and types of parameters. Protocol: -Parameter profile and return type.
(3)

Parameters
Parameters allow the subprogram to manipulate data external to the subprogram. Provide local names for external data. Allow binding of local names to different data during each call.

Alternative: non-local variable referencing. Also allows access to external data. Reduces readability of subprogram. Reduces flexibility of programming -Requires changing external state before passing
(4)

Positional, Keyword, Defaults


Positional: Matching of formal to actual parameters done via position in parameter list. Keyword: labels used to match formal to actual parameters. Example: Common Lisp -(make-instance pizza :toppings (cheese)) Default: Enables only a subset of actual parameters to be provided in the call.

(5)

Procedures vs. Functions


Procedures: No return value Computation results provided via: -side effects upon non-local variables. -result parameters (in languages that provide this facility). Functions: Invocation results in a value (the result). No side effect necessary (and typically not desired)
(6)

Design Issues for Subprograms


Local referencing environments Parameter passing methods Overloading Separate compilation

(7)

Local Referencing Environments


Local variables: Variables declared inside subprograms. Static local variables: -Allocated once, at beginning of execution. -Retains value between invocations. -Prevents recursive subprograms. Stack-dynamic local variables: -Rebound each time subprogram invoked. -Supports recursive subprograms. -Values lost between invocations.

(8)

Parameter Passing Methods


Two issues: data direction flow and data access style. Data direction flow: In mode: From actuals to formals Out mode: from formals to actuals In-out mode: both directions Data access style: Value: data value is provided. Reference: access path to data value provided.
(9)

Call-by-value: Swap.c (buggy)


void swap (int x, int y) { int z; z = x; x = y; y = z; } main () { int a = 1; int b = 2; swap (a, b); return a; } What is the return value of this program?
(10)

void swap (int *px, int *py) { int z; z = *px; *px = *py; *py = z; } main () { int a = 1; int b = 2; swap (&a, &b); return a; }

Call-by-reference: Swap.c

(11)

Swap is called with the address of locations. Swap is declared to accept pointers.

pseudocode foo (x,y) { i = y; } pseudo main () { int i = 2; int j = 3; foo (i, j); return i; }

Call-by-value-result

What does this program return? What would this program return under callby-value or call-by-reference?
(12)

Call-by-name
Uses actual textual substitution of arguments for parameters

Not used in modern programming languages due to great potential for name conflicts Example: the call swap (i, A[i]) would rewrite the body of swap before execution as: void swap (int x, int y) { int z; z = i; i = A[i]; A[i] = z; } What is the problem here?

(13)

Parameter passing conventions


C++ Default is call-by-value & operator provides call-by-reference Common Lisp Default is call-by-value Prolog Default is call-by-value

(14)

The formal parameter should specify type of subprogram for type checking (just like it specifies the type of the variable). Subprogram type is its protocol. Example: Pascal -procedure int(function fun (x:real) :real); begin funval := fun(2.5) end; Example: C++ -Only pointers can be passed. -Pointer contains protocol information.
(15)

Passing Subprograms as Parameters

Passing Subprograms (cont.)


What environment does passed subprogram execute within? Two choices: Shallow binding: -Within environment that subprogram is called within. -Consistent with dynamically scoped variables. Deep binding -Within environment that subprogram is declared within. -Consistent with lexically scoped variables.

(16)

Overloaded and Generic subprograms


Overloaded subprograms Multiple subprograms with the same name. Distinguished by different protocols. Each subprogram has its own implementation. A form of ad-hoc polymorphism. Generic subprograms: Single subprogram that can be instantiated for different types of parameters. Each subprogram shares a common shell of code. Differences are restricted to parameters. A form of parametric polymorphism

(17)

Compilation
Large system development must avoid total recompilation whenever possible.

Separate compilation: Compilation units can be compiled at different times, but recompilation is not independent if there is any use of one module by another. Requires access to other units during compilation. Independent compilation: Units can be compiled without access to others. (18) Errors are caught at link-time or run-time.

End of module

(19)

You might also like