subroutines
play

Subroutines Control abstraction Subroutine design If a subroutine - PowerPoint PPT Presentation

Subroutine Subroutines Control abstraction Subroutine design If a subroutine does not fit on the screen, it is too long Problem should be divided into smaller pieces Subroutine = If some task X is needed twice, make it a


  1. ● Subroutine Subroutines – Control abstraction ● Subroutine design – If a subroutine does not fit on the screen, it is too long • Problem should be divided into smaller pieces Subroutine = – If some task X is needed twice, make it a procedure (statement) - no return value subroutine. - side effects • When are two seemingly different things the function (expression) same? - return value • When are seemingly similar things different? - (no side effects in some languages) 189

  2. ● Subroutine header ● Subroutine definition (prototype) Terminology ● Subroutine body (definition) of – header (interface) ja body (functionality) ● Subroutine call (activation) subroutines ● Active subroutine • name – When a subroutine has been called, it has • Formal parameteres started execution, • name and type but not finished it. ● Parameters • Result type (function) procedure P ( parameters ) • body – Formal and actual void P ( parameters ) ● Parameter profile • Definitions and statements – Number, order, and types of formal parameters • (invariants) • (exceptions) 190

  3. ● Formal parameters (header) ● Actual parameters (call) Parameters ● Parameter binding – Binding actual parameters to formal ones – Type checks – Arity-check non-ANSI-C: ● (C (non-ANSI)) void fun ( i, j ) – (Number and types are not checked) int i, j; – (A varying number of params is allowed) { ... } ... • e.g., printf ( also ANSI-C) fun ( 1, 2, 3 ); 191

  4. ● pass-by-value ● pass-by-result Parameter ● pass-by-value-result passing ● pass-by-reference mechanisms ● (pass-by-name) 192

  5. ● Information passing from caller to procedue Value – Formal parameter is a local parameters variable (pass-by- – Formal parameter gets the value) same value as the actual parameter at call-time – Changes in the formal parameter do not affect the actual parameter 193

  6. ● Passing information in both directions ● Formal parameter is a reference to the Reference variable in the actual parameter parameters ● Variable is defined before the call and (pass-by- remains the same throughout the execution reference) ● Changes in the formal parameter are visible immediately in the variable ● Requires actual parameter to be a variable (lvalue) ● Aliasing can occur (same variable accessed in multiple ways) 194

  7. • Passing information in one direction – Formal parameter is a reference to Constant the variable in the actual parameter reference – Variable is defined before the call parameters and remains the same throughout (pass-by- the execution constant) – Formal parameter cannot be changed – (Changes in actual parameter are visible in procedure) – Good if copying values is costly (memory/time) 195

  8. • Like pass-by-value, but information moves in both directions Value-result – Formal parameter is a local parameters variable – Initial value like in pass-by-value – End value of formal parameter is copied into the actual parameter – Differences to pass-by-reference: no aliasing, no requirement for common memory (remote calls) – If exceptions occur, parameter not changed 196

  9. • Passing information from the procedure to the caller Result – Formal parameter a local parameters variable – End value is copied into the actual paremeter – At call time no value is passed to the procedure! – Rare, can be used for "multiple return values" 197

  10. • Not any more used in any major language • The actual parameter expression is Pass-by- (re)calculated every time the formal name parameter is used • Variable names in the parameter expression still refer to the scope of the caller • (A little bit like lazy evaluation, but value is possibly calculated several times) • Another interpretation: a lambda containing the actual parameter expression is passed to the function • C/C++ preprocessor macro (#define) parameters resemble pass-by-name (but not quite) 198

  11. ● Java & Python – value parameters for primitive types Parameter – Reference parameters for classes – (depends on the definition of "variable" vs "object") passing in ● C – Value parameters (array = reference) languages – Name parameters in #define-macros (almost) ● C++ – Value or reference ● Ada – value parameters (in) – result parameters (out) – value-result or reference parameters (in out) ● Haskell Name parameters – Think about it! ??? in Algol60 199

  12. ● Same memory location/ variable may be referenced with multpile Observations names about – Readability suffers! aliasing – E.g., global variable passed as reference ● Birth of a an alias: – Parameter passing (pass-by-reference) – Pointer/reference variables ● Possibility of aliasing prevents compiler optimizations 200

  13. ● Static allocation (e.g., Fortran) Subroutine – Every variable is allocated at compile time memory – Waste of memory management – Prevents recursion – Enables bad style (e.g., use of values generated at previous instances) ● Dynamic allocation (modern pls) – Activation records for subroutine calls – Stack allocation for activation records – Memory is allocated only when called 201

  14. • Call of subroutine 1. Pass the return address to the Subroutine subroutine (PC + 1) implementation 2. Pass the actual parameters (simple) 3. Move control to the subroutine 4. Assign local variables • Return from subroutine 1. Move the return value to the place reserved for it Local variables 2. Move the parameter values to the real Return address parameters (value-result or result) Parameters 3. Move control back to the caller (return Result address) 202

  15. ● Recursion – Storing program counter is not sufficient More – Each recursive call has it's own complex parameters, local variables and return subroutines value – Dynamic link points the place of calling ● Nested subroutines – Static link is needed to point to the outer scope: • Outer subroutine execution cannot end before all its inner subroutines have ended • A subroutine can call only the subroutines that are in its scope 203

  16. ● Function result: place for the return value ● True parameters Activation – Value parameters: as local variables records – Reference parameters: space for address/pointer ● Return address: calltime PC + 1 ● (State: saving registers etc.) Local variables ● Static (access) link Paikalliset muuttujat (Parametrit) (Parametrit) Parametrit Parametrit Parametrit (state) (Funktion tulos) Funktion tulos Funktion tulos Dynamic link – Points one scope up, i.e., what is the ”parent scope” Return address ● Dynamic (control) link Static link – Old top of the stack, i.e., a calling subroutine's activation Parameters record Function result ● Local variables Local variables – Also possible temporary, compiler generated variables Paikalliset muuttujat (Parametrit) (Parametrit) Parametrit Parametrit Parametrit ... 204

  17. Activation Static and dynamic links record stack B procedure A ... in point 2 dyn. link procedure B stat. link < point2 > end B; D ... dyn. link procedure C stat. link procedure D C < point1 > ... B; dyn. link end D; stat. link D; A end C; ... C; dyn. link end A; stat. link 205

  18. ● Can be implemented like subroutines Code – Paremeterless subroutines blocks – Always called in the same place ● Streamlining implementation – The required memory for local variables of the block is static – Entry and exit to and from blocks happens in textual order of the code – Memory can be allocated after local variables 206

  19. C++/C/Java: Implementing void main ( ) { blocks int x, y, z; e while ( ... ) { d Block int a, b, c; variables c ... b / g while ( ... ) { a / f int d, e; main- z variables ... y } } x while ( ... ) { main int f, g; ... ... ... } ... ... } 207

  20. ● Lambdas are nameless functions., but can refer to variables of the scope where they were created: they Implementation capture variables of lambdas ● When a lambda expression is evaluated, a closure is produced: a "thing" that contains the lambda's code (function) and a reference to the lambda's environment ● Environment reference can be implemented as pointers to used variables only or to the entire activation record ● What happens if a lambda outlives its environment? (returned as return value, for example?) ● C++: lambdas can copy the used variables instead of using references, problem solved? ● Many functional languages allocate activation records on heap , leaving their destruction to garbage collection (activation record can outlive its function). 208

  21. ● Binding by position Parameter – Binding is done according to order ● Binding by keywords binding procedure F ( i, j: in Integer; a, b: out Float ); Ada: ... F ( a => my_a, b => my_b, i => 0, j => 2 ); ● Default values procedure Increment ( i: in out Integer; by: in Integer := 1 ); ... Increment ( x ); 209

  22. ● Ada, Python Rules for function ComputePay ( default Income: Float; Exemptions: Integer := 1; parameters TaxRate: Float ) return Float; ... Pay := ComputePay ( 2000.0, TaxRate => 0.15 ); ● C++: – Default parameters must be the last ones in the list 210

Recommend


More recommend