• #### 科普

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: Functions

Imagine if every time you wanted someone to make a sandwich, but they had no understanding

of what make a sandwich means, you’d have to describe every last step of sandwich-making.

From opening the fridge to slicing the bread, to layering the ingredients to spreading the

condiments.

If you ever needed someone to make a sandwich more than once, you’d want to agree on a common

shorthand for make a sandwich.

And you’d probably want make a sandwich to be generic enough to allow someone to make

a ham and swiss or peanut butter and jelly or tuna and sprouts.

Make a sandwich becomes a new function that can take in certain inputs such as filling,

condiments and bread and performs a set of operations on them.

Spreading the condiments on bread, layering the filling between the two slices and returns

an object of type: sandwich.

Remember our fifty red triangles from the last stage?

What if we didn’t want fifty of the exact same shape but fifty red polygons, each one

with one more side than the last.

First triangle, then square, a pentagon, a hexagon and so on.

We can do just that by defining a new function that doesn’t return the same shape every time.

Let’s call our new function My Polygon.

It should take a single number for sides and return a red polygon of given sides.

We can call this function with every number from three to fifty-three to fill up our screen

with red polygons.

At the beginning of this course we talked about math as a kind of language.

When we define variables, we are essentially adding new things or nouns to our language.

When we define functions, we expand our language with new actions or verbs.

In this next stage, you’re going to edit and write simple functions to output a variety

of images.

To get inside a function, you click the word edit in the corner of the function’s block.

From there, you can complete the contract for your new function, including a name, domain

and range.

There’s also a section for you to write a description of what your function should do.

Underneath the function contract is an area for you to build your function definition.

Make sure to use the parameter blocks that were generated when you filled out the domain.

When you call your function, the values that you passed as inputs end up in these placeholders.

【B】Cod

【MR】拾月