0

So this is my code and I know the output for call by value is

Here is f
main: z = 15 

What would it be if a language used call by name instead of call by value?

int f() {
    cout << "Here is f" << endl;
    return 5;
}
int g(int a) {
    int x = a;
    int y = 2 * a;
    return x + y;
}
int main() {
    int z = g(f());
    cout << "main: z = " << z << endl;
}
  • 1
    What on Earth do you mean by 'call by name' and 'call by value'? – SergeyA May 17 '18 at 20:14
  • 2
    Call by name is just a variant of *lazy evaluation*, which is very common in functional languages. It should not be hard to guess a few possible variants. So please do guess, what do *you* think? And *why* do you think that? – Some programmer dude May 17 '18 at 20:14
  • 3
    @Someprogrammerdude how much do you charge for your cristal ball? – SergeyA May 17 '18 at 20:15
  • C++ doesn't have call by name. Your code calls g() with the value returned by calling f(). The way to find out what the output is would be to compile and run it. –  May 17 '18 at 20:15
  • 1
    @SergeyA It's easy when you have [the answer](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_name) :) – Some programmer dude May 17 '18 at 20:16
  • @Some "Call by name is an evaluation strategy where the arguments to a function are not evaluated before the function is called" - which is not what happens here. –  May 17 '18 at 20:17
  • @Someprogrammerdude ok, thanks :) – SergeyA May 17 '18 at 20:19
  • 1
    You can get something like call by name with macro functions. – Kevin May 17 '18 at 20:20
  • 1
    @NeilButterworth No I know that C++ doesn't have it. But the question is what would happen *if* C++ had it. It's a speculative question. And probably part of an exam or interview. – Some programmer dude May 17 '18 at 20:22
  • Okay so I think the same output will come out, because of the return 5 – Bronia Berlin May 17 '18 at 20:24
  • @Some OK, I misread the question. But that's a terrible question - who knows what would happen in some imaginary language? –  May 17 '18 at 20:24
  • Can one of you elaborate on when call-by-name would ever be preferable to call-by-value? The wikipedia article @Someprogrammerdude linked says , *when the argument is not used in the function it is sent to*. Additionally i can also think of the proper exception handling infrastructure not being present at the point of call-by-name. But I would like to know a more concrete use case :) – Srini May 17 '18 at 20:24
  • In my mind this boils down to "What would C++ do if it did something that it does not do". To which the answer is basically "Then it would be a different language". What's the point of this question? *I* don't get it.. – Jesper Juhl May 17 '18 at 20:27
  • 1
    @Srini Call by name is not popular - I can't think of a case when it would be preferable to call by value, particularly when you would need extra syntactical support for it, for minimal advantage. –  May 17 '18 at 20:27
  • 1
    It is only a question for my college class and I know Haskell uses Call by name, but it is not very popular. So maybe I can compare call by value and call by name, when I know how they change the output. – Bronia Berlin May 17 '18 at 20:30
  • @BroniaBerlin Think again... The function `f` have a *side effect*, in that it prints something. What it returns is kind of irrelevant, it's the side-effect of calling the function `f` that makes it an interesting thought experiment. If `f()` is not evaluated in the call to `g`, then when might it be called? And how many times? – Some programmer dude May 17 '18 at 20:30
  • But we can't know how call by name would work in C++ because no-one has even tried to specify such a thing. Your question basically makes no sense in the C++ domain. I am tempted to remove the C++ tag. –  May 17 '18 at 20:31
  • In the new programming language I expect `Here is f` to be printed 2 times and the result to remain the same as the call by value. Meaning `main: z = 15` – drescherjm May 17 '18 at 20:33
  • Wait could it be that the output only would be 5? Or am I thinking in a wrong direction? – Bronia Berlin May 17 '18 at 20:33
  • 2
    @Srini: *"Can one of you elaborate on when call-by-name would ever be preferable to call-by-value?"*. Imagine a function `int my_if_call_by_name(bool cond, int t, int f) {return cond ? t : f;}` with `my_if_call_by_name(cond, heavy_computation1, heavy_computation2)`. – Jarod42 May 17 '18 at 21:14
  • @SergeyA - what is meant by the terms 'call by name' and 'call by value' is exactly what Algol 60 meant; 'call by value' and 'call by name' were the two parameter-passing mechanisms defined in that ground-breaking language. –  Mar 18 '19 at 02:23

2 Answers2

1

You could do it like this:

#define f(n) (cout << "Here is f" << endl, n)

#define g(n) (n + 2 * n)

int main()
{
    int z = g(f(5));
    cout << "main: z = " << z << endl;
}

The "body" of f() is evaluated twice, because it is used twice in g().

Sid S
  • 6,037
  • 2
  • 18
  • 24
1

You can simulate call by name with std::function, and adapting the syntax:

int f() {
    std::cout << "Here is f" << std::endl;
    return 5;
}

int g(std::function<int()> a) {
    int x = a();
    int y = 2 * a();
    return x + y;
}
int main() {
    int z = g(f);
    cout << "main: z = " << z << endl;
}

With output:

Here is f
Here is f
main: z = 15

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302