Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

When I write programs I using pass by value or pass by reference always seem to be logical methods. When learning about different programming languages I came across pass by name.

Pass by name is a parameter passing method that waits to evaluate the parameter value until it is used. See Stack Overflow pass by name question for more information on the method.

What I would like to know is: what are some good examples and/or reasons to use pass by name and should it be re-introduced into some more modern languages.

share|improve this question

3 Answers

up vote 3 down vote accepted

Some of the advantages are described in a description of pass by name linked to in your linked question's answer.

The advantages of pass-by-name are (paraphrased from the article):

  1. It has a simple semantic model as textual substitution.
  2. Modification and re-evaluation of argument expressions has useful applications, such as Jensen's device. (Ed: Described in the second section)
  3. Argument expressions are not necessarily evaluated. Here, y is not evaluated if x is false:

    boolean procedure and (x, y);
      boolean x, y;
    begin
        if x then return y else return false
    end;
    

Would I use it?

I see no reason to, even considering the advantages put forward.

I'd rather not even dynamically evaluate code but if I had to, anything I could do with pass-by-name I'd rather do with eval() (which offers greater control and security).

Pointers and short-circuit evaluation prevent expression evaluation as much as I need it, and I'm quite happy with the semantic models I use regularly.

share|improve this answer
eval() is just about as insecure as you can get. Unless you have a language in mind that can sandbox the eval'd statement (which would then mean it's nowhere near as flexible), which you really need to indicate. – Izkata Sep 6 '12 at 18:06

Scala is an example of modern language that supports call-by-name evaluation (as well as call-by-value, which is the default). As you said, the parameter is lazily evaluated.

This feature can be used for creating your own control structure. The code given by Axidos in his answer is a simple, but good example.

There are other examples in the book Programming in Scala. I'll try to post these examples here tonight.

share|improve this answer

Pass by name has some purpose.

Indeed we are saying that pass by name uses textual substitution or otherwise macro substitution.

If we are using pass by value or pass by reference we are merely passing values or addresses, but we can't pass an expression such as i*x[i]; (in pass by reference it evaluated into a temporary location whose address is passed ), but it is possible with pass by name.

For example if we want to perform the sum of the expression i*x[i] where i varies from 1 to 5 using a function, we can write a function that uses pass by reference as written below (don't mind the syntax)

  *procedure sum (int low, int high, int i , real exp)
  { 
       begin

         real S;
         S := 0;
         for i := low step 1 until high do
           {  S := S + Exp;
             Sum := S}
       end;*
 }

then a call such as sum(1,5,i,x[i]) will find out the sum of series i*x[i], where Exp gets literally substituted for i*x[i].

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.