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?
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:Now let’s call it with some arguments:
xandyare going to be substituted for the parameters, so the call tofooabove is going to result in: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
yin the function body, eventually returning 457.It’s contrived, but I hope you get the idea. It’s all about substitution.