1 subprograms fundamentals of subprograms design issues for subprograms –parameter-passing methods...

42
1 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

Upload: sydney-edwards

Post on 28-Dec-2015

238 views

Category:

Documents


1 download

TRANSCRIPT

1 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

2Subprograms

Levels of Control Flow:1. Among program units (this lecture) – Ch. 9

2. Among program statements – Ch. 8

3. Within expressions – Ch. 7

Two fundamental abstraction facilities1. Process abstraction – Ch. 9

2. Data abstraction – Ch. 11

3Fundamentals of Subprograms

General characteristics of subprograms:1. has a single entry point

2. caller is suspended during execution of the called subprogram

3. control always returns to the caller when the called subprogram’s execution terminates

Note: 2. does not apply to concurrent programs

4Definitions

• 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

5Actual/Formal Parameter Correspondence

1. Positional (most common)

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

3. Default valuese.g.

procedure SORT(LIST: LIST_TYPE; LENGTH: INTEGER := 100);

...

SORT(LIST => A);

6Subprograms (Functions and Procedures)

• Formal parameter (names)

• Actual parameters

• Passing– In

– Out

– In/OutSubprogram

Formal parameters

In

In/OutIn/Out

Out

Result/ReturnValue

Side EffectsInput/Output

ActualParameters

7Design Issues for Subprograms

1. Parameter passing– can subprograms be used?

2. Type checking of parameters– how strict?

3. Static or dynamic– local variable allocation

4. Nesting – can subprogram be defined in another subprogram definition?

5. Referencing environment – what can be accessed within the caller (sub)program?

6. Overloading – is it allowed?

7. Generic– can subprogram be generic?

8Parameter Passing Methods

• How can parameters be transmitted to and/or from the caller and the called subprogram?

1. Pass-by-value (in mode)

2. Pass-by-result (out mode)

3. Pass-by-value-result (in/out mode)

4. Pass-by-reference (in/out mode)

5. Pass-by-name

9Parameter Passing with Physical Moves (Copy)

Caller(sub(a, b, c))

In mode

Out mode

In/out mode

a

b

c

x

y

z

Callee(void sub (int x, int y, int z))

1

2

3

Return

Call

Call

Return

10Design 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

11Pass-by-value (In Mode)

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

b. Access path (reference)– Advantages & Disadvantages

• Opposite of above

12Pass-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!!!

13Pass-by-value-result (In/Out Mode)

• Combination of pass-by-value andpass-by-result

– Also called pass-by-copy

• Formal parameters have local storage

• Physical move both ways

• Advantages/Disadvantages:– Same as for pass-by-result– Same as for pass-by-value

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

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

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

17Pass-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

18Pass-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]);

19Multidimensional 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

20Parameter 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

21Parameter 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

22Parameter Passing Methods in PLs

• PHP– very similar to C#

• Perl– all actual parameters are implicitly placed in a

predefined array named @_

23Type 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

24Subprograms 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

25Referencing Environments

• What is the correct referencing environment of a subprogram that is passed as a parameter?

• Possibilities:

1. It is that of the subprogram that declared it• Deep binding• Most natural for static-scoped PLs

2. It is that of the subprogram that enacted it• Shallow binding• Most natural for dynamic-scoped PLs

3. It is that of the subprogram that passed it• Ad hoc binding (Has never been used)

26Referencing 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]

27Nested Subprogram Definitions

• Does the language allow them?

• Are there limits on nesting depth?

• How are non-local variables handled?– Static scope– Dynamic scope

28Design Issues Specific to Functions

1. Are side effects allowed?• Two-way parameters

• Ada does not allow them

• Non-local references • Allowed in all PLs

2. What types of return values are allowed?

29Return 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.

30Accessing Non-local Environments

• Non-local variables– variables that are visible but not declared in the

subprogram

• Global variables– variables that are visible in all subprograms

31Nonlocal 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

32Overloaded 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

33Generic Subprograms

• A generic or polymorphic subprogram– takes parameters of different types on different

activations, or– executes different code on different activations

• Overloaded subprograms offer– ad hoc polymorphism

• A subprogram where the type of a parameter is described by a type expression with a generic parameter – parametric polymorphism

34Generic 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);

35Generic 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);

36Generic 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);

37Parametric Polymorphism in C++

• C++ – Template functions– e.g.

template <class Type>

Type max(Type first, Type second) {

return first > second ? first : second;

}

38C++ 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

39Generics 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)??

40Generics in Java (cont.)

• Generic types can be restricted to subclassesclass UrlList<Element extends URL> {…}

• Generic types can be used in "for-each" clausefor (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 typesclass MyList<Element> extends ArrayList<Element> {…}

MyList<Integer> numbers = new MyList<Integer>()

numbers.add(313);

int sum = 0;

for (int number : numbers) {sum += number;}

41Generics 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 typeclass MyList<Element> extends ArrayList<Element> {

Element[] toArray() {

Element[] array = new Element[this.size()];

return array;

}

}

42Generic 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 callInteger [] array = {1, 2, 3, 4, 5, 6};int successor = succ(3, array);