• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

• 精品课
• 公开课
• 欢迎下载我们在各应用市场备受好评的APP

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

#### 【用代数讲计算机科学】函数的设计方法

CS in Algebra: DesignRecipe

Up to this point, you focused on two elements of functions:

the contract which describes

what data types your function takes as its domain and returns as its range

and the definition which defines how your function interacts with the data passed to its domain to

produce its range.
“协议”能帮助我们确保函数拥有正确的结构
The contract helps us to ensure that our function the right structure but it doesn’t help us

understand what your function should do with

the values passed to it to return the expected value.
“示例”是“协议”和“定义”之间缺少的链接
Examples are the missing link between the contract and the definition.

By writing two or more examples of how your function should operate given specific inputs,

you give the computer something to test your function definition against

soft-developer often call this unit test or test cases

Writing examples serve two purposes.

Second, they give your function something to test against to make sure it works like you expected

All three of these processes taken together, the contract, examples, and the definition,

are called the design recipe and they give you a step-by-step approach to designing really great functions.

Let’s follow the design recipe for a simple function called green-triangle that should

take a number for size and return a solid green triangle with the given size.

First we’ll write a contract that describes the general structure of our function.

It should have a name of green-triangle, a single number as its domain and a range of image.

Now that we have a contract, let’s write some examples.

These will be concrete examples of how our function should operate with specific inputs.

If we pass the number 15 to green-triangle, it should return a 15 pixel solid green triangle.

If we pass the number 100 to green-triangle, it should return 100 pixel solid green triangle.

If we compare the two examples, we’ll see that the only thing that changes between them

is the size parameter passed to the triangle function and those directly correlate to the

input the function was passed.

With that knowledge, we can write a function definition that replaces that changing element with a variable.

Each example has a test button next to it.

Clicking test will run the code in your example and display its output in the play area on the left.

Running a test will also compare the value returned by your test against the value returned

by your function definition given the same values and tell you whether or not your example

In this stage, you’ll use the Design Recipe to write functions that will help animate a rocket blasting off.

【B】Cod