cps 506 comparative programming languages

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

Upload: nerina

Post on 24-Feb-2016

50 views

Category:

Documents


0 download

DESCRIPTION

CPS 506 Comparative Programming Languages. Sub-program and Parameter Passing. Topics. Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters That Are Subprograms Overloaded Subprograms - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CPS  506 Comparative Programming Languages

CPS 506Comparative Programming

LanguagesSub-program and Parameter Passing

Page 2: CPS  506 Comparative Programming Languages

Topics• Introduction• Fundamentals of Subprograms• Design Issues for Subprograms• Local Referencing Environments• Parameter-Passing Methods• Parameters That Are Subprograms• Overloaded Subprograms• User-Defined Overloaded Operators• Generic Subprograms• Design Issues for Functions• Co-routines

Page 3: CPS  506 Comparative Programming Languages

Fundamentals of Subprograms

• Three fundamentals– Each subprogram has a single entry point

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

–Control always returns to the caller when the called subprogram’s execution terminates

Page 4: CPS  506 Comparative Programming Languages

Basic Definitions• Two kinds of subprograms– Function– Procedure

• A subprogram definition describes– The interface– The actions

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

Page 5: CPS  506 Comparative Programming Languages

Basic Definitions• In Python, function definitions are executable; in all other languages,

they are non-executable# map.py

def map( fun, list ):

nlist = []

for item in list:

nlist.append( fun( item ) )

return nlist

# Make a sample test function

def increment(x):

return x+1

# Test them out!

map( increment, [1,2,3,4,5] )

# should return [2,3,4,5,6]

Page 6: CPS  506 Comparative Programming Languages

Basic Definitions• 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 – Number– Order– and Types of its parameters

• The protocol of subprogram is– Parameter profile – and its Return type (if it is a function)

Page 7: CPS  506 Comparative Programming Languages

Basic Definitions (continued)

• 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 subprogram

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

Page 8: CPS  506 Comparative Programming Languages

Basic Definitions (continued)

• Functions as first-class entities–Can be stored in data structures–Pass as parameters–Returned from functions–Lua (anonymous functions)function cube(x) return x * x * x endcube = function (x) return x * x * x end

Page 9: CPS  506 Comparative Programming Languages

Actual/Formal Parameter Correspondence

• Positional–The binding of actual parameters to

formal parameters is by position: the first actual parameter is bound to the first formal parameter and so forth–Safe and effective–All the languages support this

method of parameter binding

Page 10: CPS  506 Comparative Programming Languages

Actual/Formal Parameter Correspondence

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

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

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

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

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

function call

Page 11: CPS  506 Comparative Programming Languages

Formal Parameter Default Values

• 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 are

positionally associated– C++float compute_pay(float income, float tax_rate, int exemptions = 1)

pay = compute_pay(20000.0, 0.15);

– PythonDef compute_pay(income, exemptions = 1, tax_rate)

pay = compute_pay(20000.0,tax_rate = 0.15)

Page 12: CPS  506 Comparative Programming Languages

Formal Parameter Default Values (con’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 an array preceded by params

Public void DisplayList(params int[] 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);

Page 13: CPS  506 Comparative Programming Languages

Formal Parameter Default Values (con’t)

• Variable number of parameters– In Ruby, the actual parameters are sent as elements of a hash

literal and the corresponding formal parameter is preceded by an asterisk. (Details discussed in Ruby part)

list = [2,4,6,8]

def tester(p1,p2,p3,*p4)

end

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

p1 is ‘first’

p2 is {mon =>72, tue =>68, wed =>59}

p3 is 2

p4 is [4,6,8]

Page 14: CPS  506 Comparative Programming Languages

Formal Parameter Default Values (con’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 2

P2 is 4

p3 is [6,8]

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

Page 15: CPS  506 Comparative Programming Languages

Ruby 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 be defined by applications

• Blocks are attached methods calls; they can have parameters (in vertical bars); they are executed when the method executes a yield statement

Page 16: CPS  506 Comparative Programming Languages

Ruby 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, " "}

puts

sum = 0

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

puts

Page 17: CPS  506 Comparative Programming Languages

Procedures and Functions

• There are two categories of subprograms– Procedures are collection of statements that

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)

Page 18: CPS  506 Comparative Programming Languages

Procedures and Functions (con’t)

• ...– Functions structurally resemble procedures but are

semantically modeled on mathematical functions• 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• Define user-defined operators (such as power)• Overload operators by defining function in Ada, Python,

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

Page 19: CPS  506 Comparative Programming Languages

Design Issues for Subprograms

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

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

• Can subprograms be overloaded?• Can subprogram be generic?

Page 20: CPS  506 Comparative Programming Languages

Local Referencing Environments

• Local variables can be stack-dynamic– Bound to storage when the subprogram begins execution– Unbounded from storage when that execution terminates– Advantages

• Support for recursion• 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

– 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

Page 21: CPS  506 Comparative Programming Languages

Local Referencing Environments (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 variables

Page 22: CPS  506 Comparative Programming Languages

Local Referencing Environments (con’t)

• In Fortran 95 a subprogram can be explicitly specified to be recursive. – So the local variables are stack-dynamic by default– Force variables to be static using Save keyword

Recursive subroutine sub()

Integer :: Count

Save, Real :: Sum

...

End Subroutine sub

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

Page 23: CPS  506 Comparative Programming Languages

Semantic Models of Parameter Passing

• In mode–No change is returned. Some

parameters are just passed to the subprogram

• Out mode–No parameter is passed. Just

result is returned to the caller• Inout mode

Page 24: CPS  506 Comparative Programming Languages

Models of Parameter Passing

Page 25: CPS  506 Comparative Programming Languages

Pass-by-Value (In Mode)• The value of the actual parameter is used to initialize 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 the called subprogram and accesses cost more (indirect addressing)

Page 26: CPS  506 Comparative Programming Languages

Pass-by-Result (Out Mode)

• When a parameter is passed by result, no value is transmitted 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 move

– Require extra storage location and copy operation

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

represent the current value of p1– Evaluation time

Page 27: CPS  506 Comparative Programming Languages

Pass-by-Result (Out Mode)

• C#void Fixer(out int x,out int y) {

x = 17;

y =35;

}

...

f.Fixer(out a, out a);

----------------------

void DoIt(out int x, int index) {

x = 17;

index = 42;

}

...

sub = 21;

f.DoIt(list[sub], sub);

Page 28: CPS  506 Comparative Programming Languages

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

• A combination of pass-by-value and pass-by-result• Sometimes called pass-by-copy• Formal parameters have local

storage• Disadvantages:–Those of pass-by-result–Those of pass-by-value

Page 29: CPS  506 Comparative Programming Languages

Pass-by-Reference (In-out Mode)

• Pass an access path• Also called pass-by-sharing• 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 addressing

– Potentials for unwanted side effects (collisions)• Unwanted aliases

– Access to non-local variables (reduce readability and reliability)

Page 30: CPS  506 Comparative Programming Languages

Pass-by-Reference (In-out Mode)

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

fun(total, total)

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

fun1(list[i], list)

Page 31: CPS  506 Comparative Programming Languages

Pass-by-Reference (In-out Mode)

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

visibleint * global;

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

Page 32: CPS  506 Comparative Programming Languages

Pass-by-Name (In-out Mode)

• By 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 binding• Algol

procedure double(x); real x;

begin x := x * 2

end;

double(C[j]) is interpreted as C[j] := C[j] * 2.

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

Page 33: CPS  506 Comparative Programming Languages

Implementing Parameter-Passing Methods

• 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 stack

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

Page 34: CPS  506 Comparative Programming Languages

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

Page 35: CPS  506 Comparative Programming Languages

Parameter Passing Methods of Major Languages

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

parameters• Java

– All parameters are passed by value– Object parameters are passed by reference

• Ada– Three semantics modes of parameter transmission

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

Page 36: CPS  506 Comparative Programming Languages

Parameter Passing Methods of Major Languages (continued)

• Fortran 95- Parameters can be declared to be in, out, or inout mode

• C#- Default method: pass-by-value–Pass-by-reference is specified by

preceding both a formal parameter and its actual parameter with ref

Page 37: CPS  506 Comparative Programming Languages

Parameter Passing Methods of Major Languages

• Adaprocedure Adder( A : in out Integer;

B : in Integer;

C : out Float)• Fortran 95

Subroutine Adder(A, B, C)

Integer,Intent(Inout) :: A

Integer,Intent(In) :: B

Integer,Intent(Out) :: C• C#

void sumer(ref int oldSum, int newOne, out nextOne) {...}

...

sumer(ref sum, newValue, nextValue);

Page 38: CPS  506 Comparative Programming Languages

Parameter Passing Methods of Major Languages (continued)

• PHP: very similar to C#– Pass-by-reference by preceding &

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

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

Page 39: CPS  506 Comparative Programming Languages

Type Checking Parameters

• Considered very important for reliability• FORTRAN 77 and original C: none• C89

double sin(x)

double x;

{...}

0rdouble sin (double x)

{...}

Page 40: CPS  506 Comparative Programming Languages

Type Checking Parameters

• Pascal, FORTRAN 90, Java, and Ada: it is always required

• ANSI C and C++: choice is made by the user– Prototypesdouble sin(double x) {...}

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

using “...”int printf(const char* format_string, ...)

At least one parameter

Page 41: CPS  506 Comparative Programming Languages

Type Checking Parameters (con’t)

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

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

Page 42: CPS  506 Comparative Programming Languages

Multidimensional Arrays as Parameters

• If a multidimensional array is passed to a subprogram and the subprogram is separately compiled, the compiler needs to know the declared size of that array to build the storage mapping function

Page 43: CPS  506 Comparative Programming Languages

Multidimensional Arrays as Parameters: C and C++

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

• Storage-mapping functionaddress(mat[I,j]) = address(mat[0,0]) + I * number_of_columns + j

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

void main() {

int mat[5][10];

...

Fun(mat);

... }

• Disallows writing flexible subprograms

Page 44: CPS  506 Comparative Programming Languages

Multidimensional Arrays as Parameters: Java and C#

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

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

Page 45: CPS  506 Comparative Programming Languages

Design Considerations for Parameter Passing

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

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

variables, which means one-way whenever possible

– But pass-by-reference is more efficient to pass structures of significant size

Page 46: CPS  506 Comparative Programming Languages

Parameters that are Subprogram Names

• It is sometimes convenient to pass subprogram names as parameters

• Issues:– 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 of the parameters, so parameters can be type checked

Page 47: CPS  506 Comparative Programming Languages

Parameters that are Subprogram Names: Parameter Type Checking

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

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

Page 48: CPS  506 Comparative Programming Languages

Parameters that are Subprogram Names

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

executing the passed subprogram• In languages that allow nested

subprograms• Three choices

Page 49: CPS  506 Comparative Programming Languages

Parameters that are Subprogram Names:

Referencing Environment• Three choices

– Shallow binding: The environment of the call statement that enacts the passed subprogram- Most natural for dynamic-scoped languages

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

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

Page 50: CPS  506 Comparative Programming Languages

Parameters that are Subprogram Names:

Referencing Environmentfunction sub1() {

var x;

function sub2() {

alert(x); };

function sub3() {

var x;

x=3;

sub4(sub2); };

function sub4(subx) {

var x;

x=4;

subx(); };

x=1;

sub3();

};

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

– Deep binding– Output from alert(x) is 1

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

Page 51: CPS  506 Comparative Programming Languages

Overloaded Subprograms

• An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment–Every version of an overloaded

subprogram has a unique protocol• C++, Java, C#, and Ada include

predefined overloaded subprograms

Page 52: CPS  506 Comparative Programming Languages

User-Defined Overloaded Operators

• Operators can be overloaded in Ada, C++, Python, and Ruby• An Ada example (dot product)

function "*" (A,B: in Vec_Type): return Integer is

Sum: Integer := 0;

begin

for Index in A'range loop

Sum := Sum + A(Index) * B(Index)

end loop

return sum;

end "*";

c = a * b; -- a, b, and c are of type Vec_Type

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

Page 53: CPS  506 Comparative Programming Languages

Overloaded Subprograms

• C++ (operator overloading)class complex{

float re, im;

complex operator +(complex d){

complex ans;

ans.re = d.re+re;

ans.im = d.im+im;

return ans;

}

}

complex c,d;

c = c + d;

Page 54: CPS  506 Comparative Programming Languages

Overloaded Subprograms

• C++class C {

int x;

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

return v1 + v2;

}

public int sum(int v3) {

return x + v3;

}

}

Page 55: CPS  506 Comparative Programming Languages

Overloaded Subprograms

• C++ (ambiguous subprogram call)void fun(float b = 0.0);

void fun();

...

fun();

Page 56: CPS  506 Comparative Programming Languages

Overloaded Subprograms

• In Ada, the return type of an overloaded function can be used to disambiguate calls (thus two overloaded functions can have the same parameters)

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

Page 57: CPS  506 Comparative Programming Languages

Overloaded Subprograms

• Javapublic class OverloadExample {

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(s);

}

public static void main(String[] args) {

OverloadExample p = new OverloadExample();

p.test();

}

}

Page 58: CPS  506 Comparative Programming Languages

Generic Subprograms• Scenario– Generic Sort subprogram to sort arrays of

different element types

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

• Overloaded subprograms provide ad hoc polymorphism

Page 59: CPS  506 Comparative Programming Languages

Generic Subprograms (continued)

• Ada–Versions of a generic subprogram are

created by the compiler when explicitly instantiated by a declaration statement–Generic subprograms are preceded by a

generic clause that lists the generic variables, which can be types on other subprograms

Page 60: CPS  506 Comparative Programming Languages

Generic Subprograms (continued)

• Adageneric

type Index_type is (<>);

type Element_Type is private;

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

for Bottom in Index_Type ...

if List(Top) > List(Bottom) then

Temp := List(Top);

List(Top) := List(Bottom);

List(Bottom) := Temp;

end if;

end loop;

end loop;

end Generic_Sort;

Page 61: CPS  506 Comparative Programming Languages

Generic Subprograms (continued)

• No code is generated by compiler unless it is instantiated for some type• Array type: Int_Array• Elements: Integer• Subscripts: Integerprocedure Integer_Sort is new Generic_Sort(

Index_Type => Integer,

Element_Type => Integer,

Vector => Int_Array);

Page 62: CPS  506 Comparative Programming Languages

Generic Subprograms (continued)

• 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);

procedure Integrate(Lowerbd : in Float;

Upperbd : in Float;

Result : in Float) is

FunVal : Float;

begin

...

FunVal := Fun(Lowerbd);

...

end Integrate

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

Page 63: CPS  506 Comparative Programming Languages

Generic Subprograms (continued)

• C++– Versions of a generic subprogram are

created implicitly when the subprogram is named in a call or when its address is taken with the & operator

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

Page 64: CPS  506 Comparative Programming Languages

Generic Subprograms (continued)

• 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:#define max(a,b) ((a) > (b)) ? (a) : (b)

Page 65: CPS  506 Comparative Programming Languages

Generic Subprograms (continued)

• C++ generic sort subprogramtemplate <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;

}

}

Float flt_list[100];

...

generic_sort(flt_list, 100);

Page 66: CPS  506 Comparative Programming Languages

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)

• Restrictions can be specified on the range of classes that can be passed to the generic method as generic parameters (bounds)

• Wildcard types of generic parameters

Page 67: CPS  506 Comparative Programming Languages

Generic Subprograms (continued)

• Java 5.0 generic methodpublic 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 typepublic void drawAll(ArrayList<? Extends Shape> things)

To draw any object whose type is a subclass of Shape

Page 68: CPS  506 Comparative Programming Languages

Generic Subprograms (con’t)

• C# 2005–Supports generic methods that

are similar to those of Java 5.0

–Differences• No support for wildcard types• Actual type parameters in call can be omitted if compiler can infer that

Page 69: CPS  506 Comparative Programming Languages

Generic Subprograms (continued)

• C# 2005class MyClass {

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

...

}

}

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

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

Page 70: CPS  506 Comparative Programming Languages

Design Issues for Functions

• Are side effects allowed?– Parameters should always be in-mode to

reduce side effect (like Ada)• What types of return values are allowed?– Most imperative languages restrict the return

types– C allows any type except arrays and functions

(handled by pointer type return values)– C++ is like C but also allows user-defined

types or classes to be returned

Page 71: CPS  506 Comparative Programming Languages

Design Issues for Functions

• What types of return values are allowed?– Ada subprograms can return any type (but Ada

subprograms are not types, so they cannot be returned)

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

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

– Lua allows functions to return multiple values

Page 72: CPS  506 Comparative Programming Languages

Co-routines• A co-routine is a subprogram that has

multiple entries and controls them itself – supported directly in Lua

• Also called symmetric control: caller and called coroutines are on a more equal basis

• A co-routine call is named a resume

Page 73: CPS  506 Comparative Programming Languages

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

beginning, but subsequent calls enter at the point just after the last executed statement in the coroutine

• Co-routines repeatedly resume each other, possibly forever

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

Page 74: CPS  506 Comparative Programming Languages

Co-routines Illustrated: Possible Execution Controls

Page 75: CPS  506 Comparative Programming Languages

Co-routines Illustrated: Possible Execution Controls

Page 76: CPS  506 Comparative Programming Languages

Co-routines Illustrated: Possible Execution Controls with Loops