Ch9: Parameter Passing - PowerPoint PPT Presentation

About This Presentation
Title:

Ch9: Parameter Passing

Description:

... parameter list includes default values to be used if the corresponding actual ... CAR. n : ans : i : ? RA. PAR. X : 10. RA. PAR. Result : ? When sigsum. Is ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 45
Provided by: hit51
Learn more at: http://www.cs.ucf.edu
Category:
Tags: ch9 | parameter | passing

less

Transcript and Presenter's Notes

Title: Ch9: Parameter Passing


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 -gt 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 gt B, xgt 32)
  • formal actual

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, subgtB, List gtmy_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 b2, int c3) 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 gt 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
Example for Pass by Value (1)
  • int plus(int a, int b)
  • a b
  • return a
  • void f()
  • int x 3
  • int y 4
  • int z plus(x, y)


a 3
b 4
return address
Previous Activation record
Result
x 3
y 4
z ?
return address
Previous Activation record
When plus Is starting
Current Activation record
19
Example for Pass by Value (2)
  • name formal parameter
    type result
  • function square(x integer) integer
  • begin
  • square x x
  • end
  • square(2) gt 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)

current AR
a 3
b 4
c ?
RA
Previous AR
x 3
y 4
z ?
RA
Previous AR
1
When plus is starting
23
Example cont.

When plus has returned
when plus is ready to return
3
current AR
2
current AR
a 3
b 4
c 7
RA
Previous AR
x 3
y 4
z ?
RA
Previous AR
x 3
y 4
z 7
RA
Previous AR
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(2, x)

When plus is starting
1
current AR
a 4
b 3
RA
PAR
x 3
RA
PAR
26
Example cont.

When plus is ready to return
When plus has returned
2
3
current AR
a 4
b 7
RA
PAR
x 3
RA
PAR
x 7
RA
PAR
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

x
reference
value

copy-out
copy-in
b
reference
value

x

reference
value

b
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
CAR
a 4
b
RA
PAR
x 3
RA
PAR
CAR
When plus is Ready to return
2
a 4
b 7
RA
PAR
x 7
RA
PAR
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 lt 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 lt 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
    callers 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
. . . .
5 3 11 10 12
A
A3 10 I 2
36
Example 1 cont.
  • swap(I, A3)
  • temp 2
  • I A3 10
  • A10 temp
  • swap(I, AI)
  • temp 2
  • I A2 10
  • A10 temp

37
Example for Pass by Name (2)
  • void f( 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


a
b
RA
PAR
i 3
RA
PAR
result?
CAR
when f() is starting
38
Jensens 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
Jensens 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
Jensens Device Example cont.
i 2
xi 5
  • Effect of calling swap(i, xi)
  • Before the call i 2, x2 5
  • What do we expect i 5, x2 2
  • After the call i 5, x2 5, x5 2

side effect
temp 2
i
Ai 2
Ai A5 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
Write a Comment
User Comments (0)
About PowerShow.com