The Java construct for implementing functions is known as the static method.
Using and defining static methods.
The use of static methods is easy to understand. For example, when you write Math.abs(a-b) in a program, the effect is as if you were to replace that code with the return value that is produced by Java's Math.abs() method when passed the expression a-b as an argument.
Flow-of-control. Harmonic.java comprises two static methods: harmonic() to compute harmonic numbers and and main() to interact with user.


Function-call trace. One simple approach to following the flow of control through function calls is to imagine that each function prints its name and argument value when it is called and its return value just before returning, with indentation(锯齿状) added on calls and subtracted on returns.
Static method definition. The first line of a static method definition, known as the signature, gives a name to the method and to each parameter variable. It also specifies the type of each parameter variable and the return type of the method. Following the signature is the body of the method, enclosed in curly braces. The body consists of the kinds of statements we discussed in Chapter 1. It also can contain a return statement, which transfers control back to the point where the static method was called and returns the result of the computation or return value. The body may declare local variables, which are variables that are available only inside the method in which they are declared.

Function calls. A static method call is nothing more than the method name followed by its arguments, separated by commas and enclosed in parentheses(圆括号). A method call is an expression, so you can use it to build up more complicated expressions. Similarly, an argument is an expression—Java evaluates the expression and passes the resulting value to the method. So, you can write code like Math.exp(-x*x/2) / Math.sqrt(2*Math.PI) and Java knows what you mean.

Multiple arguments. Like a mathematical function, a Java static method can take on more than one argument, and therefore can have more than one parameter variable.
Multiple methods. You can define as many static methods as you want in a .java file. These methods are independent and can appear in any order in the file. A static method can call any other static method in the same file or any static method in a Java library such as Math.
Overloading. Static methods with different signatures are different methods. whose signatures differ are different static methods. Using the same name for two static methods whose signatures differ is known as overloading.(重载)
Multiple return statements. You can put return statements in a method wherever you need them: control goes back to the calling program as soon as the first return statement is reached.
Single return value. A Java method provides only one return value to the caller, of the type declared in the method signature.
Scope. The scope of a variable is the part of the program that can refer to that variable by name. The general rule in Java is that the scope of the variables declared in a block of statements is limited to the statements in that block. In particular, the scope of a variable declared in a static method is limited to that method's body. Therefore, you cannot refer to a variable in one static method that is declared in another.
Side effects. A pure function is a function that, given the same arguments, always returns the same value, without producing any observable side effects, such as consuming input, producing output, or otherwise changing the state of the system. The function harmonic() is an example of a pure function. However, in computer programming, we often define functions whose only purpose is to produce side effects. In Java, a static method may use the keyword void as its return type, to indicate that it has no return value. (例如drawTriangle函数)
Closed form. In the simplest situation, we have a closed-form mathematical equation defining our function in terms of functions that are implemented in the Math library. This is the case for .
No closed form. Otherwise, we may need a more complicated algorithm to compute function values. This situation is the case for , for which no closed-form expression exists. For small (respectively large) z, the value is extremely close to 0 (respectively 1); so the code directly returns 0 (respectively 1); otherwise the following Taylor series approximation is an effective basis for evaluating the function:
Properties of static methods.

FunctionExamples.java gives a number of examples.
Implementing mathematical functions.
We now consider two important functions that play an important role in science, engineering, and finance. The Gaussian (normal) distribution function (高斯正态分布)is characterized by the familiar bell-shaped curve and defined by the formula:

and the cumulative Gaussian distribution function Φ(z) is defined to be the area under the curve defined by ϕ(x) above the x-axis and to the left of the vertical line x = z.


Given n, compute a random coupon value.
Given n, do the coupon collection experiment.
Get n from the command line, and then compute and print the result.
Pass by value. You can use parameter variables anywhere in the code in the body of the function in the same way you use local variables. The only difference between a parameter variable and a local variable is that Java evaluates the argument provided by the calling code and initializes the parameter variable with the resulting value. This approach is known as pass by value.(传值)
Arrays as arguments. When a static method takes an array as an argument, it implements a function that operates on an arbitrary number of values of the same type. (数组作为参数)
Side effects with arrays. It is often the case that the purpose of a static method that takes an array as argument is to produce a side effect (change values of array elements).
Arrays as return values. A static method can also provide an array as a return value.
Gaussian.java implements both of these static methods.
Using static methods to organize code.
With the ability to define functions, we can better organize our programs by defining functions within them when appropriate. For example, Coupon.java is a version of CouponCollector.java that better separates the individual components of the computation. (代码模块化)
Whenever you can clearly separate tasks in programs, you should do so.
Passing arguments and returning values.
Next, we examine the specifics of Java's mechanisms for passing arguments to and returning values from functions.
ArrayFunctionExamples.java gives a number of examples of arrays as arguments to and return values from functions.
Superposition of sound waves.
Notes like concert A have a pure sound that is not very musical, because the sounds that you are accustomed(通常的习惯的) to hearing have many other components. Most musical instruments produce harmonics (the same note in different octaves and not as loud), or you might play chords (multiple notes at the same time). To combine multiple sounds, we use superposition: simply add the waves together and rescale to make sure that all values stay between −1 and 1.

PlayThatTuneDeluxe.java is a version of PlayThatTune that encapsulates the sound wave calculation and adds harmonics. (演示并看懂代码)

Here a few sample data files (created by various students):

