CSCI 431 Programming Languages Fall 2003 Parameter Passing

  • Slides: 14
Download presentation
CSCI 431 Programming Languages Fall 2003 Parameter Passing (Section 8. 3) A compilation of

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

Parameter Passing • Notation for function calls: – prefix notation f(a, b) – Cambridge

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; (if (> a b) (setf max a) (setf max b)) (a > b) if. True: [max <- a] if. False: [max <- b]. (* Pascal *) ; Lisp "Smalltalk" – Lisp and Smalltalk - no syntactic distinction between control statements and function calls 2

Parameter Modes • Issues: – implementation mechanism (what is it passed? ) » »

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 3

Parameter Modes • Main parameter-passing modes: – call by value » the value of

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 – better to pass a large object by reference • Safety – 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? 4

Parameter Modes • C – – – everything is passed by value arrays are

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 (&v 1, &v 2); – what if we want to change a pointer? int * p; void my_allocate (int * x) { x = new int; . . . } my_allocate (p); Wrong int * p; void my_allocate (int ** x) { *x = new int; . . . } my_allocate (&p); 5

Parameter Modes • C – – – Permissible operations – read and write Change

Parameter Modes • C – – – Permissible operations – read and write Change on actual parameter – no Alias – 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 6

Parameter Modes • Pascal – programmer's choice - call by value or call by

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 7

Parameter Modes • Modula-3 – call by value (small objects) or call by reference

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” 8

Parameter Modes • Ada – three parameter modes: » in - read only »

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 9

Parameter Modes • Ada – for composite types – pass either a value or

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

Parameter Modes • C++ – same modes as in C, plus references: void swap

Parameter Modes • C++ – same modes as in C, plus references: void swap (int & a, int & b) { int t = a; a = b; b = t; }. . . swap (v 1, v 2); – 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) 11

Parameter Modes • C++ – can also return references from functions: cout << a

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; 12

Call by Name • Call by name (Algol 60, Simula) – parameters are re-evaluated

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 – Example (Jensen's device): To evaluate the sum: Call: y : = sum (3*x*x - 5*x + 2, x, 1, 10); 13

Summary 14

Summary 14