Call-by-name uses the actual argument as is in the function. Let’s see how CBN would evaluate squareSum(2, 4+1).
svg viewer
The advantage of CBN is that a function argument is not evaluated if the corresponding parameter is unused in the evaluation of the function body.
Conclusion
Simply put, CBV will evaluate every expression to its final value before calling the function, regardless of if the function body needs it or not. CBN, on the other hand, will only take the expressions required by the body of the function and pass them to the function just as you passed them. It then reduces the expressions to their final value in the function body.
For instance, we could have a function which takes two parameters, but only uses the first one in its function body.
def func(int x, int y) = {
print(x)
}
You then call that function passing it two expressions.
func(1+1, 1+2)
CBV will start evaluating by:
func(2,3)
CBN will start evaluating by:
print(1+1)
In the next lesson, let’s see if we can figure out which evaluation strategy is the faster option.