1 / 19

ICS 313: Programming Language Theory

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.

ulla-burt
Download Presentation

ICS 313: Programming Language Theory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICS 313:Programming Language Theory Module 12: Subprograms

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

  3. 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.

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

  5. 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.

  6. 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)

  7. Design Issues for Subprograms • Local referencing environments • Parameter passing methods • Overloading • Separate compilation

  8. 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.

  9. 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.

  10. 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?

  11. 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.

  12. 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?

  13. 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?

  14. 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

  15. 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.

  16. 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.

  17. 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”

  18. 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.

  19. End of module

More Related