9

In my principles of programming class we are talking about different calling methods. Some we discussed were:

  • call by value
  • call by reference
  • call by value/result
  • and call by name

I can't find an example of how call by name works. Anyone care to give me an example? I think that when you take an xml file as input this is similar to call by name. Could someone give me a more traditional example?

sixtyfootersdude
  • 25,859
  • 43
  • 145
  • 213
  • I don't know of any modern examples of call by name. It was a very bad idea back in ALGOL 60, and I doubt that has changed. – John Saunders Jul 25 '10 at 21:47
  • What kind of example are you looking for? An example of a language that uses call-by-name or an example of what evaluating an expression using call-by-name would look like? – sepp2k Jul 25 '10 at 21:51
  • 1
    @sepp2k, both could be useful. I am not sure, but from what I have figured out so far it seems like a pretty dead idea (correct me if I am wrong) but it is included in the course material (maybe just for historical reasons?) – sixtyfootersdude Jul 25 '10 at 22:06

3 Answers3

9

I'll work in a hypothetical programming language. Let's assume we have a function p(x) that prints out x and then returns it. Now let's define a function:

function foo(x, y) { return y+1; }

Now let's call it with some arguments:

foo(p(123),p(456))

x and y are going to be substituted for the parameters, so the call to foo above is going to result in:

return p(456)+1;

So we're going to print 456 to the screen and return 457. In another evaluation strategy, we would evaluate the function parameters first (printing 123 and 456 to the screen in the process) and then substitute 456 for y in the function body, eventually returning 457.

It's contrived, but I hope you get the idea. It's all about substitution.

Gian
  • 13,735
  • 44
  • 51
  • so would call by name also call call by reference? value? Anything specific? – sixtyfootersdude Jul 25 '10 at 22:12
  • @sixtyfootersdude, that sentence made my brain hurt. Care to re-state it? – Gian Jul 25 '10 at 22:18
  • Sure. Lets say you have this (**Call by name**): `int x = 0; foo(t(x)); print(x);` if it was *also* "call by value" this would print 0. If it was *also* "call by reference" it *could* print something else. – sixtyfootersdude Jul 25 '10 at 22:58
  • Gian! Come back and answer him! – temporary_user_name Dec 14 '12 at 06:57
  • I'm still fairly certain I don't understand the question. In a call-by-reference scenario, it is always possible that the value being referenced could be modified, so the expected semantics of name-passing don't quite hold up to scrutiny. Other than that, I'm still not sure what is being asked. – Gian Dec 14 '12 at 23:10
3

http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_name

In call by value, you evaluate arguments, retrieving values that are then passed to the function. foo(bar()) is evaluated as arg = bar();, then foo(arg) is called, and in the body of the function, this newly allocated arg variable is accessible, modifiable, ...

In call by name, you substitues in the body of the function any references to arguments by their code used during the call. Then, evaluating the body, you will evaluate the arguments. foo(bar()) with foo(arg) { return arg; } will be evaluated as foo(arg) { return bar(); }

Scharron
  • 17,233
  • 6
  • 44
  • 63
1

Call by name work as call by reference when actual parameter be scaler, but be different when actual parameter is expression or array then actual parameter is re-evaluated on each access.

here is simple example

begin
    integer n;
    procedure p(k: integer);
        begin
        print(k);
        n := n+1;
        print(k);
        end;
    n := 0;
    p(n+10);
    end;
  • call by value output => 10 10
  • call by name output => 10 11
Abdo
  • 600
  • 6
  • 9