We have identified in JavaScript some of the elements that must appear in any powerful programming language:

- Numbers and arithmetic operations are primitive data and functions.
- Nesting of combinations provides a means of combining operations.
- Constant declarations that associate names with values provide a limited means of abstraction.

Now we will learn about
*function declarations*,
a much more powerful abstraction technique by which a compound
operation can be given a name and then referred to as a unit.

We begin by examining how to express the idea of
squaring.

We might say,
To square something, take it times itself.

This is expressed in our language as

function square(x) { return x * x; }

function square( x ) { return x * x; } // ^ ^ ^ ^ ^ ^ ^ // To square something, take it times itself.

The simplest form of a function declaration
is
`function $name$( $parameters$ ) { return $expression$; }`
The
$\textit{name}$
is a symbol to be associated with the
function
definition in the environment.[2]
The
$\textit{parameters}$
are the names used within the body of the
function
to refer to the
corresponding arguments of the
function.
The $\textit{parameters}$
are grouped within
parentheses, as they will be in an application of the function
being declared.
In the simplest form, the
*body* of a function declaration is a single
*return*
statement, which consists of the keyword
`return`
followed by the return
$\textit{expression}$
that will yield the value of the function application, when the formal
parameters are replaced by the actual arguments to which the function
is applied.[3]

Having declared `square`,
we can now use it in a
function application expression, which we turn into a statement
using a semicolon:

square(21);

- To evaluate a function application, do the following:
- Evaluate the function expression of the application.
- Evaluate the argument expressions of the application.
- Apply the function that is the value of the function expression to the values of the argument expressions.

square(2 + 5);

square(square(3));

We can also use `square`
as a building block in defining other
functions.
For example, $x^2 +y^2$ can be expressed as

square(x) + square(y);

We can easily define a
function
`sum_of_squares`
that, given any two numbers as arguments, produces the
sum of their squares:

function sum_of_squares(x,y) { return square(x) + square(y); }

sum_of_squares(3, 4);

function f(a) { return sum_of_squares(a + 1, a * 2); }

f(5);

In addition to compound functions, JavaScript provides a number of
*primitive functions* that are built into the interpreter. An
example is the function `math_log`
that computes the natural logarithm of its argument.[4]
Primitive functions are used in exactly the same way as compound
functions; evaluating the application expression
`math_log(1)` results in the number 0.
Indeed, one could not tell by looking at the definition of
`sum_of_squares` given above whether
`square` was built into the
interpreter, like `math_log`, or
defined as a compound function.

[1]
Observe that there are two
different operations being combined here: we are creating the
function,
and we are giving
it the name `square`. It is possible, indeed
important, to be able to separate these two notions—to create
functions
without naming them, and to give names to
functions
that have already been created. We will see how to do this in
section 1.3.2.

[2]
Throughout this book, we will
describe the general syntax of expressions by using italic
symbols—e.g.,
$\textit{name}$—to
denote the

slotsin the expression to be filled in when such an expression is actually used.

[3]
More
generally, the body of the function can be a sequence of statements.
In this case, the interpreter evaluates each statement in the sequence
in turn until a return statement determines the value of the
function application.

[4]
This book
assumes that all functions and constants of JavaScript's
Math library
are available as `math_*`.

1.1.4 Compound Functions