Parameter Passing Section 8.3 - PowerPoint PPT Presentation

About This Presentation
Title:

Parameter Passing Section 8.3

Description:

if a subroutine is allowed to change the actual parameter - pass it by reference ... to allow 'call by reference' - must pass the address explicitly as a pointer: ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 15
Provided by: csU50
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: Parameter Passing Section 8.3


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
Parameter Passing
  • 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))
  • Control abstraction - example
  • if a gt b then max a else max b ( Pascal
    )
  • (if (gt a b) (setf max a) (setf max b)) Lisp
  • (a gt b) ifTrue max lt- a ifFalse max lt-
    b. "Smalltalk"
  • Lisp and Smalltalk - no syntactic distinction
    between control statements and function calls

3
Parameter Modes
  • 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
Parameter 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
  • Speed
  • Safety
  • better to pass a large object by reference
  • if a subroutine is allowed to change the actual
    parameter - pass it by reference
  • 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
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?

6
Parameter Modes
  • C

read and write
  • Permissible operations
  • Change on actual parameter
  • Alias

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 ltgt pointer to constant huge_record
  • How do we define a constant pointer to
    huge_record?

huge_record const r
7
Parameter Modes
  • Pascal
  • 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
Parameter Modes
  • Modula-3
  • call by value (small objects) or call by
    reference (large ones)
  • READONLY mode can be specified to prohibit changes
  • Fortran
  • 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)
  • everything is a reference anyway
  • call by sharing

9
Parameter Modes
  • Ada
  • 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
Parameter Modes
  • Ada
  • for composite types pass either a value or a
    reference
  • program is "erroneous" if the results are
    different

print 4
  • Passing values
  • Passing addresses

print 5
11
Parameter Modes
  • C
  • same modes as in C, plus references

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

int i int j i i 2 j 3 cout ltlt i //
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
Parameter Modes
  • C
  • can also return references from functions
  • cout ltlt a ltlt b ltlt c
  • // equivalent to

((cout.operatorltlt (a)).operatorltlt (b)).operatorltlt
(c) // the ltlt operator returns a reference to
the output stream
  • if it returned a pointer
  • ((cout.operatorltlt (a)).operatorltlt
    (b)).operatorltlt (c)
  • // or equivalently (no more elegant syntax)

((cout ltlt a) ltlt b) ltlt c
13
Call by Name
  • 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

14
Summary
Write a Comment
User Comments (0)
About PowerShow.com