Advanced Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Advanced Programming

Description:

Parameter Passing Giuseppe Attardi Universit di Pisa Call by value Copies argument Special cases: array struct typedef struct { int x, y} Pair; Pair q; zed(q); Pair ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 43
Provided by: Giusep74
Category:

less

Transcript and Presenter's Notes

Title: Advanced Programming


1
Advanced Programming
  • Parameter Passing
  • Giuseppe Attardi
  • Università di Pisa

2
Parameter Passing
3
Parameter Passing
  • Call by value
  • Call by reference
  • Call by result, value/result
  • C references
  • Closures as parameters
  • Call by name
  • Label parameters
  • Variable number of arguments
  • Function returns

4
Terminology
  • Expressions evaluate to R-values
  • Variables and components of variables of
    structured types also have an L-value, that is,
    an address where their R-value is stored
  • The assignment statement requires an L-value on
    the left-hand side (L stands for left) and an
    R-value on the right-hand side (R stands for
    right)

5
  • int x 3
  • x x
  • int v40
  • v3 v3
  • (x 3)
  • 3
  • int foo()
  • foo .
  • point.x 12

6
Value model, reference model
  • b 2
  • c b b b 1
  • a b c

Value model
Reference model
6
5
a
a
2
3
3
b
b
2
c
c
2
7
Example
  • class Point int x, y
  • Point p Point(3, 4)
  • Point p2 p
  • p.x 5
  • p2.x ?
  • foo(p)
  • int i 3
  • Integer I new Integer(3)

8
C
  • void foo (int x)
  • x 3
  • struct Point int x, y
  • void bar(Point p) p-gtx 3
  • Point p p.x 5
  • Point p2 p
  • bar(p2)

9
Reference model
  • In language using reference model, every variable
    is a L-value
  • When it appears in a R-value context, it must be
    dereferenced
  • Usually dereference is implicit
  • Examples
  • Algol68, Lisp, ML, Haskell, SmallTalk
  • Java mixed (value for built-in types)

10
Call by value
  • The value of the R-value of the actual parameter
    is copied into the formal parameter at invocation

11
Call by result
  • The value of the formal parameter is copied into
    the actual parameter (which must have an L-value)
    at procedure return.

12
Call by value/result
  • The parameter is treated as in value mode during
    invocation and as in result mode during return.

13
  • foo (inout x) x
  • foo1 (ref x) x x 1
  • foo1(v3)
  • s asdasd
  • foo1(s) // OK
  • foo1(asdasd) // KO
  • foo1(3) // KO
  • int y 7
  • foo1(y)
  • y?

14
  • foo2 (Point ref q) q new Point()
  • Point p new Point(3,4)
  • Point prev p
  • foo2(ref p)
  • p prev?

15
  • void foo (ref int x)
  • x x 3
  • x x/0
  • int z 2
  • foo(z)

16
Call by reference
  • The L-value of the formal parameter is set to the
    L-value of the actual parameter.
  • The address of the formal parameter is the same
    as the address of the actual parameter. Any
    assignment to the formal parameter immediately
    affects the actual parameter.

17
Note
  • // C
  • void foo(int ref x) x 1
  • // C
  • void foo(int x) x 1
  • int a3
  • a0 0
  • foo(ref a0) // C
  • foo(a0) // C
  • foo(p)

18
C Reference Datatype
  • C provides a Reference dataype, built from
    other types
  • If T is a type T is a new type called reference
    to T
  • One should avoid the confusion between the
    general concept of reference and a Reference Type
  • Also avoid the confusion with the notion of
    Reference types in Java and C
  • Reference Types are not pointers, even though
    they are implemented through pointers and hence
    accessing a reference type may involve an
    implicit dereference operation
  • Using Reference Types in parameters even though
    they are still passed by value.

19
Reference/Value Types
  • In programming language theory, a reference type
    is a data type that can only be accessed by
    references
  • Objects of a reference type are always
    dynamically allocated
  • value type objects instead can be manipulated
    directly and are copied when moved (assignment,
    parameter passing or function return)

20
C Reference Type Example
  • void foo(int x)
  • x x 1
  • int y 7
  • foo(y)
  • y ?
  • foo(3) // only feasible if x was
  • // declared as const int

21
Call by name
  • Every use of the formal parameter causes the
    actual parameter to be freshly evaluated in the
    referencing environment of the invocation point.
  • If the formal parameters L-value is needed (for
    example, the parameter appears on the left-hand
    side of an assignment), the actual parameters
    L-value must be freshly evaluated.
  • If the formal parameters Rvalue is needed, the
    actual parameters R-value must be freshly
    evaluated.

22
Call by name example
  • int sum(name int expr, name int j, int size)
  • int tot 0
  • for ( j lt size j)
  • tot expr
  • return tot
  • sum(Ai, i, n) / sum of vector elements /

23
  • int i
  • int A
  • int thunk() return Ai
  • sum(thunk, i, n)

24
Call by macro
  • Every use of the formal parameter causes the text
    of the actual parameter to be freshly evaluated
    in the referencing environment of the use point.

25
  • DEFINE foo(x) x x
  • foo(read())
  • read() read()
  • inline int foo(int x) return x x
  • int foo(int x, int y) x y return x
    y
  • Foo(a, a)

26
Parameter passing modes
Implementation Operations Change to actual? Alias?
Value Value RW No no
In, const Value, reference RO No Maybe
Out (C, Ada) Value, reference WO Yes Maybe
Value/result Value RW Yes No
Var, ref Reference RW Yes Yes
Sharing Value, reference RW Yes Maybe
In out Value, reference RW Yes Maybe
Name Closure RW Yes yes
27
Esercizio
  • swap(x, y) using just call-by-value
  • void swap(int x, int y)
  • int temp
  • temp x
  • x y
  • y temp
  • Does not work.

28
  • swap(x, y) using just call-by-name
  • int b10 swap(i, bi)
  • void swap(name int x, name int y)
  • int temp
  • temp x
  • x y
  • y temp

29
Note on
  • List l new List()
  • Student s new Student()
  • l.add(s)
  • float pi 3.14
  • l.add(pi)
  • (new Integer(314)).toString()
  • s.toString()

30
Note
  • int a new int3
  • int b new int5
  • swap(a, b)
  • a
  • swap(i, ai)
  • int temp temp x x y y temp
  • a3, b5 a5, b5 a5, b3
  • void swap(inout int x, inout int y)
  • int temp
  • temp x
  • x y
  • y temp

31
Exercise
  • swap(x, y) using just call-by-name
  • swap(name x, name y) gt
  • Does it work?

32
Call by name
  • Counter example
  • swap(i, Ai)
  • (i3, A3 4) gt (i4, A4 4, A3 unchanged)

33
Call by value/result
  • swap(i, Ai)works even in case (i2, A2 99)

34
Call by value
  • Copies argument
  • Special cases
  • array
  • struct
  • typedef struct int x, y Pair
  • Pair q
  • zed(q)
  • Pair foo() Pair p return p
  • Pair p foo()
  • stack.push_back(Pair(2,3))
  • int 200000 v
  • bar(v)
  • stdstring s bad(s)

35
  • int foo()
  • int v100
  • return v

36
Closures
  • First Class function objects

37
Downward Closure in Pascal
38
Lexical Environment of Functions
  • Because of nested lexical scope, even passing
    functions in Pascal requires passing the
    enclosing lexical environment

39
(No Transcript)
40
Closure in Python
  • def makeInc(x)def inc(y) return y
    xreturn inc
  • inc3 makeInc(3)
  • inc10 makeInc(10)
  • inc3(5) returns 8
  • inc10(5) returns 15

41
(No Transcript)
42
Closure as Classes
  • def makePair(x, y)def get(arg)
  • if arg x return x
  • elif arg y
  • return y
  • return get
  • p makePair(3, 5)
  • p(x) -gt 3
  • P(y) -gt 5
Write a Comment
User Comments (0)
About PowerShow.com