# 1.1.4功能

> 译者： [https://sicp.comp.nus.edu.sg/chapters/6](https://sicp.comp.nus.edu.sg/chapters/6)

我们已经在JavaScript中确定了必须在任何强大的编程语言中出现的一些元素：

*   数字和算术运算是原始数据和函数。
*   组合嵌套提供了一种组合操作的方法。
*   将名称与值相关联的常量声明提供了有限的抽象方法。

 <split>现在，我们将学习_函数声明_，这是一种功能更强大的抽象技术，通过该技术可以为复合操作指定名称，然后将其称为单元。

我们首先研究如何表达&lt;quote&gt;平方的概念。&lt;/quote&gt; 我们可能会说，&lt;quote&gt;要对某事求平方，请将其乘以自身。&lt;/quote&gt; 用我们的语言表示为

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

We can understand this in the following way:

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

We have here a _compound function_, which has been given the name `square`. The function represents the operation of multiplying something by itself. The thing to be multiplied is given a local name, `x`, which plays the same role that a pronoun plays in natural language. Evaluating the declaration creates this compound function and associates it with the <nobr>name `square`.</nobr>[[1]](6#footnote-1)</split> <split>函数声明的最简单形式是`function ![%5Ctextit%7Bname%7D](img/abe58dbcc6883742e3e0d539cb238bc1.jpg)( ![%5Ctextit%7Bparameters%7D](img/d1b0d252cdb87f544051d91747e77542.jpg) ) { return ![%5Ctextit%7Bexpression%7D](img/f0cc862b727cfe66f52e91676be9e021.jpg); }` ![%5Ctextit%7Bname%7D](img/abe58dbcc6883742e3e0d539cb238bc1.jpg)是与环境中的函数关联的符号。 [ [2] ](6#footnote-2) ![%5Ctextit%7Bparameters%7D](img/d1b0d252cdb87f544051d91747e77542.jpg)是在函数主体中使用的名称，用于引用函数的相应参数。 关键字`return`之后的![%5Ctextit%7Bexpression%7D](img/f0cc862b727cfe66f52e91676be9e021.jpg)是_返回表达式_，当将参数替换为应用函数的参数时，该表达式将产生函数应用程序的值。 [ [3] ](6#footnote-3) ![%5Ctextit%7Bparameters%7D](img/d1b0d252cdb87f544051d91747e77542.jpg)分组在括号内，并用逗号分隔，就像它们在对所声明函数的实际调用中一样。

声明了`square`函数之后，我们现在可以在_函数应用程序表达式_中使用它，我们使用分号将其转换为语句：

```js
square(21);
```

The name `square` is the _function expression_ of the application, and `21` is the _argument expression_.

```js
square(2 + 5);
```

Here, the argument expression is itself a compound expression, the operator expression `2 + 5`.

```js
square(square(3));
```

Of course application expressions can also serve as argument expressions.</split> <split>在声明其他功能时，我们还可以使用`square`作为构建块。 例如，![x%5E2%20%2By%5E2](img/d4914298bf3c0fde073ab2a5652e6b4f.jpg)可以表示为

```js
square(x) + square(y);
```</split>  <split>我们可以轻松地声明一个函数`sum_of_squares`，给定任意两个数字作为参数，该函数将产生其平方和：

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

Now we can use `sum_of_squares` as a building block in constructing further functions:

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

The application of functions such as `sum_of_squares(3,4)` is—after operator combination—the second kind of combination of expressions into larger expressions that we encounter. 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]](6#footnote-4) Evaluating the application expression `math_log(1)` results in the number 0. Primitive functions are used in exactly the same way as compound functions. 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.</split>

* * *

[[1]](6#footnote-link-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 <ref name="sec:lambda">[1.3.2](20)</ref>.

[[2]](6#footnote-link-2) Throughout this book, we will describe the general syntax of programs by using italic symbols—e.g., ![%5Ctextit%7Bname%7D](img/abe58dbcc6883742e3e0d539cb238bc1.jpg)—to denote the <quote>slots</quote> in the expression to be filled in when such an expression is actually used.

[[3]](6#footnote-link-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]](6#footnote-link-4) The Source language used in this adaptation introduces names `math_*` for all functions and constants in JavaScript's [Math library](https://www.ecma-international.org/ecma-262/9.0/index.html#sec-math-object).

