Search code examples
functionfunctional-programmingprogramming-languagesparameter-passingevaluation

Are the different meanings of "call-by-value" related?


Currently I am taking a course on concepts of programming languages, which is where I came across an issue that had left me confused once before (when I was watching the videos of Martin Odersky's functional programming course on Coursera):

The term "call-by-value" is constantly being used in two different contexts:

Context 1: Evaluation regime (Call-by-value vs. call-by-name)

If a function is being passed an expression as an argument, is that expression being reduced to a value (i.e., evaluated) before the parameter identifiers in the function's body are substituted with it? If so, it's called call-by-value, if not, it's call-by-name. At least that's how I understand it.

Context 2: Parameter passing (Call-by-value vs. call-by-reference)

If a function is being passed an identifier as an argument, is the function's body being evaluated with a new name binding that points to a copy of what the given identifier points at, or can the function actually make changes to what the identifier points at, i.e., changes that are "visible" to the calling context? Again, the first variant would be call-by-value, whereas the second one would be call-by-reference. If I got it right, of course.

However, even though I fail to see a relation between the two use cases of the term "call-by-value", it seems too much of a coincidence for the term to be used in both scenarios by accident.

Does someone know why the same term was chosen to describle two (seemingly different) matters?


Solution

  • Your analysis is correct, and I also find the overloading of the phrase "call-by-value" to be confusing.

    Adopting language from the relevant Wikipedia article, call-by-value and call-by-reference can be considered two variants of "strict evaluation", while call-by-name is a type of "non-strict evaluation".

    To answer your question, I have a conjecture that the sloppy use of the phrase "call-by-value" to refer to "strict evaluation" in general is born out of the fact that call-by-value is the type of strict evaluation implemented by some of the most popular imperative programming languages. Some old lecture notes from a PL course at UMD conflate the two contexts you describe. They refer to protection of arguments against modification as "another feature" of call-by-value in C, C++, and Java (slide 21), where strict evaluation is implied (erroneously IMHO) to be the definitive feature of call-by-value.