Problems and Pitfalls Pertaining to Parameter Passing Paradigms

Chapter: Problems and Pitfalls Pertaining to Parameter Passing Paradigms

There is no clear consensus on what is the right way to pass parameters. It is clearly most important that whatever language you are programming in, you should endeavor to understand its paradigms. This lab is not exhaustive in covering all the possibilities. We hope that it will provide you with a good foundation for experimenting and asking the right questions when you start work with a new language. In this chapter, we briefly mention some language features (idiosyncracies?) not mentioned elsewhere.

Our implementation of call-by-reference forces the user to always pass references. If you define a procedure using proc-by-ref then you are asking for trouble if you pass it arguments that are not boxes.


Exercise 7

What happens if you pass any of the following to a Scheme function defined using proc-by-ref which is expecing a box (i.e. a variable defined using our VAR macro or the equivalent.

Some languages do let you pass non-references to by-reference procedures. In such cases, the value of the actual argument is placed in a new location, and assignments to this location made by the called procedure have no effects visible in the calling procedure.

Another issue that you might like to think about is the feasibility of combination of protocols. Does call-by-need mesh well with call-by-reference in any meaningful way? Ask yourself such questions and try to answer them to yourself.

With call-by-text and call-by-need, delayed evaluation can cause unexpected problems.


Exercise 8

Imagine that your main procedure has declared a vector arr of 4 integers, and has initialized them so that the array contains the values arr[0] = 3, arr[1] = 2, arr[2] = 1, arr[3] = 0. Further imagine that you have written a procedure swop to exchange the contents of two integer variables, as follows.
(define swop
  (proc-by-??? (a b)
    (LOCALVAR ((temp 'notyet))
      (:= temp a)
      (:= a b)
      (:= b temp))))
In the above, the meaning of (:= x y) will vary depending upon the calling protocol. Make reasonable assumptions. Define a variable i to have the value 0. Now make a call to (swop i arr[i]).

What will be the resulting values of i and the array arr if the parameter passing protocol is:

Do this exercise as a thought experiment. To do this question by writing actual code would require way too much coding.


rhyspj@gwu.edu