CSP 506 Comparative Programming Languages

Download Report

Transcript CSP 506 Comparative Programming Languages

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
User-Defined Overloaded Operators
Generic Subprograms
Design Issues for Functions
Co-routines
2
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
3
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
4
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]
5
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)
6
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
7
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 end
cube = function (x) return x * x * x end
8
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
9
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
10
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);
– Python
Def compute_pay(income, exemptions = 1, tax_rate)
pay = compute_pay(20000.0,tax_rate = 0.15)
11
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);
12
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]
13
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
P2
p3
p4
is
is
is
is
2
4
[6,8]
{‘mon’:72, ‘tue’:68, ‘wed’:59}
14
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
15
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
16
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)
17
•
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
•
•
•
•
18
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?
•
•
•
•
19
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
20
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
21
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
22
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
23
Models of Parameter Passing
24
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)
25
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
26
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);
27
•
•
•
•
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
28
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)
29
Pass-by-Reference (In-out Mode)
– C++
• Collusion between actual parameters
void fun(int &first, int &second)
fun(total, total)
first and second in fun will be aliases.
• Collusion between array elements
fun(list[i], list[j])
fun1(list[i], list)
30
Pass-by-Reference (In-out Mode)
– C++
•
Collusion between formal parameters and non-local variables that are visible
int * 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.
31
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
32
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
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
• 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
35
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
36
Parameter Passing Methods of Major
Languages
•
•
•
Ada
procedure 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);
37
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
38
Type Checking Parameters
• Considered very important for reliability
• FORTRAN 77 and original C: none
• C89
double sin(x)
double x;
{...}
0r
double sin (double x)
{...}
39
Type Checking Parameters
• Pascal, FORTRAN 90, Java, and Ada: it is always
required
• ANSI C and C++: choice is made by the user
– Prototypes
double 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
40
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
41
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
42
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 function
address(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
43
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
44
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
45
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
46
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
47
Parameters that are Subprogram
Names
• Issues: (con’t)
– Referencing environment for
executing the passed subprogram
• In languages that allow nested
subprograms
• Three choices
48
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
49
Parameters that are Subprogram
Names: Referencing Environment
function sub1() {
var x;
function sub2() {
– Shallow binding
– Output from alert(x) is 4
alert(x); };
function sub3() {
var x;
x=3;
– Deep binding
– Output from alert(x) is 1
sub4(sub2); };
function sub4(subx) {
var x;
x=4;
subx(); };
– Ad hoc binding
– Output from alert(x) is 3
x=1;
sub3();
};
50
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
51
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 operator
int operator * (const vector &a, const vector &b, int len);
52
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;
53
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;
}
}
54
Overloaded Subprograms
• C++ (ambiguous subprogram call)
void fun(float b = 0.0);
void fun();
...
fun();
55
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
56
Overloaded Subprograms
• Java
public 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();
}
}
57
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
58
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
59
Generic Subprograms
(continued)
• Ada
generic
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;
60
Generic Subprograms
(continued)
• No code is generated by compiler unless it is
instantiated for some type
• Array type: Int_Array
• Elements: Integer
• Subscripts: Integer
procedure Integer_Sort is new Generic_Sort(
Index_Type => Integer,
Element_Type => Integer,
Vector => Int_Array);
61
Generic Subprograms
(continued)
• Using generic subprogram in Ada to pass subprogram as
parameter
generic
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);
62
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
63
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)
64
Generic Subprograms
•
(continued)
C++ 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;
}
}
Float flt_list[100];
...
generic_sort(flt_list, 100);
65
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
66
Generic Subprograms
•
(continued)
Java 5.0 generic method
public static <T> T doIt(T[] list) {
...
}
doIt<String>(myList);
•
Java 5.0 wildcard type
void printCollection(Collection<?> c) {
for (object e: c) {
System.out.println(e);
}
}
•
Java 5.0 bounded wildcard type
public void drawAll(ArrayList<? Extends Shape> things)
To draw any object whose type is a subclass of Shape
67
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
68
Generic Subprograms
•
(continued)
C# 2005
class MyClass {
public static T DoIt<T>(T p1) {
...
}
}
Which could be called
int myInt = MyClass.DoIt(17); //calls DoIt<int>
String myStr =MyClass.DoIt(‘apples’); //calls DoIt<String>
69
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
70
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
71
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
72
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 coroutines); their execution is interleaved,
but not overlapped
73
Co-routines Illustrated: Possible
Execution Controls
74
Co-routines Illustrated: Possible
Execution Controls
75
Co-routines Illustrated: Possible
Execution Controls with Loops
76