200 likes | 411 Views
ICS 313: Programming Language Theory. Module 12: Subprograms. Objectives. Understand design issues in subprograms: parameter passing type checking overloading and parameterized procedures. Basic Subprogram Definitions. Subprogram definition: Describes the actions of the subprogram.
E N D
ICS 313:Programming Language Theory Module 12: Subprograms
Objectives • Understand design issues in subprograms: • parameter passing • type checking • overloading and parameterized procedures
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.
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
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.
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)
Design Issues for Subprograms • Local referencing environments • Parameter passing methods • Overloading • Separate compilation
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.
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.
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?
Call-by-reference: Swap.c 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;} • Swap is called with the address of locations. • Swap is declared to accept pointers.
Call-by-value-result pseudocode foo (x,y) { i = y;}pseudo main () { int i = 2; int j = 3; foo (i, j); return i;} • What does this program return? • What would this program return under call-by-value or call-by-reference?
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?
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
Passing Subprograms as Parameters • 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.
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.
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”
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. • Errors are caught at link-time or run-time.