Call by value is a way to pass arguments to subroutines in which the value itself is copied. As a consequence, any modification to the value within the subroutine will not be preserved after the subroutine terminates; this is cleaner in the sense that it leads to a slightly more declarative style of programming.

Call by reference, however, is less work if values can be large. Call by value return is just fun. And call by name is amazing.

In the classifcation of functional languages, this refers to the strategy for evaluating expressions where all of the arguments passed to a function are evaluated before the body of a function is evaluated. This is also known as eager evaluation or strict evaluation. For example, this fragment of Caml code:

let one x = 1
in
let rec fact n =
  match n with
     0 -> 1
   | n*(fact (n-1))
in
one (fact ~-1);;

will attempt to evaluate (fact ~-1) (the factorial of -1, which is the argument to the function one) before attempting to evaluate the body of 'one'. This example also illustrates a problem with these semantics; this code will probably crash the Objective Caml interpreter because it represents infinite recursion. The argument to one, (fact -1), need never be evaluated, because it is not actually used in the evaluation of one. For this reason, call by value is usually not used in "pure" functional languages such as Haskell or Miranda, which instead use lazy evaluation. The equivalent of this code in those languages would evaluate simply to 1. These semantics also break referential transparency for that reason; for example the function one above for example does not always evaluate to 1 in every context; passed an infinitely recursive function it fails to terminate.

However, use of call by value semantics in functional languages produce languages that are much faster and use less memory than functional languages that use other evaluation methods, and they are also the only way of producing quasi-functional languages, as the presence of imperative language features requires this. Strict and well-defined evaluation order of functions ensures that side effects that come from imperative features arise in the order they are expected; call by name semantics or lazy evaluation might result in a side effect taking place at some random time in the evaluation of an expression, occuring more times than expected, or in fact not even taking place at all.

Most mainstream functional languages, such as Common Lisp, Scheme, SML, and Caml, all use call by value semantics in evaluating their expressions.

Log in or register to write something here or to contact authors.