430 likes | 966 Views
Subprograms. Fundamentals of subprograms Design issues for subprograms Parameter-passing methods Type checking of parameters Static or dynamic storage Local referencing environments Nesting of subprogram definitions Overloaded subprograms Generic subprograms Design issues for functions
E N D
Subprograms • Fundamentals of subprograms • Design issues for subprograms • Parameter-passing methods • Type checking of parameters • Static or dynamic storage • Local referencing environments • Nesting of subprogram definitions • Overloaded subprograms • Generic subprograms • Design issues for functions • Subprograms as parameters • User-defined overloaded operators
Subprograms Levels of Control Flow: • Among program units (this lecture) – Ch. 9 • Among program statements – Ch. 8 • Within expressions – Ch. 7 Two fundamental abstraction facilities • Process abstraction – Ch. 9 • Data abstraction – Ch. 11
Fundamentals of Subprograms General characteristics of subprograms: • has a single entry point • caller is suspendedduring execution of the called subprogram • control always returns to the caller when the called subprogram’s execution terminates Note: 2. does not apply to concurrent programs
Definitions • Subprogram • Description of the subprogram's actions • Subprogram call • Explicit request to execute the subprogram • Subprogram header • the 1st part of subprogram's definition: its name, type, and formal parameters • Parameter profile • the number, order, and types of the formal parameters • Subprogram protocol • its parameter profile plus, if it is a function, its return type • Subprogram declaration • its name and its protocol, but not the body • Formal parameter • “dummy” variable listed in the parameter profile and used in the subprogram body • Actual parameter • value or address used in the subprogram call statement
Actual/Formal Parameter Correspondence • Positional (most common) • With keyword • e.g. • SORT(LIST => A, LENGTH => N); • Advantage: order is irrelevant • Disadvantages • user must know the formal parameter’s names • less readable and writeable • Default values e.g. procedure SORT(LIST: LIST_TYPE; LENGTH: INTEGER := 100); ... SORT(LIST => A);
Subprograms (Functions and Procedures) • Formal parameter (names) • Actual parameters • Passing • In • Out • In/Out Subprogram Result/ Return Value In Formal parameters In/Out In/Out Out Actual Parameters Input/Output Side Effects
Design Issues for Subprograms • Parameter passing • can subprograms be used? • Type checking of parameters • how strict? • Static or dynamic • local variable allocation • Nesting • can subprogram be defined in another subprogram definition? • Referencing environment • what can be accessed within the caller (sub)program? • Overloading • is it allowed? • Generic • can subprogram be generic?
Parameter Passing Methods • How can parameters be transmitted to and/or from the caller and the called subprogram? • Pass-by-value (in mode) • Pass-by-result (out mode) • Pass-by-value-result (in/out mode) • Pass-by-reference (in/out mode) • Pass-by-name
Parameter Passing with Physical Moves (Copy) Caller (sub(a, b, c)) Callee(void sub (int x, int y, int z)) Call 1 a x Return In mode 2 b y Call Out mode c z 3 In/out mode Return
Design Choices for Parameter Passing • Efficiency vs. style and reliability • One-way or two-way parameters? • Good programming style • limited access to variables, e.g. one-way whenever possible • Efficiency • pass by reference is the fastest way to pass big structures, however the access is then a bit slower • Note the conflict! • Also style • functions should minimize pass by reference parameters
Pass-by-value (In Mode) • Physical move (copy) • Advantages • No need to write-protect in the called subprogram • Accesses cost lower (no indirect addressing) • Disadvantages • Requires more storage (duplicated space) • Cost of the moves (if the parameter is large) • Access path (reference) • Advantages & Disadvantages • Opposite of above
Pass-by-result (Out Mode) • No value is transmitted to the subprogram • Local’s value is passed back to the caller • Physical move is usually used • Disadvantages: • If value is passed, time and space • Order dependence may be a problem • e.g. procedure sub(y: int, z: int); ... sub(x, x); • Value of x depends on order of assignments in the callee!!!
Pass-by-value-result (In/Out Mode) • Combination of pass-by-value andpass-by-result • Also called pass-by-copy • Formal parameters have local storage • Physicalmove both ways • Advantages/Disadvantages: • Same as for pass-by-result • Same as for pass-by-value
Pass-by-Reference (In/Out Mode) • Pass an access path (reference) • Also called pass-by-sharing • Advantage • Efficient - no copying and no duplicated storage • Disadvantages • Slower accesses to formal parameters (than in pass-by value) • Potential for unwanted side effects • Allows aliasing • See next slide
Pass-by-Reference Aliasing • Actual parameter collisions: e.g. procedure sub(a: int, b: int); ... sub(x, x); • Array element collisions: e.g. • sub(a[i], a[j]); /* if i = j */ • Also, • sub2(a, a[i]); /* (different one) */ • Collision between formal parameters and global variables
Pass-by-Reference Problems • Root cause • Subprogram has more access to non-locals than necessary • Reference to the memory location allows side effects • Pass-by-value-result solves this • It does not allow these aliases • has other problems
Pass-by-Name (In/Out Mode) • By textual substitution • Formal parameters are bound to an access method at the time of the call • Actual binding to a value or address takes place at the time of a reference or assignment • Advantage • Flexibility in late binding • Disadvantages • Reliability: weird semantics
Pass-by-name Semantics • If actual is a scalar variable, it is pass-by-reference • If actual is a constant expression, it is pass-by-value • If actual is an array element, it is like nothing else • e.g. procedure sub1(x: int; y: int); begin x := 1; y := 2; x := 2; y := 3; end; sub1(i, a[i]);
Multidimensional Arrays as Parameters • If a multidimensional array is passed-by-value in a separately compiled subprogram • The compiler needs to know the declared size of that array to build the storage mapping function
Parameter Passing Methods of Major Languages • Fortran • Always used the in/out semantics model • Before Fortran 77: pass-by-reference • Fortran 77 and later: scalar variables are often passed by value result • C • Pass-by-value • Pass-by-reference is achieved by using pointers as parameters • C++ • A special pointer type called reference type for pass-by-reference • Java • All primitive type parameters are passed by value • Object parameters are passed by reference
Parameter Passing Methods in PLs (cont.) • Ada • Three semantics parameter modes • in • can be referenced but not assigned • out • can be assigned but not referenced • in/out • can be referenced and assigned • in is the default mode • C# • Pass-by-value is default • Pass-by-reference • ref must precede both the formal and its actual parameter
Parameter Passing Methods in PLs • PHP • very similar to C# • Perl • all actual parameters are implicitly placed in a predefined array named @_
Type Checking of Parameters • Very important for reliability • Which errors are caught by type checking? • FORTRAN 77 and original C • No type checking • Pascal, FORTRAN 90, Java, and Ada • Type checking is always done • ANSI C and C++, Lisp • User can avoid type checking
Subprograms As Parameters • Are subprograms allowed as parameters? • if so, are types checked? • Early Pascal and FORTRAN 77 • no • Later versions of Pascal and FORTRAN 90 • yes • C and C++ • pass pointers to functions • parameters can be type checked • Java, Ada, Perl • no
Referencing Environments • What is the correct referencing environment of a subprogram that is passed as a parameter? • Possibilities: • It is that of the subprogram that declared it • Deep binding • Most natural for static-scopedPLs • It is that of the subprogram that enacted it • Shallow binding • Most natural for dynamic-scopedPLs • It is that of the subprogram that passed it • Ad hoc binding (Has never been used)
Referencing Environments (cont.) Example: function sub1() { var x; function sub2() { alert(x); } function sub3() { var x=3; call sub4(sub2); } function sub4(subx) { var x=4; call subx(); } x=1; call sub3; } • What is the referencing environment of sub2 when it is called in sub4? • Shallow binding => sub2 <- sub4 <- sub3 <- sub1 [output = 4] • Deep binding => sub2 <- sub1 [output = 1] • Ad-hoc binding => sub3 local x [output = 3]
Nested Subprogram Definitions • Does the language allow them? • Are there limits on nesting depth? • How are non-local variables handled? • Static scope • Dynamic scope
Design Issues Specific to Functions • Are side effects allowed? • Two-way parameters • Ada does not allow them • Non-local references • Allowed in all PLs • What types of return values are allowed?
Return Types of Functions in PLs • FORTRAN, Pascal • only simple types • C • any type except functions and arrays • Ada • any type (but subprograms are not types) • C++ and Java • like C, but also allow classes to be returned • Common Lisp • any type, function, class, object, structure, closure, etc.
Accessing Non-local Environments • Non-local variables • variables that are visible but not declared inthe subprogram • Global variables • variables that are visible in all subprograms
Nonlocal Environments in PL • FORTRAN COMMON blocks • The only way in pre-90 FORTRAN to access nonlocal variables • Can be used to share data or share storage • Static scoping • discussed already • External declarations - C • Subprograms are not nested • Globals are created by external declarations • they are simply defined outside any function • Access is by either implicit or explicit declaration • Declarations give types to externally defined variables • External modules - Ada • More in Chapter 11 • 5. Dynamic Scope – Common Lisp
Overloaded Subprograms • Overloaded subprogram • has the same name as another subprogram in the same referencing environment • C++, Java and Ada • have built-in subprogram overloading • users can write their own overloaded subprograms
Generic Subprograms • A generic or polymorphicsubprogram • takes parameters of different types on different activations, or • executes different code on different activations • Overloaded subprograms offer • adhocpolymorphism • A subprogram where the type of a parameter is described by a type expression with a generic parameter • parametric polymorphism
Generic Subprograms in Ada • Ada • types, subscript ranges, constant values, etc., can be generic in subprograms, packages • e.g. generic type element is private; type vector is array (INTEGER range <>) of element; procedure Generic_Sort (list: in out vector);
Generic Subprograms in Ada (cont.) procedure Generic_Sort (list : in out vector) is temp: element; begin for i in list'FIRST.. i'PRED(list'LAST) loop for j in i'SUCC(i)..list'LAST loop if list(i) > list(j) then temp := list(i); list(i) := list(j); list(j) := temp; end if; end loop; -- for j end loop; --for i End Generic_Sort procedure Integer_Sort is new Generic_Sort (element => INTEGER; vector => INTEGER_ARRAY);
Generic Subprograms Parameters in Ada • Ada generics can be used for parameters that are subprograms • Note: the generic part is a subprogram • Example: generic with function fun(x: FLOAT) return FLOAT; procedure integrate (from: in FLOAT; to: in FLOAT; result: out FLOAT) is x: FLOAT; begin ... x := fun(from); ... end; integrate_fun is new integrate(fun => my_fun);
Parametric Polymorphism in C++ • C++ • Template functions • e.g. template <class Type> Type max(Type first, Type second) { return first > second ? first : second; }
C++ Template Functions • Template functions are instantiated implicitly when • the function is named in a call, or • its address is taken with the & operator • Example: template <class Type> void generic_sort(Type list[], int len) { int i, j; Type temp; for (i = 0; i < len - 2; i++) for (j = i + 1; j < len - 1; j++) { if (list[i] > list[j]) { temp = list [i]; list[i] = list[j]; list[j] = temp; } //** end if } //** end for j } //** end for i } //** end of generic_sort … float number_list[100]; generic_sort(number_list, 100); // Implicit instantiation
Generics in Java • Java provides generic types • Syntax: • class_name<generic_class {, generic_class}*> e.g.: class MyList<Element> extends ArrayList<Element> {…} e.g.: class MyMap<Key,Value> extends HashMap<Key,Value> {…} • Types substituted when used: e.g.: MyList<String> courses = new MyList<String>(); e.g.: MyMap<String,String> table = new MyMap<String,String>(); • Type-checking works! courses.add("ics313"); // ok courses.add(313); // incorrect, 313 is not String • No casts needed! String course = courses.get(0); //(String)courses.get(0)??
Generics in Java (cont.) • Generic types can be restricted to subclasses class UrlList<Element extends URL> {…} • Generic types can be used in "for-each" clause for (String course : courses) { System.out.println (course.toUpperCase()); } • Generic types are used in standard libraries • Collections: • List, ArrayList, Map, Set • Primitive types can be substituted for generic types class MyList<Element> extends ArrayList<Element> {…} MyList<Integer> numbers = new MyList<Integer>() numbers.add(313); int sum = 0; for (int number : numbers) {sum += number;}
Generics in Java: Parameters, Return Type • Generic types can be used • as formal parameters class MyList<Element> extends ArrayList<Element> { int occurences(Element element) { … if (element.equals(this)) sum++; … } } • even as return type class MyList<Element> extends ArrayList<Element> { Element[] toArray() { Element[] array = new Element[this.size()]; … return array; } }
Generic Methods in Java • Method can be made depend on a generic type • Generic type precedes method's return type • Generic type can be used to • As the return type • To declare formal parameters • To declare local variables • E.g. <T> T succ(T value, T [] array) { T element = null; ... return element; } • Actual type is inferred from the call Integer [] array = {1, 2, 3, 4, 5, 6}; int successor = succ(3, array);