csci 3136 principles of programming languages
play

CSCI 3136 Principles of Programming Languages Subroutines and - PowerPoint PPT Presentation

CSCI 3136 Principles of Programming Languages Subroutines and Control Abstraction Summer 2013 Faculty of Computer Science Dalhousie University 1 / 42 Basic Definitions Subroutines are what we normally call Functions, if they return a


  1. CSCI 3136 Principles of Programming Languages Subroutines and Control Abstraction Summer 2013 Faculty of Computer Science Dalhousie University 1 / 42

  2. Basic Definitions • Subroutines are what we normally call ◦ Functions, if they return a value, or ◦ Procedures, if they do not and thus are called for their side effects. • Subroutine parameters ◦ Formal parameters are the parameter names that appear in the subroutine declaration. ◦ Actual parameters are the values assigned to the formal parameters when the subroutine is called. 2 / 42

  3. Static Chains and Dynamic Chains Source code Program execution Execution stack 3 / 42

  4. Inline Expansion Inline expansion replaces a subroutine call with the code of the subroutine. 4 / 42

  5. Inline Expansion Inline expansion replaces a subroutine call with the code of the subroutine. Advantages • Avoids overhead associated with subroutine calls ⇒ faster code. • Encourages building abstractions in the form of many small subroutines. • Usually better than macros. 5 / 42

  6. Inline Expansion Inline expansion replaces a subroutine call with the code of the subroutine. Advantages • Avoids overhead associated with subroutine calls ⇒ faster code. • Encourages building abstractions in the form of many small subroutines. • Usually better than macros. Disadvantages • Code bloating. • Cannot be used for recursive subroutines. 6 / 42

  7. Parameter Passing Modes 7 / 42

  8. Parameter Passing Modes Call by value • A copy of the argument’s value is passed • Changes to the formal parameter do not affect the actual parameter 8 / 42

  9. Parameter Passing Modes Call by value • A copy of the argument’s value is passed • Changes to the formal parameter do not affect the actual parameter Call by reference • The address of the argument is passed • Formal parameter is an alias of actual parameter • Changes to the formal parameter affect the actual parameter • Actual parameter must be an l-value (e.g., not an arithmetic expression) 9 / 42

  10. Examples of Parameter Passing Modes (1) 10 / 42

  11. Examples of Parameter Passing Modes (1) FORTRAN • All parameters are passed by reference • Temporary variables are used to pass non-l-value expressions 11 / 42

  12. Examples of Parameter Passing Modes (1) FORTRAN • All parameters are passed by reference • Temporary variables are used to pass non-l-value expressions Pascal • Call by value is the default • Keyword var before formal parameter switches to call by reference Example: procedure sub(a : integer; var b : integer) 12 / 42

  13. Examples of Parameter Passing Modes (1) FORTRAN • All parameters are passed by reference • Temporary variables are used to pass non-l-value expressions Pascal • Call by value is the default • Keyword var before formal parameter switches to call by reference Example: procedure sub(a : integer; var b : integer) C • Call by value • If array, what is passed by value is a pointer • To simulate call by reference, pass a pointer 13 / 42

  14. Examples of Parameter Passing Modes (2) 14 / 42

  15. Examples of Parameter Passing Modes (2) Smalltalk, Lisp, Clu, ML • Reference model of variables, hence call by reference (more precisely call by sharing ) • In call by sharing, immutable objects may be passed by value 15 / 42

  16. Examples of Parameter Passing Modes (2) Smalltalk, Lisp, Clu, ML • Reference model of variables, hence call by reference (more precisely call by sharing ) • In call by sharing, immutable objects may be passed by value Ada • in parameters: pass information from the caller to the callee (call by value) • out parameters: pass information from the callee to the caller (“call by result”) • in out parameters: pass information in both directions (call by reference) 16 / 42

  17. Examples of Parameter Passing Modes (2) Smalltalk, Lisp, Clu, ML • Reference model of variables, hence call by reference (more precisely call by sharing ) • In call by sharing, immutable objects may be passed by value Ada • in parameters: pass information from the caller to the callee (call by value) • out parameters: pass information from the callee to the caller (“call by result”) • in out parameters: pass information in both directions (call by reference) C++ • Same as C but with the addition of reference parameters ( &a ) • For example, void swap(int &a, int &b) { int t = a; a = b; b = t; } 17 / 42

  18. Examples of Parameter Passing Modes (3) 18 / 42

  19. Examples of Parameter Passing Modes (3) Java • Call by value for primitive types • Call by reference for compound types (objects) 19 / 42

  20. Examples of Parameter Passing Modes (3) Java • Call by value for primitive types • Call by reference for compound types (objects) C# • Call by value is the default • ref and out keywords to force call by reference • Distinction between call by value and call by reference made at data type level: ◦ struct types are values. ◦ class types are references. 20 / 42

  21. Read-Only Parameters 21 / 42

  22. Read-Only Parameters A common practice in Pascal • Large values are passed by reference for efficiency reasons • High potential for bugs 22 / 42

  23. Read-Only Parameters A common practice in Pascal • Large values are passed by reference for efficiency reasons • High potential for bugs Read-only parameters address this problem : • Efficiency of call by reference • Safety of call by value 23 / 42

  24. Read-Only Parameters A common practice in Pascal • Large values are passed by reference for efficiency reasons • High potential for bugs Read-only parameters address this problem : • Efficiency of call by reference • Safety of call by value Modula 3: readonly parameters ANSI C, C++: const parameters 24 / 42

  25. Read-Only Parameters A common practice in Pascal • Large values are passed by reference for efficiency reasons • High potential for bugs Read-only parameters address this problem : • Efficiency of call by reference • Safety of call by value Modula 3: readonly parameters ANSI C, C++: const parameters When using call by value, declaring a parameter readonly or const is pointless. 25 / 42

  26. Closures as Parameters A closure ( a reference to a subroutine, together with its referencing environment) may be passed as a parameter. 26 / 42

  27. Closures as Parameters A closure ( a reference to a subroutine, together with its referencing environment) may be passed as a parameter. Languages that support this: • Pascal • Ada 95 (not Ada 83) • All functional programming languages 27 / 42

  28. Closures as Parameters A closure ( a reference to a subroutine, together with its referencing environment) may be passed as a parameter. Languages that support this: • Pascal • Ada 95 (not Ada 83) • All functional programming languages Restricted passing of functions in C/C++: • Functions not allowed to nest • No need for closures • Pointers to subroutines suffice 28 / 42

  29. Default (Optional) Parameters Default (optional) parameters need not be specified by the caller. If not specified, they take default values. Ada procedure put(item : in integer; width : in field := 11; base : in number base := 10 ); C++ void put(int item, int width = 11, int base = 10) { ... } Implementation is trivial. How? 29 / 42

  30. Named (Keyword) Parameters Named (keyword) parameters need not appear in a fixed order. 30 / 42

  31. Named (Keyword) Parameters Named (keyword) parameters need not appear in a fixed order. Languages that support this: • Ada • Common Lisp • Modula-3 • Fortran 90 • Python 31 / 42

  32. Named (Keyword) Parameters Named (keyword) parameters need not appear in a fixed order. Languages that support this: • Ada • Common Lisp • Modula-3 • Fortran 90 • Python Ada: • put(item => 37, base => 8); • put(base => 8, item => 37); • put(37, base => 8); 32 / 42

  33. Named (Keyword) Parameters Named (keyword) parameters need not appear in a fixed order. Languages that support this: • Ada • Common Lisp • Modula-3 • Fortran 90 • Python Ada: • put(item => 37, base => 8); • put(base => 8, item => 37); • put(37, base => 8); Implementation is once again trivial. How? 33 / 42

  34. Exception Handling An exception is an unexpected or abnormal condition arising during program execution. 34 / 42

  35. Exception Handling An exception is an unexpected or abnormal condition arising during program execution. They may be • generated automatically in response to runtime errors, • or raised explicitly in the program. 35 / 42

  36. Exception Handling An exception is an unexpected or abnormal condition arising during program execution. They may be • generated automatically in response to runtime errors, • or raised explicitly in the program. Typical semantics of exception handling • Exception handler lexically bound to a block of code. • An exception raised in the block replaces the remaining code in the block with the code of the corresponding exception handler. • If there is no matching handler, the subroutine exits and a handler is looked for in the calling subroutine. 36 / 42

  37. Use of Exception Handlers • Perform operations necessary to recover from the exception. • Terminate the program gracefully, with a meaningful error message. • Clean up resources allocated in the local block before re-raising the exception. 37 / 42

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend