2.10 Name Evaluation

The primitive elements presented so far, such as numbers and strings, evaluate to themselves, i.e., the value of an expression composed only by a primitive element is the primitive element itself. For names, this is no longer true.

Names have a special meaning in Julia. Note that when we define a function, we give it a name. Its formal parameters are names as well. When a combination is written, the evaluator uses the function definition associated with the name of the operator of the combination. This means that the value of the operator in a combination is the associated function. If we had defined the function square, as suggested in section Defining Functions, we could verify this behavior by testing the following expressions:

> square(3)

9

> square

square (generic function with 1 method)

As we can see, the value of the name square is an entity that Julia describes using a special notation. This entity is, as shown, a function. The same behavior happens with any other function:

> +

+ (generic function with 200 methods)

> *

* (generic function with 352 methods)

As we have seen, the sum + and multiplication * signs are some of the predefined names of the language. For example, the symbol pi is also predefined and associated with an approximated value of \(\pi\):

> pi

π = 3.1415926535897...

However, when the body of the expression is evaluated, the value of a name assigned to a parameter of the function is the corresponding argument during the function call. For example, in the combination square(3), after figuring out that the value of square is a function and that the value of 3 is \(3\), the evaluator then evaluates the body of the function, replacing the name x, whenever necessary, with the same \(3\) that was previously assigned to the function’s parameter x.