Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Parameter Passing (Section 8.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott."— Presentation transcript:

1 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 2 Parameter Passing Notation for function calls: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)); | x tothe n = x * (x tothe (n-1)); Control abstraction - example: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 b) ifTrue: [max <- a] ifFalse: [max <- b]."Smalltalk" –Lisp and Smalltalk - no syntactic distinction between control statements and function calls

3 3 Parameter Modes Issues: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 4 Parameter Modes Main parameter-passing 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 SpeedSpeed SafetySafety –better to pass a large object by reference –if a subroutine is allowed to change the actual parameter - pass it by reference Semantic issue: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 5 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? 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

6 6 Parameter Modes C –Permissible operations – –Change on actual parameter – –Alias – read and write 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 7 Parameter Modes PascalPascal –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 8 Parameter Modes Modula-3Modula-3 –call by value (small objects) or call by reference (large ones) –READONLY mode can be specified to prohibit changes FortranFortran –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)Languages with reference model (Smalltalk, Lisp, Clu) –everything is a reference anyway –“call by sharing”

9 9 Parameter Modes AdaAda –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 10 Parameter Modes AdaAda –for composite types – pass either a value or a reference –program is "erroneous" if the results are different: –Passing values – –Passing addresses – print 4 print 5

11 11 Parameter Modes C++C++ –same modes as in C, plus references: int i; int &j = i; i = 2; j = 3; cout << i;// 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: 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 12 Parameter Modes C++C++ –can also return references from functions: cout << a << b << c; // equivalent to: –if it returned a pointer: *(*(cout.operator<< (a)).operator<< (b)).operator<< (c); // or equivalently (no more elegant syntax): ((cout.operator<< (a)).operator<< (b)).operator<< (c); // the << operator returns a reference to the output stream *(*(cout << a) << b) << c;

13 13 Call by Name Call by name (Algol 60, Simula)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);

14 14 Summary


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

Similar presentations


Ads by Google