480 likes | 655 Views
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.
E N D
Announcement Aditya’s lecture note is at http://budhi.uow.edu.au/staff/aditya/csci337/lecturenotes.html my lecture note /share/cs-pub/337/ CSCI337 Organization of Prog. Lang.
Subprograms Outline • into • basic terminology and behavior • Implementation Issues • parameter passing methods • scope issues CSCI337 Organization of Prog. Lang.
Into: Subprograms • Fundamental building block of programs • Benefits • 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 CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
Basic Subprogram Characteristics & Terminology • single entry point • calling program unit is suspended during execution of the subprogram • control 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 subprogram • once called the body of the subprogram is said to be active CSCI337 Organization of Prog. Lang.
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() CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
function square (x : integer) : integer; begin square := x*x end; name: square formal: x (of type integer) result type: integer body: return the value of x*x actuals: 0, 1, 2 etc. square(0), square(1), etc. Simple Example CSCI337 Organization of Prog. Lang.
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. CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
procedure getch; begin while eoln do readln; read(ch) end; function square (x : integer) : integer; begin square := x*x end; Beware: square := x*x makes square looks like a variable, but ‘square’ is a function name. Pascal Examples CSCI337 Organization of Prog. Lang.
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; CSCI337 Organization of Prog. Lang.
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; CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
Method 1: Pass-by-Value • initial values of formals copied from current values of actuals • final values of formals are ‘lost’ at return time (like local variable) • in mode • benefit: 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 CSCI337 Organization of Prog. Lang.
c: array[1..10] of integer; m, n integer; procedure p(i, j : integer) begin i := i+1; j := j+2 end; at call 1: i = 2, j =3 print 1: 2 3 at call 2: i = 2, j = 4 print 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 Example CSCI337 Organization of Prog. Lang.
Method 2: Pass-by-Reference • formal parameters are pointers to the actuals (must have an location) • address computations are performed at the call site • changes to the formal are thus changes to the actuals • inout mode CSCI337 Organization of Prog. Lang.
Pass-by-Reference continue • benefit: efficient but all accesses to the actuals are indirect via pointers • disadvantage: • if only in modeis 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 CSCI337 Organization of Prog. Lang.
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 location y and A[i] have same location z := x; x := y; y := z; produce z := 2; i := 99; A[2] := z; lvalue of x: location with assignable content rvalue of x: content of location Example CSCI337 Organization of Prog. Lang.
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 result • out mode • typically requires copying of values CSCI337 Organization of Prog. Lang.
Method 4: Pass-by-Value-Result • initial values of formal copied from values of actual • final values of formal copied back to actual • combined functionality of pass-by-value and pass-by-result for same parameter • inout mode • implemented 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 CSCI337 Organization of Prog. Lang.
c: array[1..10] of integer; m, n integer; procedure p(i, j : integer) begin i := i+1; j := j+2 end; … 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 Example (same as before) CSCI337 Organization of Prog. Lang.
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: Pass by Value-Result Example CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
More consideration of Pass by Result 2 • choice 2: perform return address computations at return time: • before any assignments: on second return: same as before but may not be if procedure has side-effects • just before that assignment, in order: on second return: m set to 3; c[3] set to 6 CSCI337 Organization of Prog. Lang.
Method 5: Pass by Name • deferred after discussion of scope CSCI337 Organization of Prog. Lang.
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. CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
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 Example: static scope, output LL n declared in L occurrence of n in W n redeclared in D, W called in D W called in L CSCI337 Organization of Prog. Lang.
Static Scoping Considerations • based on program text • to connect name reference to a variable, complier must find the declaration • search involved: search declarations first locally, then in increasing larger enclosing scopes until complier finds the given name • variable can be hidden by having a closer variable with the same name CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
procedure D; var n: char; begin n := ‘D’; W end; procedure D; var r: char; begin r := ‘D’; W end; Example CSCI337 Organization of Prog. Lang.
Copy Rule • The renaming principle may be applied until all local variables have distinct names, each name has only one declaration • When all local variables are distinct, one may use a copy rule to transform a program: insert subprogram bodies in place of calls • the process of renaming local variables to be distinct and then applying the copy rule preserves lexical scoping CSCI337 Organization of Prog. Lang.
|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 Evaluation of Static Scoping CSCI337 Organization of Prog. Lang.
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 renaming • Lisp, APL, Perl CSCI337 Organization of Prog. Lang.
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 Example: dynamic scope, output LD n declared in L occurrence of n in W n redeclared in D, W called in D W called in L CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
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 CSCI337 Organization of Prog. Lang.
Method 5 Return: Pass by Name • actuals are textually substituted (literally a string is substituted) whenever formals are in the subprogram • a 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 used • the form of the argument dictates the implementation of pass-by-name • to avoid the unexpected result of producing dynamic scoping, renaming must be performed prior to substitution. CSCI337 Organization of Prog. Lang.
Summary • pass-by value: Java, C, C++, Pascal, Ada, Algol68, Scheme • pass by reference: Java objects, C++ with &, some Fortrans, Pascal with var, COBOL • pass by result: Ada • pass by value result: some Fortrans, Ada • pass by name: Algol 60 CSCI337 Organization of Prog. Lang.
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. CSCI337 Organization of Prog. Lang.