第一周 教学大纲
Week oneHelp
-
Functions — Functions 函数
-
- Functions are reusable pieces of programs that take an input and produce an output.(定义)
- A function definition is a compound statement consisting of a header and a body.(组成)
- The header includes the keyword
def
, a sequence of parameters enclosed by parentheses, followed by a colon:
. def function_name(): body return # computes the area of a triangle def triangle_area(base, height): # header - ends in colon area = (1.0 / 2) * base * height # body - all of body is indented return area # body - return outputs value
- The body consists of a sequence of statements, all indented by 4 spaces.(缩进一般为4,注意同一逻辑层的缩进一致)
- Functions may return a value using the keyword
return
or have a side effect (e.g.,print
). - To evaluate a function call, replace the function's parameters in the body of the function by their associated values in the call and execute the body of the function.
- Lecture examples - Functions
- More examples - Stucture of Functions, Uses of Functions, Scope of Variables, Examples of Functions
Indentation — Functions(缩进)
-
- Indentation consists of whitespace formed by blanks, tabs, and newlines.
- Leading white space indicates indentation level (4 spaces per