12-Sequence Control (Subprograms)
12-Sequence Control (Subprograms)
09, 2013
1 Subprogram Definition
2 Subprogram Mechanisms
Simple Call Return
Recursie Call
Exception
3 Parameter Passing
4 Higher-order Funtions
1 Subprogram Definition
2 Subprogram Mechanisms
Simple Call Return
Recursie Call
Exception
3 Parameter Passing
4 Higher-order Funtions
def a p p l y ( i n t e r v a l : I n t ,
r e p e a t s : Boolean = t r u e )
( op : => U n i t ) {
v a l timeOut = new j a v a x . swing . A b s t r a c t A c t i o n ( ) {
def a c t i o n P e r f o r m e d
( e : j a v a . awt . event . A c t i o n E v e n t ) = op
}
v a l t = new j a v a x . swing . Timer ( i n t e r v a l , timeOut )
t . setRepeats ( r e p e a t s )
t . start ()
}
def a p p l y ( i n t e r v a l : I n t ,
r e p e a t s : Boolean = t r u e )
( op : => U n i t ) {
v a l timeOut = new j a v a x . swing . A b s t r a c t A c t i o n ( ) {
def a c t i o n P e r f o r m e d
( e : j a v a . awt . event . A c t i o n E v e n t ) = op
}
v a l t = new j a v a x . swing . Timer ( i n t e r v a l , timeOut )
t . setRepeats ( r e p e a t s )
t . start ()
}
An activation of a subprogram:
is created when the subprogram is invoked
is destroyed when the subprogram completed its
execution
An activation of a subprogram:
is created when the subprogram is invoked
is destroyed when the subprogram completed its
execution
An activation includes
Static part: Code segment
Dynamic part: Activation record
formal parameters
local data
return address
other links
1 Subprogram Definition
2 Subprogram Mechanisms
Simple Call Return
Recursie Call
Exception
3 Parameter Passing
4 Higher-order Funtions
Simple Call-Return
Recursive Call
Exception Processing Handler
Coroutines
Scheduled Subprograms
Tasks
i n t mul ( Node r o o t ) {
i f ( r o o t == n u l l ) r e t u r n 1 ;
else r o o t . v a l ∗ mul ( r o o t . l e f t )
∗ mul ( r o o t . r i g h t ) ;
}
Rewrite the above function to short-circuit the traversal if
val of a node in the binary tree is 0?
Basic Features
No recursion
Explicit Call Site
Single Entry Point
Immediate Control Passing
Single Execution
Raising exception
By user interaction
(Click, MouseMove, TextChange, ...)
Raising exception
By user interaction
(Click, MouseMove, TextChange, ...)
By operating system
Raising exception
By user interaction
(Click, MouseMove, TextChange, ...)
By operating system
By an object (Timer)
Raising exception
By user interaction
(Click, MouseMove, TextChange, ...)
By operating system
By an object (Timer)
By programmer (throw )
object Timer {
def a p p l y ( i n t e r v a l : I n t ,
r e p e a t s : Boolean = t r u e )
( op : => U n i t ) {
v a l timeOut = new j a v a x . swing . A b s t r a c t A c t i o n ( ) {
def a c t i o n P e r f o r m e d
( e : j a v a . awt . event . A c t i o n E v e n t ) = op
}
v a l t = new j a v a x . swing . Timer ( i n t e r v a l , timeOut )
t . setRepeats ( r e p e a t s )
t . start ()
}
}
Timer ( 2 0 0 0 ) { p r i n t l n ( " Timer went o f f " ) }
Timer (10000 , f a l s e ) { p r i n t l n ( " 1 0 seconds are over ! " ) }
Termination Semantic
non-resumable (common) + stack unwinding
resumable
at the statement causing the error
after the statement causing the error
A B
resume B
resume A
resume B
resume A
resume B
resume A
v a l pa = ( 0 u n t i l 1 0 0 0 0 ) . t o A r r a y . par
pa . map( _ + 1 )
pa . f o l d ( 0 ) { _ + _ }
var a = 0
pa foreach { a += _ }
1 Subprogram Definition
2 Subprogram Mechanisms
Simple Call Return
Recursie Call
Exception
3 Parameter Passing
4 Higher-order Funtions
Definition
Formal parameters:int foo(float x,bool& y);
just a simple name
close to a variable declaration
combine with symbols relating to parameter passing
mechanism
Actual parameters/Arguments:foo(4*a,b)
an expression
Definition
Formal parameters:int foo(float x,bool& y);
just a simple name
close to a variable declaration
combine with symbols relating to parameter passing
mechanism
Actual parameters/Arguments:foo(4*a,b)
an expression
Formal-Actual Corresponding
by position
int foo(float a,int b) ⇐ foo(x+1,y-2)
by name
int foo(float a,int b) ⇐ foo(b = x+1, a = y-2)
Input-Output
By value-result
By reference
By name
Input Only
By value
By constant reference
Output Only
By result
As a result of a function
Pass by value-result
Pass by value-result
caller
a 5
b 6
Pass by value-result
findMax(a,b) ⇒ int findMax(int x,int y) {...}
caller callee
value
a 5 5 x
value
b 6 6 y
Pass by value-result
int findMax(int x,int y) {...}
caller callee
a 5 7 x
b 6 8 y
Pass by value-result
findMax(a,b) ⇐ int findMax(int x,int y) {...}
caller callee
value
a 7 7 x
value
b 8 8 y
Pass by value-result
caller
a 7
b 8
Pass by value-result
Pass by reference
Pass by value-result
Pass by reference
caller
a 5
b 6
Pass by value-result
Pass by reference
findMax(a,b) ⇒ int findMax(int& x,int& y) {...}
caller callee
address
a 5 x
address
b 6 y
Pass by value-result
Pass by reference
int findMax(int& x,int& y) {...}
caller callee
a 5 x
b 6 y
Pass by value-result
Pass by reference
int findMax(int& x,int& y) {...}
caller callee
a 7 x
b 8 y
Pass by value-result
Pass by reference
findMax(a,b) ⇐
caller
a 7
b 8
Pass by value-result
Pass by reference
Pass by name
Pass by value-result
Pass by reference
Pass by name
findMax(a,b) ⇒ int findMax(int⇒ x,int⇒ y) {...}
Pass by value-result
Pass by reference
Pass by name
int findMax(int⇒ x,int⇒ y) {...}
a≡x
b≡y
void swap ( i n t x , i n t y ) {
int t = x ;
x = y;
y = t;
}
void main ( ) {
int a [ ] = {2 , 1 , 0}; i = 0;
swap ( i , a [ i ] ) ;
c o u t << i << a [ 0 ] << a [ 1 ] << a [ 2 ] ;
}
Pass by value
Pass by value
caller
a 5
b 6
Pass by value
findMax(a,b) ⇒ int findMax(int x,int y) {...}
caller callee
value
a 5 5 x
value
b 6 6 y
Pass by value
int findMax(int x,int y) {...}
caller callee
a 5 7 x
b 6 8 y
Pass by value
findMax(a,b) ⇐
caller
a 5
b 6
Pass by value
Pass by value
caller
a 5
b 6
Pass by value
address
b 6 y
Pass by value
b 6 y
Pass by value
b 6 y
Pass by value
caller
a 5
b 6
Pass by result
Pass by result
caller
a 5
b 6
Pass by result
findMax(a,b) ⇒ int findMax(int x,int y) {...}
caller callee
a 5 x
b 6 y
Pass by result
int findMax(int x,int y) {...}
caller callee
a 5 3 x
b 6 4 y
Pass by result
findMax(a,b) ⇐
caller
value
a 3 3
value
b 4 4
Pass by result
As a result of a function
Pass by result
As a result of a function
int foo() ... return 0;
No actual parameter: foo() + 1
1 Subprogram Definition
2 Subprogram Mechanisms
Simple Call Return
Recursie Call
Exception
3 Parameter Passing
4 Higher-order Funtions
int
i n t _ s o r t e r ( const void ∗ f i r s t _ a r g , const void ∗ second_arg )
int f i r s t = ∗( int ∗) f i r s t _ a r g ;
i n t second = ∗ ( i n t ∗ ) second_arg ;
i f ( f i r s t < second ) r e t u r n −1;
else i f ( f i r s t == second ) r e t u r n 0 ;
else r e t u r n 1 ;
}
i n t main ( ) {
int array [10] , i ;
/∗ f i l l array ∗/
f o r ( i = 0 ; i < 1 0 ; ++ i )
a r r a y [ i ] = 10 − i ;
q s o r t ( a r r a y , 10 , s i z e o f ( i n t ) , i n t _ s o r t e r ) ;
f o r ( i = 0 ; i < 1 0 ; ++ i )
p r i n t f ( "%d \ n " , a r r a y [ i ] ) ;
}
Dr.. Nguyen Hua Phung Control Abstraction 32 / 39
Functions as Parameters
Example,
Example,
int x = 1;
i n t f ( i n t y ) { r e t u r n x+y ; }
var X : r e a l ;
procedure SUB2( X , Y : r e a l ; function F (U : r e a l ) : r e a l ) ;
var Z : r e a l ;
begin
Z : = abs ( X − Y ) ;
Z := (F(X) + F(Y) ) ∗ Z / 2;
write (Z ) ; / / ??? i n s t a t i c −scoping language
procedure SUB1 ;
var Y : r e a l ;
function FUNC( V : r e a l ) : r e a l ;
begin
FUNC : = X ∗ V + Y ;
begin
Y := 1;
SUB2( 0 , 1 ,FUNC ) ;
begin
X := 3;
SUB1 ;
end .
Dr.. Nguyen Hua Phung Control Abstraction 34 / 39
Example in Scala
object FileMatcher {
p r i v a t e def f i l e s H e r e =
(new j a v a . i o . F i l e ( " . " ) ) . l i s t F i l e s
def f i l e s E n d i n g ( query : S t r i n g ) =
f o r ( f i l e <− f i l e s H e r e ;
i f f i l e . getName . endsWith ( query ) )
yield f i l e }
def f i l e s C o n t a i n i n g ( query : S t r i n g ) =
f o r ( f i l e <− f i l e s H e r e ;
i f f i l e . getName . c o n t a i n s ( query ) )
yield f i l e
def f i l e s R e g e x ( query : S t r i n g ) =
f o r ( f i l e <− f i l e s H e r e ;
i f f i l e . getName . matches ( query ) )
yield f i l e
}
Dr.. Nguyen Hua Phung Control Abstraction 35 / 39
Example in Scala
object FileMatcher {
p r i v a t e def f i l e s H e r e =
(new j a v a . i o . F i l e ( " . " ) ) . l i s t F i l e s
def f i l e s E n d i n g ( query : S t r i n g ) =
f o r ( f i l e <− f i l e s H e r e ;
i f f i l e . getName . endsWith ( query ) )
yield f i l e }
def f i l e s C o n t a i n i n g ( query : S t r i n g ) =
f o r ( f i l e <− f i l e s H e r e ;
i f f i l e . getName . c o n t a i n s ( query ) )
yield f i l e
def f i l e s R e g e x ( query : S t r i n g ) =
f o r ( f i l e <− f i l e s H e r e ;
i f f i l e . getName . matches ( query ) )
yield f i l e
}
Dr.. Nguyen Hua Phung Control Abstraction 35 / 39
Example in Scala
object FileMatcher {
p r i v a t e def f i l e s H e r e =
(new j a v a . i o . F i l e ( " . " ) ) . l i s t F i l e s
def f i l e s M a t c h i n g ( query : S t r i n g ,
matcher : ( S t r i n g , S t r i n g ) => Boolean ) = {
f o r ( f i l e <− f i l e s H e r e ;
i f matcher ( f i l e . getName , query ) )
yield f i l e
}
def f i l e s E n d i n g ( query : S t r i n g ) =
f i l e s M a t c h i n g ( query , _ . endsWith ( _ ) )
def f i l e s C o n t a i n i n g ( query : S t r i n g ) =
f i l e s M a t c h i n g ( query , _ . c o n t a i n s ( _ ) )
def f i l e s R e g e x ( query : S t r i n g ) =
f i l e s M a t c h i n g ( query , _ . matches ( _ ) )
}
Dr.. Nguyen Hua Phung Control Abstraction 36 / 39
Functions as Returns
Subprogram mechanisms
Simple Call-Return
Recursive Call
Exception
Coroutine
Scheduled Call
Tasks
Parameter Passing
Higher-order Functions