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.

Our simplest form of a function declaration is
`function $\textit{name}$( $\textit{parameters}$ ) { return $\textit{expression}$; }`
The $\textit{name}$
is a symbol to be associated with the function
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{expression}$
after the keyword `return`
is the *return expression* that will yield the value of
the function application when the parameters are replaced by
the arguments to which the function is
applied.[3]
The
$\textit{parameters}$
are grouped within
parentheses and separated by commas, just as they
would be in an actual call to the function being declared.

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

square(21);

square(2 + 5);

square(square(3));

We can easily declare 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); }

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

[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 programs 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] We shall see in the next section that
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] The Source language used in this adaptation introduces
names `math_*` for all functions and
constants in JavaScript's
Math library.

1.1.4
Functions