1 / 14

Parameter Passing (Section 8.3)

Parameter Passing (Section 8.3). CSCI 431 Programming Languages Fall 2003. A compilation of material developed by Felix Hernandez-Campos and Michael Scott. Parameter Passing. Notation for function calls:. prefix notation f(a, b) Cambridge Polish notation (f a b) infix notation operators

dacia
Download Presentation

Parameter Passing (Section 8.3)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parameter Passing(Section 8.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott

  2. Parameter Passing • Notation for function calls: • prefix notation f(a, b) • Cambridge Polish notation (f a b) • infix notation • operators • can be requested explicitly in ML: infixr 8 tothe; (* exponentiation *) fun x tothe 0 = 1.0 | x tothe n = x * (x tothe (n-1)); • Control abstraction - example: if a > b then max := a else max := b; (* Pascal *) (if (> a b) (setf max a) (setf max b)) ; Lisp (a > b) ifTrue: [max <- a] ifFalse: [max <- b]. "Smalltalk" • Lisp and Smalltalk - no syntactic distinction between control statements and function calls

  3. Parameter Modes • Issues: • implementation mechanism (what is it passed?) • value • reference • name • closure • legal operations (inside subroutine) • read • write • change performed on actual parameter? • yes • no • change visible immediately? • yes • no

  4. Parameter Modes • Main parameter-passing modes: • call by value • the value of actual parameter is copied into formal parameter • the two are independent • call by reference • the address of actual parameter is passed • the formal parameter is an alias for the actual parameter • Speed • Safety • better to pass a large object by reference • if a subroutine is allowed to change the actual parameter - pass it by reference • Semantic issue: • argument passed by reference - is it because it's large, or because changes should be allowed? • what if we want to pass a large argument, but not to allow changes?

  5. int * p; void my_allocate (int * x) { x = new int; ... } my_allocate (p); int * p; void my_allocate (int ** x) { *x = new int; ... } my_allocate (&p); Wrong Parameter Modes • C • everything is passed by value • arrays are pointers - what is passed by value is a pointer • to allow "call by reference" - must pass the address explicitly as a pointer: • void swap (int * a, int * b) • { int t = *a; *a = *b; *b = t; } • ... • swap (&v1, &v2); • what if we want to change a pointer?

  6. Parameter Modes • C read and write • Permissible operations – • Change on actual parameter – • Alias – no no • Speed - better to pass the address of a large object • How do we prohibit changes to the object? void f (const huge_record * r) { ... } r <=> pointer to constant huge_record • How do we define a constant pointer to huge_record? huge_record * const r

  7. Parameter Modes • Pascal • programmer's choice - call by value or call by reference procedure ( var a : integer, b : integer); (* a passed by reference *) ... (* b passed by value *) • if an array is passed without var - it will be passed by value!! • var should be used with: • arguments that need to be changed • large arguments • no mechanism to prohibit changes to an argument passed by reference

  8. Parameter Modes • Modula-3 • call by value (small objects) or call by reference (large ones) • READONLY mode can be specified to prohibit changes • Fortran • everything is passed by reference • does not require actual parameter to be a l-value • if it's a constant: • creates a temporary location to hold it • allowed to change the temporary • Languages with reference model (Smalltalk, Lisp, Clu) • everything is a reference anyway • “call by sharing”

  9. Parameter Modes • Ada • three parameter modes: • in - read only • out - write only • in out - read and write • for scalar types - always pass values • call by value/result • if it's an out or in out parameter - copy formal into actual parameter upon return • change to actual parameter becomes visible only at return

  10. Parameter Modes • Ada • for composite types – pass either a value or a reference • program is "erroneous" if the results are different: print 4 • Passing values – • Passing addresses – print 5

  11. Parameter Modes • C++ • same modes as in C, plus references: void swap (int & a, int & b) { int t = a; a = b; b = t; } ... swap (v1, v2); • safety - use const to prohibit changes to actual parameter • references - can be used not only for parameters: int i; int &j = i; i = 2; j = 3; cout << i; // prints 3 • implementation: i is an integer, j is a pointer to i • semantic: both are treated as integers (same operations apply to both)

  12. Parameter Modes • C++ • can also return references from functions: cout << a << b << c; // equivalent to: ((cout.operator<< (a)).operator<< (b)).operator<< (c); // the << operator returns a reference to the output stream • if it returned a pointer: *(*(cout.operator<< (a)).operator<< (b)).operator<< (c); // or equivalently (no more elegant syntax): *(*(cout << a) << b) << c;

  13. Example (Jensen's device): To evaluate the sum: Call: y := sum (3*x*x - 5*x + 2, x, 1, 10); Call by Name • Call by name (Algol 60, Simula) • parameters are re-evaluated in the caller's referencing environment every time they are used • similar to a macro (textual expansion) • pass a hidden routine (thunk) - re-evaluates the parameter

  14. Summary

More Related