Top Banner
Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 1 Announcement Aditya’s lecture note is at http://budhi.uow.edu.au/staff/aditya/csci337/lect urenotes.html my lecture note /share/cs-pub/337/
48

Announcement

Jan 25, 2016

Download

Documents

Announcement. Aditya’s lecture note is at http://budhi.uow.edu.au/staff/aditya/csci337/lecturenotes.html my lecture note /share/cs-pub/337/. Subprograms. Outline into basic terminology and behavior Implementation Issues parameter passing methods scope issues. Into: Subprograms. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 1

Announcement

Aditya’s lecture note is at

http://budhi.uow.edu.au/staff/aditya/csci337/lecturenotes.html

my lecture note

/share/cs-pub/337/

Page 2: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 2

Subprograms

Outline into basic terminology and behavior Implementation Issues

parameter passing methods scope issues

Page 3: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 3

Into: Subprograms

Fundamental building block of programsBenefits abstraction: increase readability of the

program, e.g. using names such as sort, reverse, etc

implementation hiding: modification can be made in isolation of the main program

modularity: programs can be partitioned into smaller pieces and thus more manageable

libraries: support standards and reusable codes

Page 4: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 4

Subprogram – Procedures and Functions

we use the term, subprogram, to cover both procedures proper and function procedures (beware: the text uses ‘procedures’ in 2 senses)

a subprogram is a construct for giving a name to a piece of coding

the piece of coding is the body of the subprogram

Page 5: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 5

Why Names?

Need to determine how to get from a name to its value:

an occurrence of a name in a program is within the scope of a declaration

a variable declaration can be bound to different storage location each time a subprogram is used

a storage location can hold different values at different stages of computation

Page 6: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 6

Basic Subprogram Characteristics & Terminology

single entry pointcalling program unit is suspended during execution of the subprogramcontrol returns to the calling program unit when execution of the subprogram terminates

subprogram call – the explicit request for the execution of the body of a subprogramonce called the body of the subprogram is said to be active

Page 7: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 7

Subprogram Call

Prefix notation is used for subprogram call

<subprogram-name> (<parameters>)

Pascal allows for dropping () when the subprogram is parameterless (e.g. eoln, realin), in C parentheses are not optional, e.g. getch()

Page 8: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 8

Actual and Formal Parameters

the parameters in a call are also referred to as actuals or arguments – the information to be passed by the caller

actual parameters are to be distinguished from formal parameters – variable local to the subprogram whose value is received from the caller, can be thought of as placeholder of the actual

parameters (in both senses) is the basic mechanism for communicating runtime values to subprograms

Page 9: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 9

Elements of Subprograms

a name for the declared subprogram

a body of local declarations and statements

the formal parameters (placeholders for actual parameters) with optionally types

optional result type

Page 10: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 10

Simple Example

function square

(x : integer) : integer;

begin

square := x*x

end;

name: squareformal: x (of type

integer)result type: integerbody: return the value of x*x

actuals: 0, 1, 2 etc.square(0), square(1),

etc.

Page 11: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 11

Binding of Actual to Formal

by position (arg1 -> param1, arg2 -> param2 etc) this is ok when a list is short

by keyword (at call: foo(LENGTH -> myleng, LIST -> mylist)

arguments can be in any order some can be keyword, some can be positional in the same

call but … must know the names of the formal parameters

default values for formal parameters: if no actual parameters are passed, the default value is used.

Page 12: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 12

Subprograms – Procedures

Procedures: collections of statements that define parameterized computations activated by a single call statement e.g.

read(ch); thought of as defining a new command or

action values are returned by

changes to non-local variables (if implemented by a PL) changes to the parameters

that can be seen by the call program unit

Page 13: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 13

Subprogram – Functions

Functions: semantically modeled on mathematical functions

called from within expressions e.g.

r * sin(angle) produces no side-effects (modifies neither

parameters nor non-local variables) produces a value that is returned to the point of the

call thought of as defining a new user-defined operator

Page 14: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 14

Pascal Examples

procedure getch;

begin

while eoln do readln;

read(ch)

end;

function square(x : integer) : integer;

beginsquare := x*x

end;

Beware:square := x*x

makes square looks like a variable, but ‘square’ is a function name.

Page 15: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 15

Recursive Subprograms

A subprogram is recursive if it can be activated from within its own body either directly by calling itself or indirectly through calls to others subprograms. A recursive subprogram can have multiple activations at the same time, example 5.4 in text, factorial function:

function f(n: integer): integer;begin

if n=0 then f :=1 else f := n*f(n-1);end;

Page 16: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 16

Another More Interesting Example: Recursive Subprogram

function f(x, y : integer) : integer;

begin

if x = 0 then f := y+1;

else if y = 0 then f := f(x-1, 1);

else f := f(x-1, f(x, y-1))

end;

Page 17: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 17

Answer

Ackermann’s function (named after Wilhelm Ackermann, 1896-1962, student of David Hilbert)

Ackermann’s function is a well-defined total function which has extremely fast growth rate – faster then any polynomials or exponentials

Page 18: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 18

Parameter Passing

caller and subprogram exchange information but how do they do it?

a convention or method is needed so that they both know what to do.

the convention can be view semantically and conceptually

Page 19: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 19

Formal Parameter Modes

Formal parameters are characterized by one of three semantic models in mode: can receive data from

corresponding actual out mode: can transmit data to

corresponding actual inout mode: do both of the above

Page 20: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 20

Conceptual Model of Data Exchange

value is copied

an access path is transmitted (a pointer) in this case formal and corresponding actual

are aliases – different names associated with a single address

Page 21: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 21

Method 1: Pass-by-Value

initial values of formals copied from current values of actualsfinal values of formals are ‘lost’ at return time (like local variable)in modebenefit: actuals protected from changes in subprogram (but see ex. 5.7 in text, we want change in some cases)disadvantage: requires copies, costs time and space for large aggregates

Page 22: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 22

Example

c: array[1..10] of integer;m, n integer;

procedure p(i, j : integer) begini := i+1; j := j+2end;

at call 1: i = 2, j =3print 1: 2 3at call 2: i = 2, j = 4print 2: 2 1 4 8

m := 2; n := 3;

p(m, n); // call 1

write m, n; // print 1

m := 2;

c[1] := 1;

c[2] := 4;

c[3] := 8;

p(m, c[m]): //call 2

write m, c[1], c[2], c[3]//print 2

Page 23: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 23

Method 2: Pass-by-Reference

formal parameters are pointers to the actuals (must have an location)address computations are performed at the call sitechanges to the formal are thus changes to the actualsinout mode

Page 24: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 24

Pass-by-Reference continue

benefit: efficient but all accesses to the actuals are indirect via pointersdisadvantage: if only in mode is required, actuals may be

changed collisions due to aliasing

2 or more visible names for same location can cause side effect not visible from code itself more of this later after discussion of scope

Page 25: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 25

Example

procedure

swap(var x, y integer);

var z: integer;

begin

z := x; x := y; y := z;

end;

i := 2;

swap(i, A[i])

suppose A[2] = 99

x and i have same locationy and A[i] have same locationz := x; x := y; y := z;

producez := 2; i := 99; A[2] := z;

lvalue of x: location with assignable content

rvalue of x: content of location

Page 26: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 26

Method 3: Pass-by-result

no value is transmitted from the actual to the formal the formal acts as a local variable just prior to returning control back to caller, the value is passed back from the formal to the actual

the actual must be a variable, what if p(1,2)?

for output values only, used to indicate that a formal is intended solely for returning a resultout modetypically requires copying of values

Page 27: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 27

Method 4: Pass-by-Value-Result

initial values of formal copied from values of actualfinal values of formal copied back to actualcombined functionality of pass-by-value and pass-by-result for same parameterinout modeimplemented via value copy

parameter acts like local variable if the semantics require that the only change to the

actuals be for the assignment of return values (not immediate values), then copy-implementation is the only option

Page 28: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 28

Example (same as before)

c: array[1..10] of integer;m, n integer;

procedure p(i, j : integer) begini := i+1; j := j+2end;

m := 2; n := 3;

p(m, n); // call 1

write m, n; // print 1

m := 2;

c[1] := 1;

c[2] := 4;

c[3] := 8;

p(m, c[m]): //call 2

write m, c[1], c[2], c[3]//print 2

Page 29: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 29

Pass by Value-Result Example

call 1: initial: i = 2 j = 3 final: i = 3 j = 5 return: m and n set

to: 3 5

print 1: 3 5

call 2: initial: i = 2 j = final: i = 3 j = return: which element

of c is modified c[2] or c[3]?

print 2: if c[2] is modified: if c[3] is modified:

Page 30: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 30

More consideration of Pass by Result 1

With pass by result or pass by value result order of assignments and address computations is important

choice 1: perform return address computations at call time: on second return m set to 3; c[2] set to 6

Page 31: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 31

More consideration of Pass by Result 2

choice 2: perform return address computations at return time:

1. before any assignments: on second return: same as before but may not be if

procedure has side-effects

2. just before that assignment, in order:on second return: m set to 3; c[3] set to 6

Page 32: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 32

Method 5: Pass by Name

deferred after discussion of scope

Page 33: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 33

Role of Scope

Scope rules determine which declaration of a name x corresponds to the use of an occurrence of x

control the use of named entities such as variables functions/procedures types

the binding occurrence of an identifier x is the occurrence of x that introduces it, other occurrence are called bound occurrences.

Page 34: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 34

Scope Concepts

Scope – the range of statements in which a variable (name) is visible

a variable (name) is visible to a statement if that statement can reference that variable (name), I.e. read or write to its value

Blocks are constructs when enable new scope creation

local variables can be declared within them variable storage is allocated when the block is

entered

Page 35: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 35

Lexical / Static Scoping

an occurrence of a name x refers to the declaration of x in the smallest enclosing block (the current block or the enclosing one)

the corresponding occurrence / declaration may be established statically at compile time

C, Ada, Pascal

Page 36: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 36

Example: static scope, output LL

program L;var n : char;procedure W;begin writeln(n) end;procedure D;begin n := ‘D’; W end;begin { L }n := ‘L’; W; D end

n declared in L

occurrence of n in W

n redeclared in D, W called in D

W called in L

Page 37: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 37

Static Scoping Considerations

based on program textto connect name reference to a variable, complier must find the declarationsearch involved: search declarations first locally, then in increasing larger enclosing scopes until complier finds the given namevariable can be hidden by having a closer variable with the same name

Page 38: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 38

Renaming Principle

Static scoping enables the renaming of local variables without changing the meaning of the program

Renaming Principle: under static scope, consistent renaming of local variables (including formal parameters) by new ones has no effect on programs

Page 39: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 39

Example

procedure D;

var n: char;

begin

n := ‘D’;

W

end;

procedure D;

var r: char;

begin

r := ‘D’;

W

end;

Page 40: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 40

Copy Rule

The renaming principle may be applied until all local variables have distinct names, each name has only one declarationWhen all local variables are distinct, one may use a copy rule to transform a program: insert subprogram bodies in place of callsthe process of renaming local variables to be distinct and then applying the copy rule preserves lexical scoping

Page 41: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 41

Evaluation of Static Scoping

|Main

| |A

| | |C

| | |_

| |

| | |D

| | |_

| |B

| |

| | |E

main calls A B

A calls C D

B calls E

specification changed: D must now access some of B’s data.

lesson: changes are hard, static scoping encourages many globals

but inefficient since compiler may have difficult optimizing globals

Page 42: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 42

Dynamic Scoping

an occurrence of a name x refers to the closest declaration of x in the current execution context;the correspondence between occurrences and declarations is done dynamically at run time;dynamic scope is obtained by using copy rule without renamingLisp, APL, Perl

Page 43: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 43

Example: dynamic scope, output LD

program L;var n : char;procedure W;begin writeln(n) end;procedure D;begin n := ‘D’; W end;begin { L }n := ‘L’; W; D end

n declared in L

occurrence of n in W

n redeclared in D, W called in D

W called in L

Page 44: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 44

Dynamic Scoping Considerations

based on the calling sequences of programs not their text (temporal vs. spatial)

search involved: references to variables are connected to their declarations by searching back through the chain of calls that forced execution to the current point

Page 45: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 45

Evaluation of Dynamic Scoping

advantage: eliminates parameter passing in some cases when parameters defined in the caller are implicitly visible

disadvantage: poor readability difficult to debug run time overhead

Page 46: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 46

Method 5 Return: Pass by Name

actuals are textually substituted (literally a string is substituted) whenever formals are in the subprograma parameter is bound to access method at the time of the program call, but actual binding of value and address is delay until parameter is usedthe form of the argument dictates the implementation of pass-by-nameto avoid the unexpected result of producing dynamic scoping, renaming must be performed prior to substitution.

Page 47: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 47

Summary

pass-by value: Java, C, C++, Pascal, Ada, Algol68, Schemepass by reference: Java objects, C++ with &, some Fortrans, Pascal with var, COBOLpass by result: Adapass by value result: some Fortrans, Adapass by name: Algol 60

Page 48: Announcement

Week 8-9a Spring 2002 CSCI337 Organization of Prog. Lang. 48

Type Checking Parameters

check the types of the values being passed in are the same as those by the parameter definition

reliability/safety required if language is (strongly) type-safe Ada/Pascal/Fortran 90/Java do it Fortran 77 doesn’t

C/C++ originally C did not check number of parameters nor

types but ANSI C does.