Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views109 pages

12-Sequence Control (Subprograms)

This document discusses control abstraction in programming through subprograms. It covers subprogram definition, mechanisms like simple call/return and recursion, parameter passing, and higher-order functions. The document outlines these topics and provides examples in Scala code to illustrate subprogram definitions and exception handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views109 pages

12-Sequence Control (Subprograms)

This document discusses control abstraction in programming through subprograms. It covers subprogram definition, mechanisms like simple call/return and recursion, parameter passing, and higher-order functions. The document outlines these topics and provides examples in Scala code to illustrate subprogram definitions and exception handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 109

Control Abstraction

Dr.. Nguyen Hua Phung

HCMC University of Technology, Viet Nam

09, 2013

Dr.. Nguyen Hua Phung Control Abstraction 1 / 39


Outline

1 Subprogram Definition

2 Subprogram Mechanisms
Simple Call Return
Recursie Call
Exception

3 Parameter Passing

4 Higher-order Funtions

Dr.. Nguyen Hua Phung Control Abstraction 2 / 39


Outline

1 Subprogram Definition

2 Subprogram Mechanisms
Simple Call Return
Recursie Call
Exception

3 Parameter Passing

4 Higher-order Funtions

Dr.. Nguyen Hua Phung Control Abstraction 3 / 39


Subprogram Definition

Subprogram definition consists of:


Specification
Subprogram name
Parameters
input + output
order
type
parameter passing mechanisms: by value, by
reference, by name,...
Behaviour of the subprogram
Implementation:
Local data
Collection of statements as subprogram body

Dr.. Nguyen Hua Phung Control Abstraction 4 / 39


Example

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 ()
}

Dr.. Nguyen Hua Phung Control Abstraction 5 / 39


Example

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 ()
}

1 How many subprogram definitions are there in the


above code?
2 How many parameters are there in each subprogram
definition?

Dr.. Nguyen Hua Phung Control Abstraction 5 / 39


Subprogram Activation

An activation of a subprogram:
is created when the subprogram is invoked
is destroyed when the subprogram completed its
execution

Dr.. Nguyen Hua Phung Control Abstraction 6 / 39


Subprogram Activation

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

Dr.. Nguyen Hua Phung Control Abstraction 6 / 39


Outline

1 Subprogram Definition

2 Subprogram Mechanisms
Simple Call Return
Recursie Call
Exception

3 Parameter Passing

4 Higher-order Funtions

Dr.. Nguyen Hua Phung Control Abstraction 7 / 39


Subprogram Mechanisms

Simple Call-Return
Recursive Call
Exception Processing Handler
Coroutines
Scheduled Subprograms
Tasks

Dr.. Nguyen Hua Phung Control Abstraction 8 / 39


Exercise

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?

Dr.. Nguyen Hua Phung Control Abstraction 9 / 39


Simple Call-Return

Basic Features
No recursion
Explicit Call Site
Single Entry Point
Immediate Control Passing
Single Execution

Dr.. Nguyen Hua Phung Control Abstraction 10 / 39


Recursive Call

Be able to call recursive


Direct Recursice Call
Indirect Recursive Call (Mutual Recursive)
Other features same as Simple Call-Return

Dr.. Nguyen Hua Phung Control Abstraction 11 / 39


Exception Processing Handler

May have no explicit call site

Dr.. Nguyen Hua Phung Control Abstraction 12 / 39


Exception Processing Handler

May have no explicit call site


Used in

Dr.. Nguyen Hua Phung Control Abstraction 12 / 39


Exception Processing Handler

May have no explicit call site


Used in
Event-Driven Programming

Dr.. Nguyen Hua Phung Control Abstraction 12 / 39


Exception Processing Handler

May have no explicit call site


Used in
Event-Driven Programming
Error Handler

Dr.. Nguyen Hua Phung Control Abstraction 12 / 39


Exception Processing Handler

May have no explicit call site


Used in
Event-Driven Programming
Error Handler

Dr.. Nguyen Hua Phung Control Abstraction 12 / 39


Exception Processing Handler

May have no explicit call site


Used in
Event-Driven Programming
Error Handler
Example,
class EmptyExcp extends Throwable { i n t x = 0 ; } ;

i n t average ( i n t [ ] V ) throws EmptyExcp ( ) {


i f ( l e n g t h ( V) = = 0 ) throws new EmptyExcp ( ) ;
else . . .
};
...
try { . . .
average (W) ;
...
}
catch ( EmptyExcp e ) { w r i t e ( " A r r a y empty " ) ; }

Dr.. Nguyen Hua Phung Control Abstraction 12 / 39


Exception Mechanisms

A language must specify:


which exceptions can be handled and how they can
be defined
how an exception can be raised
how an exception can be handled

Dr.. Nguyen Hua Phung Control Abstraction 13 / 39


How an exception can be defined

Java: subclass of Throwable


Ada: values of a special type
C++: any value

Dr.. Nguyen Hua Phung Control Abstraction 14 / 39


How an exception can be raised

Raising exception
By user interaction
(Click, MouseMove, TextChange, ...)

Dr.. Nguyen Hua Phung Control Abstraction 15 / 39


How an exception can be raised

Raising exception
By user interaction
(Click, MouseMove, TextChange, ...)
By operating system

Dr.. Nguyen Hua Phung Control Abstraction 15 / 39


How an exception can be raised

Raising exception
By user interaction
(Click, MouseMove, TextChange, ...)
By operating system
By an object (Timer)

Dr.. Nguyen Hua Phung Control Abstraction 15 / 39


How an exception can be raised

Raising exception
By user interaction
(Click, MouseMove, TextChange, ...)
By operating system
By an object (Timer)
By programmer (throw )

Dr.. Nguyen Hua Phung Control Abstraction 15 / 39


Example in Scala

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 ! " ) }

Dr.. Nguyen Hua Phung Control Abstraction 16 / 39


How an exception can be handled

Define the protected block to intercept the exception


for being handled
Define exception handler associated with the
protected block

Dr.. Nguyen Hua Phung Control Abstraction 17 / 39


How an exception can be handled

Define the protected block to intercept the exception


for being handled
Define exception handler associated with the
protected block

Termination Semantic
non-resumable (common) + stack unwinding
resumable
at the statement causing the error
after the statement causing the error

Dr.. Nguyen Hua Phung Control Abstraction 17 / 39


Coroutines
A coroutine may postpone its execution and control is
back to caller. Its execution later is resumed at the place it
postponed.

A B

resume B
resume A

resume B
resume A

resume B
resume A

Dr.. Nguyen Hua Phung Control Abstraction 18 / 39


Tasks

able to execute concurrently with other tasks


run on multi-processor machine or
single processor machine using time sharing

Dr.. Nguyen Hua Phung Control Abstraction 19 / 39


Tasks

able to execute concurrently with other tasks


run on multi-processor machine or
single processor machine using time sharing
Issue?

Dr.. Nguyen Hua Phung Control Abstraction 19 / 39


Tasks

able to execute concurrently with other tasks


run on multi-processor machine or
single processor machine using time sharing
Issue?
Synchronization
Race condition
Deadlock
Communication

Dr.. Nguyen Hua Phung Control Abstraction 19 / 39


Example in Scala

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 map { v => i f ( v % 2 == 0 ) v else −v }

pa . f o l d ( 0 ) { _ + _ }

var a = 0

pa foreach { a += _ }

Dr.. Nguyen Hua Phung Control Abstraction 20 / 39


Scheduled subprograms

The execution of callee is NOT started when it is


invoked
scheduled by time
CALL A AT TIME = CURRENT_TIME + 10
scheduled by priority
CALL B WITH PRIORITY 7
Controlled by a scheduler

Dr.. Nguyen Hua Phung Control Abstraction 21 / 39


Outline

1 Subprogram Definition

2 Subprogram Mechanisms
Simple Call Return
Recursie Call
Exception

3 Parameter Passing

4 Higher-order Funtions

Dr.. Nguyen Hua Phung Control Abstraction 22 / 39


Formal and Actual Parameter

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

Dr.. Nguyen Hua Phung Control Abstraction 23 / 39


Formal and Actual Parameter

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)

Dr.. Nguyen Hua Phung Control Abstraction 23 / 39


Parameter Passing

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

Dr.. Nguyen Hua Phung Control Abstraction 24 / 39


Input-Output Parameters

Pass by value-result

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

Pass by value-result

caller
a 5

b 6

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

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

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

Pass by value-result
int findMax(int x,int y) {...}
caller callee
a 5 7 x

b 6 8 y

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

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

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

Pass by value-result

caller
a 7

b 8

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

Pass by value-result

Pass by reference

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

Pass by value-result

Pass by reference

caller
a 5

b 6

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

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

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

Pass by value-result

Pass by reference
int findMax(int& x,int& y) {...}
caller callee
a 5 x

b 6 y

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

Pass by value-result

Pass by reference
int findMax(int& x,int& y) {...}
caller callee
a 7 x

b 8 y

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

Pass by value-result

Pass by reference
findMax(a,b) ⇐
caller
a 7

b 8

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

Pass by value-result

Pass by reference

Pass by name

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

Pass by value-result

Pass by reference

Pass by name
findMax(a,b) ⇒ int findMax(int⇒ x,int⇒ y) {...}

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Input-Output Parameters

Pass by value-result

Pass by reference

Pass by name
int findMax(int⇒ x,int⇒ y) {...}
a≡x
b≡y

Dr.. Nguyen Hua Phung Control Abstraction 25 / 39


Exercise

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 ] ;
}

Dr.. Nguyen Hua Phung Control Abstraction 26 / 39


Input Only Parameters

Pass by value

Dr.. Nguyen Hua Phung Control Abstraction 27 / 39


Input Only Parameters

Pass by value

caller
a 5

b 6

Dr.. Nguyen Hua Phung Control Abstraction 27 / 39


Input Only Parameters

Pass by value
findMax(a,b) ⇒ int findMax(int x,int y) {...}
caller callee
value
a 5 5 x

value
b 6 6 y

Dr.. Nguyen Hua Phung Control Abstraction 27 / 39


Input Only Parameters

Pass by value
int findMax(int x,int y) {...}
caller callee
a 5 7 x

b 6 8 y

Dr.. Nguyen Hua Phung Control Abstraction 27 / 39


Input Only Parameters

Pass by value
findMax(a,b) ⇐
caller
a 5

b 6

Dr.. Nguyen Hua Phung Control Abstraction 27 / 39


Input Only Parameters

Pass by value

Pass by constant reference

Dr.. Nguyen Hua Phung Control Abstraction 27 / 39


Input Only Parameters

Pass by value

Pass by constant reference

caller
a 5

b 6

Dr.. Nguyen Hua Phung Control Abstraction 27 / 39


Input Only Parameters

Pass by value

Pass by constant reference


findMax(a,b) ⇒ int findMax(const int& x,const int&
y) {...}
caller callee
address
a 5 x

address
b 6 y

Dr.. Nguyen Hua Phung Control Abstraction 27 / 39


Input Only Parameters

Pass by value

Pass by constant reference


int findMax(const int& x,const int& y)
{...}
caller callee
a 5 x

b 6 y

Dr.. Nguyen Hua Phung Control Abstraction 27 / 39


Input Only Parameters

Pass by value

Pass by constant reference


int findMax(const int& x,const int& y)
{...}
caller callee
a 5 x

b 6 y

Dr.. Nguyen Hua Phung Control Abstraction 27 / 39


Input Only Parameters

Pass by value

Pass by constant reference


findMax(a,b) ⇐

caller
a 5

b 6

Dr.. Nguyen Hua Phung Control Abstraction 27 / 39


Output Only Parameters

Pass by result

Dr.. Nguyen Hua Phung Control Abstraction 28 / 39


Output Only Parameters

Pass by result

caller
a 5

b 6

Dr.. Nguyen Hua Phung Control Abstraction 28 / 39


Output Only Parameters

Pass by result
findMax(a,b) ⇒ int findMax(int x,int y) {...}
caller callee
a 5 x

b 6 y

Dr.. Nguyen Hua Phung Control Abstraction 28 / 39


Output Only Parameters

Pass by result
int findMax(int x,int y) {...}
caller callee
a 5 3 x

b 6 4 y

Dr.. Nguyen Hua Phung Control Abstraction 28 / 39


Output Only Parameters

Pass by result
findMax(a,b) ⇐
caller
value
a 3 3

value
b 4 4

Dr.. Nguyen Hua Phung Control Abstraction 28 / 39


Output Only Parameters

Pass by result

As a result of a function

Dr.. Nguyen Hua Phung Control Abstraction 28 / 39


Output Only Parameters

Pass by result

As a result of a function
int foo() ... return 0;
No actual parameter: foo() + 1

Dr.. Nguyen Hua Phung Control Abstraction 28 / 39


Exercise

type VECT = array [ 1 . . 3 ] of i n t e g e r ;


type VECTPTR = @VECT;
procedure SUB1 ;
var A , B : VECT ; P ,Q: VECTPTR ;
begin
A [ 1 ] : = 7 ;A [ 2 ] : = 8 ; A [ 3 ] : = 9 ;
B[ 1 ] := 7;b [ 2 ] := 8; B[ 3 ] := 9;
P : = @A; Q : = @B;
SUB2( P ,Q ) ;
end ;
procedure SUB2(R : VECTPTR ; var S : VECTPTR)
begin
R^ [ 1 ] := R^ [ 1 ] + 10; // 1
S^ [ 1 ] := S^ [ 1 ] + 10; // 2
i f . . . then R : = S ; // 3
else S : = R ; // 4
end ;

Dr.. Nguyen Hua Phung Control Abstraction 29 / 39


Exercise (cont’d)

Which storage (variable) is changed and what is its new


value after changing after the following instruction is
executed?
a.
// 1
b. // 2
c.
// 3
d. // 4

Dr.. Nguyen Hua Phung Control Abstraction 30 / 39


Outline

1 Subprogram Definition

2 Subprogram Mechanisms
Simple Call Return
Recursie Call
Exception

3 Parameter Passing

4 Higher-order Funtions

Dr.. Nguyen Hua Phung Control Abstraction 31 / 39


Higher-order Functions

A function is higher-order when it accepts functions


as its input parameters (fairly common)

Dr.. Nguyen Hua Phung Control Abstraction 32 / 39


Higher-order Functions

A function is higher-order when it accepts functions


as its input parameters (fairly common)
as its out parameters (less common - but required in
functional programming)

Dr.. Nguyen Hua Phung Control Abstraction 32 / 39


Higher-order Functions

A function is higher-order when it accepts functions


as its input parameters (fairly common)
as its out parameters (less common - but required in
functional programming)

Dr.. Nguyen Hua Phung Control Abstraction 32 / 39


Higher-order Functions

A function is higher-order when it accepts functions


as its input parameters (fairly common)
as its out parameters (less common - but required in
functional programming)

Example, in stdlib.h of C, there is a built-in sorting function


void q s o r t ( void ∗base , s i z e _ t nmemb, s i z e _ t s i z e ,
i n t ( ∗ compar ) ( const void ∗ , const void ∗ ) ) ;

Dr.. Nguyen Hua Phung Control Abstraction 32 / 39


Higher-order Functions

A function is higher-order when it accepts functions


as its input parameters (fairly common)
as its out parameters (less common - but required in
functional programming)

Example, in stdlib.h of C, there is a built-in sorting function


void q s o r t ( void ∗base , s i z e _ t nmemb, s i z e _ t s i z e ,
i n t ( ∗ compar ) ( const void ∗ , const void ∗ ) ) ;

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 ;
}

Dr.. Nguyen Hua Phung Control Abstraction 32 / 39


Higher-order Functions

A function is higher-order when it accepts functions


as its input parameters (fairly common)
as its out parameters (less common - but required in
functional programming)
Example, in stdlib.h of C, there is a built-in sorting function
void q s o r t ( void ∗base , s i z e _ t nmemb, s i z e _ t s i z e ,
i n t ( ∗ compar ) ( const void ∗ , const void ∗ ) ) ;

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

What is non-local environment?


Deep binding
Shallow binding

Example,

Skip Scala Example

Dr.. Nguyen Hua Phung Control Abstraction 33 / 39


Functions as Parameters

What is non-local environment?


Deep binding
Shallow binding

Example,
int x = 1;
i n t f ( i n t y ) { r e t u r n x+y ; }

int g ( int h( int b ) ) {


int x = 2;
return h ( 3 ) + x ; / / shallow binding
}
...
{ int x = 4;
i n t z = g ( f ) ; / / deep b i n d i n g
}

Skip Scala Example

Dr.. Nguyen Hua Phung Control Abstraction 33 / 39


Functions as Parameters

What is non-local environment?


Deep binding
Shallow binding

Example, Static scope: z =


int x = 1;
i n t f ( i n t y ) { r e t u r n x+y ; }

int g ( int h( int b ) ) {


int x = 2;
return h ( 3 ) + x ; / / shallow binding
}
...
{ int x = 4;
i n t z = g ( f ) ; / / deep b i n d i n g
}

Skip Scala Example

Dr.. Nguyen Hua Phung Control Abstraction 33 / 39


Functions as Parameters

What is non-local environment?


Deep binding
Shallow binding

Example, Static scope: z = 6


int x = 1;
i n t f ( i n t y ) { r e t u r n x+y ; }

int g ( int h( int b ) ) {


int x = 2;
return h ( 3 ) + x ; / / shallow binding
}
...
{ int x = 4;
i n t z = g ( f ) ; / / deep b i n d i n g
}

Skip Scala Example

Dr.. Nguyen Hua Phung Control Abstraction 33 / 39


Functions as Parameters

What is non-local environment?


Deep binding
Shallow binding

Example, Dynamic scope + Deep binding: z =


int x = 1;
i n t f ( i n t y ) { r e t u r n x+y ; }

int g ( int h( int b ) ) {


int x = 2;
return h ( 3 ) + x ; / / shallow binding
}
...
{ int x = 4;
i n t z = g ( f ) ; / / deep b i n d i n g
}

Skip Scala Example

Dr.. Nguyen Hua Phung Control Abstraction 33 / 39


Functions as Parameters

What is non-local environment?


Deep binding
Shallow binding

Example, Dynamic scope + Deep binding: z = 9


int x = 1;
i n t f ( i n t y ) { r e t u r n x+y ; }

int g ( int h( int b ) ) {


int x = 2;
return h ( 3 ) + x ; / / shallow binding
}
...
{ int x = 4;
i n t z = g ( f ) ; / / deep b i n d i n g
}

Skip Scala Example

Dr.. Nguyen Hua Phung Control Abstraction 33 / 39


Functions as Parameters

What is non-local environment?


Deep binding
Shallow binding

Example, Dynamic scope + Shallow binding: z =


int x = 1;
i n t f ( i n t y ) { r e t u r n x+y ; }

int g ( int h( int b ) ) {


int x = 2;
return h ( 3 ) + x ; / / shallow binding
}
...
{ int x = 4;
i n t z = g ( f ) ; / / deep b i n d i n g
}

Skip Scala Example

Dr.. Nguyen Hua Phung Control Abstraction 33 / 39


Functions as Parameters

What is non-local environment?


Deep binding
Shallow binding

Example, Dynamic scope + Shallow binding: z = 7


int x = 1;
i n t f ( i n t y ) { r e t u r n x+y ; }

int g ( int h( int b ) ) {


int x = 2;
return h ( 3 ) + x ; / / shallow binding
}
...
{ int x = 4;
i n t z = g ( f ) ; / / deep b i n d i n g
}

Skip Scala Example

Dr.. Nguyen Hua Phung Control Abstraction 33 / 39


Exercise

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

What returns as functions

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment
Example,

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment
Example,
void−> i n t F ( ) {
int x = 1;
int g () {
r e t u r n x +1;
}
return g ;
}
void−> i n t gg = F ( ) ;
i n t z = gg ( ) ;

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment
Example,
void−> i n t F ( ) {
int x = 1;
int g () {
r e t u r n x +1;
}
return g ;
}
void−> i n t gg = F ( ) ;
i n t z = gg ( ) ;

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment
Example,
void−> i n t F ( ) {
int x = 1;
int g () {
r e t u r n x +1;
}
return g ;
}
void−> i n t gg = F ( ) ;
i n t z = gg ( ) ;

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment
Example, main
void−> i n t F ( ) { gg
int x = 1; z
int g () {
r e t u r n x +1;
}
return g ;
}
void−> i n t gg = F ( ) ;
i n t z = gg ( ) ;

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment
Example, main F
void−> i n t F ( ) { gg x 1
int x = 1; z
int g () {
r e t u r n x +1;
}
return g ;
}
void−> i n t gg = F ( ) ;
i n t z = gg ( ) ;

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment
Example, main F
void−> i n t F ( ) { gg x 1
int x = 1; z
int g () {
r e t u r n x +1;
}
return g ; g
}
void−> i n t gg = F ( ) ; code
i n t z = gg ( ) ;

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment
Example, main
void−> i n t F ( ) { gg
int x = 1; z
int g () {
r e t u r n x +1;
}
return g ; g
}
void−> i n t gg = F ( ) ; code
i n t z = gg ( ) ;

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment
Example, main
void−> i n t F ( ) { gg
int x = 1; z
int g () {
r e t u r n x +1;
}
return g ; g
}
void−> i n t gg = F ( ) ; code
i n t z = gg ( ) ;

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment
Example, main g
void−> i n t F ( ) { gg
int x = 1; z
int g () {
r e t u r n x +1;
}
return g ; g
}
void−> i n t gg = F ( ) ; code
i n t z = gg ( ) ;

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Functions as Returns

What returns as functions


Code
Environment
Example, main g
void−> i n t F ( ) { gg No x
int x = 1; to eval
z
int g () { x+1
r e t u r n x +1;
}
return g ; g
}
void−> i n t gg = F ( ) ; code
i n t z = gg ( ) ;

Dr.. Nguyen Hua Phung Control Abstraction 37 / 39


Exercise

1 How to keep the environment for function as results?


2 Does the same problem happen for function as
parameter? If your answer is Yes, please give an
example.
Skip Scala Example

Dr.. Nguyen Hua Phung Control Abstraction 38 / 39


Summary

Subprogram mechanisms
Simple Call-Return
Recursive Call
Exception
Coroutine
Scheduled Call
Tasks
Parameter Passing
Higher-order Functions

Dr.. Nguyen Hua Phung Control Abstraction 39 / 39

You might also like