1 / 12

CS 152, Programming Paradigms Fall 2011, SJSU

CS 152, Programming Paradigms Fall 2011, SJSU. Jeff Smith. Parameter passing methods. Parameters may be used for input or output or both. Parameter values may be copied from argument values. Or parameters may be aliases of actual arguments. Parameter passing in Ada.

kineks
Download Presentation

CS 152, Programming Paradigms Fall 2011, SJSU

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. CS 152, Programming ParadigmsFall 2011, SJSU Jeff Smith

  2. Parameter passing methods • Parameters may be used for input or output or both. • Parameter values may be copied from argument values. • Or parameters may be aliases of actual arguments.

  3. Parameter passing in Ada • Ada distinguishes among IN, OUT, and IN OUT parameters. • The first case is the default. • In Ada, IN parameters cannot be assigned to. • So they may be passed by reference. • If OUT parameters can be read, they're just IN OUT parameters

  4. Conventional terminology • pass by value • IN parameter, passed as copy • pass by reference • IN OUT parameter, passed as reference • pass by result • OUT parameter, passed as copy • pass by value-result • IN OUT parameter, passed as copy

  5. Pass by value • is often the default method • is eager evaluation • may require much copying • required in C • recall that array variables are effectively pointers • required in Java • recall that object variables are effectively pointers

  6. Pass by value and constant parameters • If pass-by-value parameters are not mutable, they may be implemented as references • thus saving the expense of copying • If pass-by-value parameters are mutable, they may be implemented as local variables • initialized to the actual arguments

  7. Pass by reference • is available in C++ and Pascal • It can be implemented just by passing the address of the argument. • and having the procedure dereference it as needed. • In C this can be done by the programmer • by passing pointers. • The actual arguments must be L-values -- so they can't be null.

  8. Pass by value vs. pass by reference • Unlike pass by value, pass by reference leads to aliasing (cf. T&N, p 233). • But unlike pass by value, pass by reference saves the expense of copying.

  9. Pass by value-result • can be implemented with one copy in each direction • can differ from pass by reference • if there is aliasing and side effects • cf. T&N, p. 234, or Louden, p. 321 • IN OUT parameters in Ada-95 are passed by value-result if they are elementary. • and by reference if they are composite.

  10. Pass by name • is different than the other methods. • It is often confusing but sometimes useful. • Think of it as textual substitution • of the arguments for the parameters • It can give results different from the other methods, when side effects are involved.

  11. Pass by name and lazy evaluation • Pass by name allows the defining of functions that don't evaluate all of their arguments. • or don't evaluate them all immediately • This approach is called lazy (or delayed) evaluation, vs. eager evaluation. • It requires arguments to be passed unevaluated • if they’re variables, their names are passed. • Scheme and Haskell use it to construct (implicitly) infinite objects.

  12. Applicative vs. normal order • A related issue is the distinction between applicative and normal order in evaluation. • Applicative order corresponds to bottom up evaluation of a syntax tree. • It’s more commonly used in languages. • In normal order, arguments are passed unevaluated, as in pass by name. • Note: (f x) is evaluated twice in normal order evaluation of (square (f x)).

More Related