As I understand it, in Scala, a function may be called either
- by-value or
- by-name
For example, given the following declarations, do we know how the function will be called?
Declaration:
def f (x:Int, y:Int) = x;
Call
f (1,2)
f (23+55,5)
f (12+3, 44*11)
What are the rules please?
The example you have given only uses call-by-value, so I will give a new, simpler, example that shows the difference.
First, let’s assume we have a function with a side-effect. This function prints something out and then returns an
Int.Now we are going to define two function that accept
Intarguments that are exactly the same except that one takes the argument in a call-by-value style (x: Int) and the other in a call-by-name style (x: => Int).Now what happens when we call them with our side-effecting function?
So you can see that in the call-by-value version, the side-effect of the passed-in function call (
something()) only happened once. However, in the call-by-name version, the side-effect happened twice.This is because call-by-value functions compute the passed-in expression’s value before calling the function, thus the same value is accessed every time. Instead, call-by-name functions recompute the passed-in expression’s value every time it is accessed.