@@ -4,18 +4,24 @@ User-defined functions
44Introduction
55============
66
7- There is still a lot of duplicated code --- the actual drawing of the rectangle
8- --- around. If you need to copy and paste code, that is usually a sign of
9- lacking abstractions. (Programmers call it a *code smell. *)
7+ Programmers can deal with some pretty complex and abstract problems, but one
8+ sign of a good programmer is that they're lazy. They only like to deal with one
9+ thing at a time. So you need a way to break up code into smaller, discrete
10+ pieces, which lets you focus on just the piece you want to.
1011
11- Functions are one way to express abstractions in Python. Let's take
12- ``turtle.reset() `` for example. It is actually an abstraction for a number of
13- steps, namely:
12+ Functions are one way to express such abstractions in Python. Let's take
13+ ``turtle.reset() `` for example. `` reset `` is a function we call on our `` turtle ``, and
14+ it is actually an abstraction for a number of steps, namely:
1415
1516* Erase the drawing board.
1617* Set the width and color back to default.
1718* Move the turtle back to its initial position.
1819
20+ But because all the code is contained in the function, we don't have to worry about these
21+ details. We can simply *call * this function, and know it will do what it says for us.
22+
23+ So - how to write your own?
24+
1925A function can be defined with the ``def `` keyword in Python::
2026
2127 def line_without_moving():
@@ -43,11 +49,7 @@ We could write more functions to remove some of the repetition::
4349 line_without_moving()
4450 turtle.right(360 / 5)
4551
46- def star():
47- star_arm()
48- star_arm()
49- star_arm()
50- star_arm()
52+ for _ in range(5):
5153 star_arm()
5254
5355.. important ::
@@ -87,14 +89,9 @@ Solution
8789
8890 def tilted_square():
8991 turtle.left(20) # now we can change the angle only here
90- turtle.forward(50)
91- turtle.left(90)
92- turtle.forward(50)
93- turtle.left(90)
94- turtle.forward(50)
95- turtle.left(90)
96- turtle.forward(50)
97- turtle.left(90)
92+ for _ in range(4):
93+ turtle.forward(50)
94+ turtle.left(90)
9895
9996 tilted_square()
10097 tilted_square()
@@ -104,35 +101,15 @@ Solution
104101 # which might be useful later:
105102
106103 def square():
107- turtle.forward(50)
108- turtle.left(90)
109- turtle.forward(50)
110- turtle.left(90)
111- turtle.forward(50)
112- turtle.left(90)
113- turtle.forward(50)
114- turtle.left(90)
104+ for _ in range(4):
105+ turtle.forward(50)
106+ turtle.left(90)
115107
116108 def tilted_square():
117109 turtle.left(20)
118110 square()
119111
120- # etc'
121-
122- Comments
123- --------
124-
125- In the solution above, the line that starts with a ``# `` is called a
126- comment. In Python, anything that goes on a line after ``# `` is ignored
127- by the computer. Use comments to explain what your program does,
128- without changing the behaviour for the computer.
129-
130- Comments can also go at the end of a line, like this:
131-
132- ::
133-
134- turtle.left(20) # now we can change the angle only here
135-
112+ # etc
136113
137114A function for a hexagon
138115========================
@@ -148,48 +125,28 @@ Now combine that function into a honeycomb. Just make it with a single layer lik
148125
149126.. image :: /images/honeycomb.png
150127
128+ Give it a good go!
129+
130+ .. hint ::
131+
132+ Make sure your hexagon function returns your turtle to exactly the same
133+ position and angle it was before it was asked to draw the hexagon. This
134+ makes it easier to reason about.
135+
151136Solution
152137--------
153138
154139::
155140
156141 def hexagon():
157- turtle.forward(100)
158- turtle.left(60)
159- turtle.forward(100)
160- turtle.left(60)
161- turtle.forward(100)
162- turtle.left(60)
163- turtle.forward(100)
164- turtle.left(60)
165- turtle.forward(100)
166- turtle.left(60)
167- turtle.forward(100)
168- turtle.left(60)
169-
170- hexagon()
171- turtle.forward(100)
172- turtle.right(60)
173-
174- hexagon()
175- turtle.forward(100)
176- turtle.right(60)
177-
178- hexagon()
179- turtle.forward(100)
180- turtle.right(60)
142+ for _ in range(6):
143+ turtle.forward(100)
144+ turtle.left(60)
181145
182- hexagon()
183- turtle.forward(100)
184- turtle.right(60)
185-
186- hexagon()
187- turtle.forward(100)
188- turtle.right(60)
189-
190- hexagon()
191- turtle.forward(100)
192- turtle.right(60)
146+ for _ in range (6):
147+ hexagon()
148+ turtle.forward(100)
149+ turtle.right(60)
193150
194151You could also put the ``turtle.forward(100); turtle.right(60) `` portion in the
195152function, but you better not call it ``hexagon `` in that case. That's
0 commit comments