1 / 44

Ch9: Parameter Passing

Ch9: Parameter Passing. Outline. Subprograms Parameter Passing Parameter correspondence Main Issues when designing subroutine in programming languages Parameter passing techniques. Characteristics of Subprogram. Each program has a single entry point

melva
Télécharger la présentation

Ch9: Parameter Passing

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. Ch9: Parameter Passing

  2. Outline • Subprograms • Parameter Passing • Parameter correspondence • Main Issues when designing subroutine in programming languages • Parameter passing techniques

  3. Characteristics of Subprogram • Each program has a single entry point • The calling program unit is suspended during execution of the called subprogram, which implies that there is only on subprogram in execution • Control always return to the caller when subprogram execution terminates • The two fundamental types of subprograms are: • procedure • functions

  4. Basic Definitions • Structure or definition of a subprogram construct is: special-word name (list of parameter) Fortran: subroutine Addr (parameters) Ada: procedure Addr (parameters) C: void Addr (parameters)

  5. Parameters • Formal parameters -> parts of the specification • Actual parameters ex) formal parameter void swap (first, second) { int temp; temp = first; first = second; second = temp; } end swap ---------- ---------- actual parameter swap (x, y);

  6. Parameters • The parameters in the subprogram header are called formal parameters. • Parameters include as arguments when the subroutine is called are named actual parameters note: a subroutine that return a value is called a function • A subroutine that does not return a value is usually called a procedure or method • Most programming languages require subroutine to be declared before they are used (forward)

  7. Parameter Correspondence Establishing the correspondence between actual parameters and formal parameters 1. Positional Correspondence The correspondence is established by pairing actual and formal parameters based on the positions of the actual and formal parameter list.

  8. Parameter Correspondence 2. Correspondence by explicit name In Ada the formal parameter is paired with each actual parameter they may be named explicitly in the calling statement. example: sub (y => B, x=> 32); formalactual

  9. Parameter Correspondence 3. Mixed keyword (Explicit name) and positional • Most languages that support keyword parameters allow both: Ada, Fortran, Dylan, Python • The first parameter in a list can be positional, and the remainder can be keyword parameters. ex) my_sub (Length, sub=>B, List =>my_array);

  10. Parameter Correspondence 4. Optimal parameters • Formal parameter list includes default values to be used if the corresponding actual parameter is missing. • This gives a very short way of writing certain kinds of overloaded function definitions

  11. Example in C++ int f(int a =1, int b=2, int c=3) {body} int f() { f(1, 2, 3); } int f(int a) { f(a, 2, 3); } int f(int a, int b) { f(a, b, 3); } int f(int a, int b, int c) { f(a, b, c); }

  12. Unlimited Parameter Lists • Some languages allow actual parameter lists of unbounded length: C, C++, and scripting languages like JavaScript, Python, and Perl • Library routines must be used to access the excess actual parameters • A hole in static type systems, since the types of the excess parameters cannot be checked at compile time int printf (char *format, .....) { body }

  13. Example in Ada function Compute_Pay (Income : Float; Exemptions : Integer := 1; Tax_Rate : Float) return Float; Pay := Compute_Pay (2000.0, Tax_Rate => 0.15);

  14. Main Issues when designing subroutine in programming languages • What parameter passing methods are used? • Are the types of the actual parameters checked against the types of the formal parameters? • Check for error condition • Are local variables statically or dynamically allocated? • Can subroutines definitions appear on other subroutines definitions?

  15. Main Issues when designing subroutine in programming languages (cont) • Can subroutines can be passed as parameters? • Can subroutines be nested? • If passed as parameters and/or nested, what is the referencing environment of a passed parameter? • Can a subroutine be generic?

  16. Parameter Passing Technique 1 Pass-by-Value • The value of the actual parameter is associated with the formal parameter • It is initialized using the value of the corresponding actual parameter, before the called method begins executing • Simplest method • Widely used • The only method in real Java • Changing to a formal parameter do not affect the actual parameter

  17. Changes Visible to the Caller • When parameters are passed by value, changes to a formal do not affect the actual • But it is still possible for the called method to make changes that are visible to the caller • The value of the paramter could be a pointer( in Java, a reference) • Then the actual cannot be changed, but the object referred to by the actual can be

  18. int plus(int a, int b){ a += b; return a; } void f(){ int x = 3; int y = 4; int z = plus(x, y); } Example for Pass by Value(1) When plus Is starting

  19. Example for Pass by Value(2) name formal parameter type result function square(x : integer) : integer begin square := x * x end square(2) => 4

  20. Example for Pass by Value(3) procedure swap(a, b); integer a, b, temp; begin temp temp := a; a := b; b := temp; end; y swap(x, y); x effect

  21. Parameter Passing Technique 2 Pass-by-Result • The formal parameter is just like a local variable in the activation record of the called procedure( it is uninitialized) • After the called method finish execution, the final value of the formal parameter is assigned to the corresponding actual parameter • It is also called copy-out • It was introduced in ALGOL 68 • Sometimes used in Ada

  22. Example for Pass by Result void plus (int a, int b, by-result int c){ c = a + b; } void f(){ int x = 3; int y = 4; int z; plus(x, y, z); } 1 When plus is starting

  23. Example cont. When plus has returned when plus is ready to return 3 2

  24. Parameter Passing Technique 3 Pass-by-Value Result • The formal parameter is just like a local variable in the activation record of the called procedure • After the called method finish execution, the final value of the formal parameter is assigned to the corresponding actual parameter • It is also called copy-in/copy-out • It is initialized using the value of the corresponding actual parameter, before the called method begins execution

  25. Example for Pass by Value Result void plus(int a, by-value-result int b){ b += a; } void f(){ int x = 3; plus(4, x); } When plus is starting 1

  26. Example cont. When plus is ready to return When plus has returned 2 3

  27. Parameter Passing Technique 4 Pass-by-Reference • The address of the actual parameter is passed to the called method. This address is used as the formal parameter. • The formal parameter is an alias for the formal parameter • One of the earliest methods: Fortran • Most efficient for large object • Still frequently used

  28. Picture reference value copy-out copy-in reference value reference value reference

  29. Example for Pass by Reference (1) void plus (int a, by-reference int b) { b += a; } void f() { int x = 3; plus(4, x); } When plus Is starting 1 1 When plus is Ready to return 2

  30. Implementing Reference void plus( int a, by-reference int b) { b += a; } previous example void f() { int x = 3; plus(4, x); } void plus (int a, int *b) { *b += a; } void f() { C implementation int x = 3; plus(4, &x); By-reference = address by value }

  31. Aliasing • When two expressions have the same lvalue, they are aliases of each other • There are obvious cases: ConsCell x = new ConsCell (0, null); ConsCell y = x; A[i] = A[j] + A[k]; • Passing by reference leads to less obvious cases...

  32. Example for Pass by Reference (2) void sigsum( by-reference int n, by-reference int ans) { ans = 0; int I = I; while ( i <= n) ans +=i ++; } Int f() { int g() { int x, y; int x; x = 10; x = 10; sigsum (x, y); sigsum (x, x); return y; return x; } }

  33. Example cont void sigsum(by-reference int n, by-reference int ans) { ans = 0; int i = 1; while (i <= n) ans += i++; } int g() { int x; x = 10; sigsum(x, x); return x; } When sigsum Is starting CAR X : 10 n : RA ans : PAR i : ? Result : ? RA PAR

  34. Parameter Passing Technique 5 Pass-by-Name • Each actual parameter is evaluated in the caller’s context, on every use of the corresponding formal parameter • Introduce in ALGOL 60 • Now unpopular • The actual parameters is treated like a “Little anonymous function” (Thunk) • Whenever the called method needs the value of the formal, it calls the Thunk to get it.

  35. Example for Pass by Name(1) procedure swap(a, b); integer a, b, temp; begin temp := a; a := b; b := temp; end 0 1 2 3 4 . . . . A A[3] = 10 I = 2

  36. Example 1 cont. swap(I, A[3]){ temp := 2 I := A[3] = 10 A[10] := temp } swap(I, A[I]){ temp := 2 I := A[2] = 10 A[10] := temp }

  37. Example for Pass by Name(2) voidf( by-name int a, by-name int b){ b = 5; b = a; } int g(){ int i = 3; f(i + 1, i ); return; } Thunk Thunk i + 1 i when f() is starting (Final value of a is 6)

  38. Jensen’s Device Real procedure sum(j, lo, hi, Exp) Value lo, hi; Real Exp; Integers j, lo, hi; Begin Real RTN; RTN := 0; FOR I = lo step 1 UNTIL hi do RTN := RTN + Exp; SUM : = RTM End x[ i ] * i

  39. Jensen’s Device Example 10 2 y = 3x - 5x + 2 i = 1 y = sum(x, I, 10, 3 * x * x - 5 * x + 2) Each time through the loop, evaluation of the expression is actually the evaluation of 2 3x - 5x + 2 Procedure swap(a, b); Integer a, b, temp; Begin temp := a; a := b; b := temp; End

  40. Jensen’s Device Example cont. i = 2 x[i] = 5 Effect of calling swap(i, x[i]) Before the call: i = 2, x[2] = 5 What do we expect: i = 5, x[2] = 2 After the call: i = 5, x[2] = 5, x[5] = 2 side effect temp 2 i A[i] = 2 A[i] = A[5] = 2

  41. Thunks Function XjThunk() : Real address VAR Expi : Real Begin Expi := x[i] * i XjThunk := address(Expi) End

  42. Specification Issues • Are these just implementation techniques, or part of the language specification? • Depends on the language: • Without side-effects, parameter-passing technique may be undetectable by the programmer • Even with side effects, some languages specify the parameter passing technique only partially

  43. Without Side Effects • Big question are parameters always evaluated, or only if they are really needed? • Cost model may also be used by the programmer • Is re-evaluation of a formal expensive? • Does parameter-passing take time proportional to the size of the object?

  44. With Side Effects • A program can detect which parameter-passing technique is being used by the language system • But it may be an implementation detail that programs are not supposed to depend on: it may not be part of the specification of the language • Case in point: Ada

More Related