SbSub-program d and Parameter Passingssamet/cps506/CPS 506 (Winter 2010 Ryerson... · • Local...

76
CPS 506 Comparative Programming L Languages Sb d Sub-program and Parameter Passing Parameter Passing

Transcript of SbSub-program d and Parameter Passingssamet/cps506/CPS 506 (Winter 2010 Ryerson... · • Local...

CPS 506Comparative Programming

LLanguagesS b d Sub-program and Parameter PassingParameter Passing

TopicsTopicsInt d ti n• Introduction

• Fundamentals of SubprogramsD si n Issu s f Subp ms• Design Issues for Subprograms

• Local Referencing EnvironmentsP r m t r P ssin M th ds• Parameter-Passing Methods

• Parameters That Are SubprogramsOverloaded Subpro rams• Overloaded Subprograms

• User-Defined Overloaded OperatorsGeneric Subprograms• Generic Subprograms

• Design Issues for FunctionsCo routines• Co-routines

2

Fundamentals of SubprogramsFundamentals of Subprograms

Th f d t l• Three fundamentals– Each subprogram has a single entry pointp g g y p

– The calling program is suspended during The calling program is suspended during execution of the called subprogram

– Control always returns to the caller h n th ll d s bp m’s x ti n when the called subprogram s execution

terminates

3

Basic DefinitionsBasic Definitions k d f b• Two kinds of subprograms

– Function– Procedure

• A subprogram definition describesA subprogram definition describes– The interface

The actions– The actions

• A subprogram call is an explicit request that the subprogram be executed

4

Basic DefinitionsBasic DefinitionsI P th f ti d fi iti t bl i ll th l • In Python, function definitions are executable; in all other languages, they are non-executable# map.pyd f ( f li t )def map( fun, list ):

nlist = [] for item in list:

nlist.append( fun( item ) ) pp ( ( ) )return nlist

# Make a sample test function def increment(x):

return x+1

# Test them out!# Test them out! map( increment, [1,2,3,4,5] ) # should return [2,3,4,5,6]

5

Basic DefinitionsBasic DefinitionsA b h d i th fi t t f th d fi iti i l di• A subprogram header is the first part of the definition, including

– Name– Kind of subprogram– Formal parameters

• The parameter profile (signature) of a subprogram is the p p ( g ) p g– Number– Order– and Types of its parametersand ypes of ts parameters

• The protocol of subprogram isParameter profile – Parameter profile

– and its Return type (if it is a function)

6

Basic Definitions (continued)Basic Definitions (continued)F ti d l ti i C d C ft ll d • Function declarations in C and C++ are often called prototypes

• A subprogram declaration provides the protocol, but not the body, of the subprogram

• A formal parameter is a dummy variable listed in the subprogram header and used in the subprogramsubprogram header and used in the subprogram

• An actual parameter represents a value or address used An actual parameter represents a value or address used in the subprogram call statement

7

Basic Definitions (continued)Basic Definitions (continued)

• Functions as first-class entitiesCan be stored in data structures–Can be stored in data structures

–Pass as parametersPass as parameters–Returned from functions–Lua (anonymous functions)function cube(x) return x * x * x endfunction cube(x) return x * x * x end

cube = function (x) return x * x * x end

8

Actual/Formal Parameter C dCorrespondence

• Positional– The binding of actual parameters to The binding of actual parameters to

formal parameters is by position: the fi t t l t i b d t th first actual parameter is bound to the first formal parameter and so forthp

– Safe and effectivell h l h h d – All the languages support this method

of parameter bindingp g9

Actual/Formal Parameter C dCorrespondence

K d• Keyword– The name of the formal parameter to which an

actual parameter is to be bound is specified with the actual parameter is to be bound is specified with the actual parameter

– Advantage: Parameters can appear in any order Advantage: Parameters can appear in any order, thereby avoiding parameter correspondence errors

– Disadvantage: User must know the formal gparameter’s names

– Python can use this type of parameter binding– Python, Ada, Fortran 95 can have both in one

function call

10

Formal Parameter Default ValuesFormal Parameter Default Values

I t i l ( C P th R b Ad PHP) f l • In certain languages (e.g., C++, Python, Ruby, Ada, PHP), formal parameters can have default values (if no actual parameter is passed)– In C++, default parameters must appear last because parameters

ll dare positionally associated

– C++float compute pay(float income float tax rate intfloat compute_pay(float income, float tax_rate, intexemptions = 1)

pay = compute_pay(20000.0, 0.15);

– PythonDef compute pay(income exemptions 1 tax rate)Def compute_pay(income, exemptions = 1, tax_rate)

pay = compute_pay(20000.0,tax_rate = 0.15)_ _

11

Formal Parameter Default Values (con’t)

V i bl b f t• Variable number of parameters– C# methods can accept a variable number of parameters as long as

they are of the same type—the corresponding formal parameter is d d b an array preceded by params

Public void DisplayList(params int[] list) {foreach (int next in list) {foreach (int next in list) {

Console.WriteLine(“Next value {0}”, next);}

}}

Myclass myObject = new Myclass;int[] myList = new int[6] {2,4,6,8,10,12};

myObject.DisplayList(myList);myObject.DisplayList(2,4,3*x-1,17);

12

Formal Parameter Default Values (con’t)

V i bl b f t• Variable number of parameters– In Ruby, the actual parameters are sent as elements of a

hash literal and the corresponding formal parameter is p g f m p mpreceded by an asterisk. (Details discussed in Ruby part)

list = [2,4,6,8]def tester(p1,p2,p3,*p4)(p ,p ,p , p )

…end…tester(‘first’, mon =>72, tue =>68, wed =>59, *list)

p1 is ‘first’p2 is {mon =>72, tue =>68, wed =>59}p3 is 2p4 is [4,6,8]

13

Formal Parameter Default Values (con’t)

V i bl b f t• Variable number of parameters– In Python, the actual is a list of values and the corresponding

formal parameter is a name with an asterisk (Details discussed in Python part)

def fun1(p1,p2,*p3,**p4)……

…Fun1(2,4,6,8, mon =72, tue =68, wed =59)

p1 is 2P2 is 4P2 is 4p3 is [6,8]

p4 is {‘mon’:72, ‘tue’:68, ‘wed’:59}

14

Ruby BlocksRuby Blocks• Ruby includes a number of iterator functions, which

are often used to process the elements of arrays

• Iterators are implemented with blocks, which can also Iterators are implemented with blocks, which can also be defined by applications

• Blocks are attached methods calls; they can have t (i ti l b ) th t d h parameters (in vertical bars); they are executed when

the method executes a yield statement

15

Ruby BlocksRuby Blocks# A method to compute and yield Fibonacci numbers up to # a limit

def fibonacci(last)

first, second = 1, 1

while first <= last

yield first

first, second = second, first + second

end

end

puts "Fibonacci numbers less than 100 are:"

fibonacci(100) {|num| print num, " "}

putsp

sum = 0

fibonacci(100) {|num| sum +=num}

putsputs

16

Procedures and FunctionsProcedures and Functions• There are two categories of subprogramsThere are two categories of subprograms

– Procedures are collection of statements that define parameterized computationsthat define parameterized computations

– Two ways of producing result• Variables that are not formal parameters

but are visible in both the procedure and the caller program unit

• Formal parameters that allow the transfer of data to the caller (call by reference parameters)

17

Procedures and Functions (con’t)

• ...– Functions structurally resemble procedures but are

semantically modeled on mathematical functionsThey are expected to produce no side effects• They are expected to produce no side effects

– No modification on the parameters– No modification on variables defined outside

• Pure functions return only a value• In practice, program functions have side effects

D fi d fi d t ( h )• Define user-defined operators (such as power)• Overload operators by defining function in Ada, Python,

Ruby, C++, and C# (discussed later)y, , ( )• void functions in C-based languages work like procedures

18

Design Issues for SubprogramsDesign Issues for Subprograms

l l bl d • Are local variables static or dynamic? • What parameter passing methods are provided?• Are parameter types checked?• If subprograms can be passed as parameters and p g p p

subprograms can be nested, what is the referencing environment of a passed subprogram?

• Can subprograms be overloaded?• Can subprogram be generic?u p g m g

19

Local Referencing EnvironmentsLocal Referencing Environments• Local variables can be stack-dynamic

– Bound to storage when the subprogram begins execution– Unbounded from storage when that execution terminatesg

– Advantages• Support for recursion

St f l l i h d ith th f i ti b ( t • Storage for locals is shared with those of inactive subprograms (great advantage for old computers with small size of memory)

– Disadvantages• Allocation/de-allocation, initialization time• Indirect addressing (access only during the execution)• Subprograms cannot be history sensitive• Subprograms cannot be history sensitive

– Writing a subprogram for generating random numbers

• Local variables can be static– Advantages and disadvantages are the opposite of those for stack-dynamic

local variables 20

Local Referencing Environments g(con’t)

• In C and C++, local variables are stack-dynamic unless specifically declared to be static

int adder(int list[], int listlen) {

static int sum = 0;

int count;

for (count=0;count < listlen;count++)

sum += list[count];

return sum;

}

• Java, C# and Ada have only stack-dynamic local variables21

Local Referencing Environments g(con’t)

• In Fortran 95 a subprogram can be explicitly specified to be recursive.

h l l bl k d b d f l– So the local variables are stack-dynamic by default– Force variables to be static using Save keyword

Recursive subroutine sub()

Integer :: CountInteger :: Count

Save, Real :: Sum

...

End Subroutine sub

• In Python methods, all local variables are stack-dynamic 22

Semantic Models of Parameter PassingSemantic Models of Parameter Passing

I d• In mode–No change is returned. Some g

parameters are just passed to the subprogram

• Out mode–No parameter is passed Just result No parameter is passed. Just result

is returned to the caller• Inout mode• Inout mode

23

Models of Parameter PassingModels of Parameter Passing

24

Pass by Value (In Mode)Pass-by-Value (In Mode)• The value of the actual parameter is used to initialize the he value of the actual parameter s used to n t al ze the

corresponding formal parameter– Normally implemented by copying

– Can be implemented by transmitting an access path but not recommended (enforcing write protection is not easy)

– Disadvantages (if by physical move): additional storage is required (stored twice) and the actual move can be costly (for large

)parameters)

– Disadvantages (if by access path method): must write-protect in g ( y p ) pthe called subprogram and accesses cost more (indirect addressing)

25

Pass by Result (Out Mode)Pass-by-Result (Out Mode)• When a parameter is passed by result, no value is transmitted p p y

to the subprogram; the corresponding formal parameter acts as a local variable; its value is transmitted to caller’s actual parameter when control is returned to the caller, by physical parameter when control is returned to the caller, by physical move

R i l i d i– Require extra storage location and copy operation

• Potential problemsPotential problems– sub(p1, p1); whichever formal parameter is copied back will

represent the current value of p1E l ti ti– Evaluation time

26

Pass by Result (Out Mode)Pass-by-Result (Out Mode)• C#C#

void Fixer(out int x,out int y) {x = 17;x 17;y =35;

}...f.Fixer(out a, out a);

----------------------void DoIt(out int x, int index) {

x = 17;index = 42;index = 42;

}...sub = 21;f.DoIt(list[sub], sub); 27

Pass-by-Value-Result y(in-out Mode)

A bi ti f b l • A combination of pass-by-value and pass-by-result

• Sometimes called pass-by-copy• Formal parameters have local • Formal parameters have local

storageDi d t• Disadvantages:–Those of pass-by-resultThose of pass by result–Those of pass-by-value

28

Pass-by-Reference (In-out Mode)Pass-by-Reference (In-out Mode)

P th• Pass an access path• Also called pass-by-sharing• Advantage: Passing process is efficient (no copying and no Advantage: Passing process is efficient (no copying and no

duplicated storage)• Disadvantages

– Slower accesses (compared to pass-by-value) to formal parameters

• Additional level of indirect addressingAdditional level of indirect addressing– Potentials for unwanted side effects (collisions)

• Unwanted aliases l l i bl ( d d bili d li bili )– Access to non-local variables (reduce readability and reliability)

29

Pass-by-Reference (In-out Mode)Pass-by-Reference (In-out Mode)

– C++• Collusion between actual parameterspvoid fun(int &first, int &second)fun(total total)fun(total, total)

first and second in fun will be aliases.• Collusion between array elementsfun(list[i], list[j])jfun1(list[i], list)

30

Pass-by-Reference (In-out Mode)Pass-by-Reference (In-out Mode)

– C++• Collusion between formal parameters and non-local variables that are visible

int * global;g

void main(){

...

sub(global);

...

}

void sub(int * param) {

...

}

param and global are aliases.All these possible aliasing situations are eliminated if pass-by-value-result is All these possible aliasing situations are eliminated if pass by value result is

used.31

Pass-by-Name (In-out Mode)Pass-by-Name (In-out Mode)• By textual substitutionBy textual substitution• Formals are bound to an access method at the time of the call, but

actual binding to a value or address takes place at the time of a reference or assignment

• Allows flexibility in late bindingAl l• Algolprocedure double(x);

real x;begin

x := x * 2x := x 2end;

double(C[j]) is interpreted as

C[j] := C[j] * 2.

• Usage– Compile time for macro– Generic subprograms in C++ and Ada

32

Implementing Parameter-Passing Methods

l • In most language parameter communication takes place through the run-time stack

• Pass-by-reference are the simplest to implement; only an address is placed in the mp m ; y pstack

• A subtle but fatal error can occur with • A subtle but fatal error can occur with pass-by-reference and pass-by-value-result: a formal parameter corresponding to a a formal parameter corresponding to a constant can mistakenly be changed

33

Function header: void sub(int a, int b, int c, int d)

Function call in main: sub(w,x,y,z)

(pass w by value, x by result, y by value-result, z by reference)

34

Parameter Passing Methods of Major Languages

• CC– Pass-by-value– Pass-by-reference is achieved by using pointers as Pass by reference is achieved by using pointers as

parameters• Java

– All parameters are passed by value– Object parameters are passed by referencej p p y

• Ada– Three semantics modes of parameter transmissionp

• in, out, in-out• in is the default mode

35

Parameter Passing Methods of Major L ( ti d)Languages (continued)

F t 95• Fortran 95- Parameters can be declared to be in, out, or inout mode C#• C#- Default method: pass-by-value–Pass-by-reference is specified by

preceding both a formal parameter preceding both a formal parameter and its actual parameter with ref

36

Parameter Passing Methods of Major Languages

• Adaprocedure Adder( A : in out Integer;

B : in Integer;C : out Float)C : out Float)

• Fortran 95Subroutine Adder(A, B, C)

I t I t t(I t) AInteger,Intent(Inout) :: AInteger,Intent(In) :: BInteger,Intent(Out) :: C

• C#void sumer(ref int oldSum, int newOne, out nextOne) {...}...sumer(ref sum, newValue, nextValue);

37

Parameter Passing Methods of Major L ( ti d)Languages (continued)

PHP i il t C#• PHP: very similar to C#– Pass-by-reference by preceding &y y p g

• Perl: all actual parameters are implicitly l d i d fi d d @placed in a predefined array named @_

• Python and Ruby use pass-by-assignment y y p y g m(all data values are objects), in effect is a pass by referencea pass-by-reference

38

Type Checking ParametersType Checking Parameters• Considered very important for reliability• FORTRAN 77 and original C: none• C89

double sin(x)

double x;

{...}

0rdouble sin (double x)

{...}

39

Type Checking ParametersType Checking Parameters• Pascal, FORTRAN 90, Java, and Ada: it is always

required• ANSI C and C++: choice is made by the user

– PrototypesPrototypesdouble sin(double x) {...}

C99 d C f l t i t t f• C99 and C++: formal parameters in prototype form– Type checking could be avoided for some parameters by

usin “ ”using ...”

int printf(const char* format_string, ...)

At least one parameter40

Type Checking Parameters yp g(con’t)

• Relatively new languages Perl, JavaScript and PHP do not require JavaScript, and PHP do not require type checkingyp g

• In Python and Ruby, variables do not have types (objects do), so parameter type checking is not possibletype checking is not possible

41

Multidimensional Arrays as ParametersMultidimensional Arrays as Parameters

• If a multidimensional array is passed to a subprogram and the passed to a subprogram and the subprogram is separately

l d h l d compiled, the compiler needs to know the declared size of that know the declared size of that array to build the storage mapping f ifunction

42

Multidimensional Arrays as Parameters: C and C++

• Programmer is required to include the declared sizes of all but g qthe first subscript in the actual parameter

• Storage-mapping functiondd ( t[I j]) dd ( t[0 0]) I *address(mat[I,j]) = address(mat[0,0]) + I *

number_of_columns + j

void fun(int matrix[][10]) {...}void main() {

int mat[5][10];int mat[5][10]; ... Fun(mat);

}... }

• Disallows writing flexible subprograms

43

Multidimensional Arrays as Parameters: Java and C#

• Arrays are objects; they are all single-dimensioned but the elements single dimensioned, but the elements can be arrays

• Each array inherits a named constant ( i J i C#) h i (length in Java, Length in C#) that is set to the length of the array when set to the length of the array when the array object is created

44

Design Considerations for Parameter Passing

d• Two important considerations– Efficiencyy– One-way or two-way data transfer

• But the above considerations are in conflict• But the above considerations are in conflict– Good programming suggest limited access

bl h h to variables, which means one-way whenever possible

– But pass-by-reference is more efficient to pass structures of significant sizep f g f

45

Parameters that are Subprogram p gNames

It i ti i t t • It is sometimes convenient to pass subprogram names as parametersp g p

• Issues:A t t h k d?– Are parameter types checked?• C and C++: functions cannot be passed as

parameters but pointers to functions can be passed and their types include the types f h b of the parameters, so parameters can be

type checked

46

Parameters that are Subprogram N P t T Ch kiNames: Parameter Type Checking

Ada does not allow subprogram • Ada does not allow subprogram parameters; an alternative is pprovided via Ada’s generic facility (discussed later)(discussed later)

• Java does not allow method names to be passed as parameters

47

Parameters that are Subprogram p gNames

I ( ’t)• Issues: (con’t)– Referencing environment for Referencing environment for

executing the passed subprogramI l th t ll t d • In languages that allow nested subprograms

• Three choices

48

Parameters that are Subprogram N R f i E i tNames: Referencing Environmenth h• Three choices

– Shallow binding: The environment of the call t t t th t t th d bstatement that enacts the passed subprogram

- Most natural for dynamic-scoped languages

– Deep binding: The environment of the definition of the passed subprogramthe passed subprogram- Most natural for static-scoped languages

– Ad hoc binding: The environment of the call statement that passed the subprogram

49

Parameters that are Subprogram N R f i E i tNames: Referencing Environment

function sub1() { Sh ll bindin() {

var x;

function sub2() {

– Shallow binding– Output from alert(x) is 4

alert(x); };

function sub3() {

var x; – Deep binding;

x=3;

sub4(sub2); };

– Output from alert(x) is 1

function sub4(subx) {

var x;

x=4;

– Ad hoc binding– Output from alert(x) is 3;

subx(); };

x=1;

Output from alert(x) is 3

sub3();

}; 50

Overloaded SubprogramsOverloaded Subprograms• An overloaded subprogram is one that

has the same name as another has the same name as another subprogram in the same referencing environmentenvironment–Every version of an overloaded y

subprogram has a unique protocolC J C# d Ad i l d • C++, Java, C#, and Ada include predefined overloaded subprograms p p g

51

User-Defined Overloaded OperatorsUser-Defined Overloaded Operators

O t b l d d i Ad C P th d R b• Operators can be overloaded in Ada, C++, Python, and Ruby• An Ada example (dot product)

function "*" (A,B: in Vec_Type): return Integer isSum: Integer := 0;beginfor Index in A'range loopSum := Sum + A(Index) * B(Index)

end loopreturn sum;

end "*";…c = a * b; -- a, b, and c are of type Vec Typeyp _ yp

• C++ prototype for the same operatorint operator * (const vector &a, const vector &b, int len);

52

Overloaded SubprogramsOverloaded SubprogramsC ( t l di )• C++ (operator overloading)class complex{

float re, im;

complex operator +(complex d){

complex ans;complex ans;

ans.re = d.re+re;

ans.im = d.im+im;

return ans;

}

}}

complex c,d;

53

c = c + d;

Overloaded SubprogramsOverloaded SubprogramsC• C++class C {

int x;

public static int sum(int v1, int v2) {

return v1 + v2;return v1 + v2;

}

public int sum(int v3) {

return x + v3;

}

}}

54

Overloaded SubprogramsOverloaded SubprogramsC ( bi b ll)• C++ (ambiguous subprogram call)void fun(float b = 0.0);

void fun();

...

fun();

55

Overloaded SubprogramsOverloaded Subprograms• In Ada, the return type of an

overloaded function can be used to overloaded function can be used to disambiguate calls (thus two overloaded functions can have the overloaded functions can have the same parameters)

• Ada, Java, C++, and C# allow users to write multiple versions of write multiple versions of subprograms with the same name

56

Overloaded SubprogramsOverloaded Subprograms• Java• Javapublic class OverloadExample {

public void print(int a) { System out println(a); }public void print(int a) { System.out.println(a); }

public void print(String a) { System.out.println(a); }

void test() {

int i = 1;

String s = “Hi”;

print(i);print(i);

print(s);

}

public static void main(String[] args) {

OverloadExample p = new OverloadExample();

p test();

57

p.test();

}

}

Generic SubprogramsGeneric Subprograms• Scenario

– Generic Sort subprogram to sort arrays of d ff l different element types

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

• Overloaded subprograms provide ad hoc polymorphismpolymorphism

58

Generic Subprograms (c ntinu d)Generic Subprograms (continued)d• Ada

– Versions of a generic subprogram are Versions of a generic subprogram are created by the compiler when explicitly instantiated by a declaration statementinstantiated by a declaration statement

– Generic subprograms are preceded by a

generic clause that lists the generic i bl hi h b t th variables, which can be types on other

subprograms59

Generic Subprograms (c ntinu d)Generic Subprograms (continued)• AdaAda

generic

type Index_type is (<>);

type Element Type is private;type e e t_ ype s p ate;

type Vector is array (Integer range <>) of Element_Type;

procedure Generic_Sort(List : in out Vector);

procedure Generic_Sort(List : in out Vector) is

Temp : Element_Type;

begin

for Top in List ...

f B tt i I d Tfor Bottom in Index_Type ...

if List(Top) > List(Bottom) then

Temp := List(Top);

List(Top) := List(Bottom);( p) ( );

List(Bottom) := Temp;

end if;

end loop;

60

end loop;

end Generic_Sort;

Generic Subprograms (c ntinu d)Generic Subprograms (continued)• No code is generated by compiler unless it is • No code is generated by compiler unless it is

instantiated for some type• Array type: Int_Array• Elements: IntegerElements Integer• Subscripts: Integerprocedure Integer Sort is new Generic Sort(procedure Integer_Sort is new Generic_Sort(

Index_Type => Integer,

Element_Type => Integer,

Vector => Int_Array);

61

Generic Subprograms (c ntinu d)Generic Subprograms (continued)• Using generic subprogram in Ada to pass subprogram as Using generic subprogram in Ada to pass subprogram as

parametergeneric

with function Fun(X: Float) return Float;

procedure Integrate(Lowerbd : in Float;

Upperbd : in Float;

Result : in Float);Result : in Float);

procedure Integrate(Lowerbd : in Float;

Upperbd : in Float;

Result : in Float) is

FunVal : Float;

begin

...

FunVal := Fun(Lowerbd);

...

end Integrate

62

procedure Integrate_Fun1 is new Integrate(Fun => Fun1);

Generic Subprograms (c ntinu d)Generic Subprograms (continued)• C++

– Versions of a generic subprogram are g p gcreated implicitly when the subprogram is named in a call or when its address is mtaken with the & operator

– Generic subprograms are preceded by a – Generic subprograms are preceded by a template clause that lists the generic variables which can be type names or variables, which can be type names or class names

63

Generic Subprograms (c ntinu d)Generic Subprograms (continued)• C++C++

template <class Type>

Type max(Type first, Type second) {

return first > second ? First : second;

}

int a, b, c;

char d, e, f;

...

c = max(a, b);

f = max(d, e);

• Why this is better than writing a macro? Like this:Why this is better than writing a macro? Like this:#define max(a,b) ((a) > (b)) ? (a) : (b)

64

Generic Subprograms (c ntinu d)Generic Subprograms (continued)• C++ generic sort subprogramC++ generic sort subprogram

template <class Type>

void generic_sort(Type list[], int len) {

int top, bottom;

Type temp;

for (top = 0; top < len – 2; top++)

for (bottom = top + 1; bottom < len – 1; bottom++)

if (list[top] > list[bottom]) {

temp = list[top];

list[top] = list[bottom];

list[bottom] = temp;list[bottom] = temp;

}

}

Float flt_list[100];

...

generic_sort(flt_list, 100);

65

Generic Subprograms (con’t)Generic Subprograms (con t)• Java 5.0

– Differences between generics in Java 5.0 and those of C++ and Ada:

• Generic parameters in Java 5.0 must be classes• Java 5.0 generic methods are instantiated just once as

truly generic methods (by casting the return value)R t i ti b ifi d th f l th t • Restrictions can be specified on the range of classes that can be passed to the generic method as generic parameters (bounds)parameters (bounds)

• Wildcard types of generic parameters

66

Generic Subprograms (c ntinu d)Generic Subprograms (continued)• Java 5 0 generic methodJava 5.0 generic method

public static <T> T doIt(T[] list) {

...

}}

doIt<String>(myList);

• Java 5.0 wildcard typevoid printCollection(Collection<?> c) {

for (object e: c) {

System.out.println(e);

}

}

• Java 5 0 bounded wildcard type• Java 5.0 bounded wildcard typepublic void drawAll(ArrayList<? Extends Shape> things)

To draw any object whose type is a subclass of Shape

67

Generic Subprograms (con’t)Generic Subprograms (con t)

• C# 2005Supports generic methods that are –Supports generic methods that are similar to those of Java 5.0

Differences–Differences• No support for wildcard types• Actual type parameters in call can be

omitted if compiler can infer thatp68

Generic Subprograms (c ntinu d)Generic Subprograms (continued)• C# 2005C# 2005

class MyClass {

public static T DoIt<T>(T p1) {

...

}

}

Which could be calledint myInt = MyClass.DoIt(17); //calls DoIt<int>int myInt MyClass.DoIt(17); //calls DoIt<int>

String myStr =MyClass.DoIt(‘apples’); //calls DoIt<String>

69

Design Issues for FunctionsDesign Issues for Functions• Are side effects allowed?• Are side effects allowed?

– Parameters should always be in-mode to reduce side effect (like Ada)reduce side effect (like Ada)

• What types of return values are allowed?M t i ti l t i t th – Most imperative languages restrict the return typesC ll t t d f ti – C allows any type except arrays and functions (handled by pointer type return values)C i lik C b l ll d fi d – C++ is like C but also allows user-defined types or classes to be returned

70

Design Issues for FunctionsDesign Issues for Functions• What types of return values are allowed?• What types of return values are allowed?

– Ada subprograms can return any type (but Ada subprograms are not types so they Ada subprograms are not types, so they cannot be returned)Java and C# methods can return any type – Java and C# methods can return any type (but because methods are not types, they cannot be returned)cannot be returned)

– Python and Ruby treat methods as first-class objects so they can be returned as well as objects, so they can be returned, as well as any other class

– Lua allows functions to return multiple values– Lua allows functions to return multiple values71

Co routinesCo-routinesA ti i b th t h • A co-routine is a subprogram that has multiple entries and controls them multiple entries and controls them itself – supported directly in Lua

• Also called symmetric control: caller d ll d ti and called coroutines are on a more

equal basisequal basis• A co-routine call is named a resume

72

Co routines (con’t)Co-routines (con t)• The first resume of a co routine is to its • The first resume of a co-routine is to its

beginning, but subsequent calls enter at the point just after the last executed point just after the last executed statement in the coroutineC dl h h • Co-routines repeatedly resume each other, possibly forever

• Co-routines provide quasi-concurrent execution of program units (the co-f p g (routines); their execution is interleaved, but not overlappedbut not overlapped

73

Co-routines Illustrated: Possible E ti C t lExecution Controls

74

Co-routines Illustrated: Possible E ti C t lExecution Controls

75

Co-routines Illustrated: Possible Execution Controls with Loops

76