@@ -44,9 +44,9 @@ In this tutorial we'll learn to define a `print_box` function
44
44
that prints text in a box. We can write the code for printing the
45
45
box once, and then use it multiple times anywhere in the program.
46
46
47
- Dividing a long program into simple functions also makes the code
48
- easier to work with. If there's a problem with the code we can
49
- test the functions one by one and find the problem easily.
47
+ [ Dividing a long program into simple functions] ( larger-program.md ) also
48
+ makes the code easier to work with. If there's a problem with the code
49
+ we can test the functions one by one and find the problem easily.
50
50
51
51
## First functions
52
52
@@ -68,9 +68,15 @@ Let's use it to define a function that does nothing.
68
68
>> >
69
69
```
70
70
71
- Seems to be working so far, we have a function. Actually it's just
72
- a value that is assigned to a variable called ` do_nothing ` . Let's see
73
- what happens if we call it.
71
+ Seems to be working so far, we have a function. It's just a value that
72
+ is assigned to a variable called ` do_nothing ` . You can ignore the
73
+ ` 0xblablabla ` stuff for now.
74
+
75
+ The ` pass ` is needed here because without it, Python doesn't know when
76
+ the function ends and it gives us a syntax error. We don't need the
77
+ ` pass ` when our functions contain something else.
78
+
79
+ Let's see what happens if we call our function.
74
80
75
81
``` python
76
82
>> > do_nothing()
@@ -176,15 +182,15 @@ integer because immutable values cannot be modified in-place.
176
182
Fortunately, Python will tell us if something's wrong.
177
183
178
184
``` python
179
- >> > foo = 1
180
- >> > def bar ():
181
- ... foo += 1
185
+ >> > thing = 1
186
+ >> > def stuff ():
187
+ ... thing += 1
182
188
...
183
- >> > bar ()
189
+ >> > stuff ()
184
190
Traceback (most recent call last):
185
191
File " <stdin>" , line 1 , in < module>
186
- File " <stdin>" , line 2 , in bar
187
- UnboundLocalError : local variable ' foo ' referenced before assignment
192
+ File " <stdin>" , line 2 , in stuff
193
+ UnboundLocalError : local variable ' thing ' referenced before assignment
188
194
>> >
189
195
```
190
196
@@ -283,8 +289,8 @@ def print_box(message, character):
283
289
print (character * len (message))
284
290
```
285
291
286
- Then we could change our existing code to always call `print_box` with
287
- a star as the second argument:
292
+ Then we could change our code to always call `print_box` with a star as
293
+ the second argument:
288
294
289
295
```python
290
296
print_box(" Hello World" , " *" )
@@ -355,11 +361,11 @@ need to:
355
361
# # Output
356
362
357
363
The built- in input function [returns a value](using- functions.md# return-values).
358
- Can our function return a value also ?
364
+ Can our function return a value too ?
359
365
360
366
```python
361
- >> > def times_two(x ):
362
- ... return x * 2
367
+ >> > def times_two(thing ):
368
+ ... return thing * 2
363
369
...
364
370
>> > times_two(3 )
365
371
6
412
418
413
419
# # Return or print?
414
420
415
- There' s two ways to output information from functions. They can print
421
+ There are two ways to output information from functions. They can print
416
422
something or they can return something. So, should we print or return ?
417
423
418
424
Most of the time ** returning makes functions much easier to use** . Think
435
441
>> >
436
442
```
437
443
444
+ # # Common problems
445
+
446
+ Functions are easy to understand, but you need to pay attention to how
447
+ you' re calling them. Note that `some_function` and `some_function()` do
448
+ two completely different things.
449
+
450
+ ```python
451
+ >> > def say_hi():
452
+ ... print (" howdy hi" )
453
+ ...
454
+ >> > say_hi # just checking what it is, doesn't run anything
455
+ < function say_hi at 0x 7f997d2a8510>
456
+ >> > say_hi() # this runs it
457
+ howdy hi
458
+ >> >
459
+ ```
460
+
461
+ Typing `say_hi` just gives us the value of the `say_hi` variable, which
462
+ is the function we defined. But `say_hi()` ** calls** that function, so
463
+ it runs and gives us a return value. The return value is None so the
464
+ `>> > ` prompt [doesn' t show it](#variables.md#none).
465
+
466
+ But we know that the print function shows None , so what happens if we
467
+ wrap the whole thing in `print ()` ?
468
+
469
+ ```python
470
+ >> > print (say_hi) # prints the function, just like plain say_hi
471
+ < function say_hi at 0x 7fd913f58488>
472
+ >> > print (say_hi()) # runs the function and then prints the return value
473
+ howdy hi
474
+ None
475
+ >> >
476
+ ```
477
+
478
+ The `print (say_hi())` thing looks a bit weird at first, but it' s easy to
479
+ understand. There' s a print insnde `say_hi` and there' s also the print
480
+ we just wrote, so two things are printed. Python first ran `say_hi()` ,
481
+ and it returned None so Python did `print (None )` . Adding an extra
482
+ `print ()` around a function call is actually a common mistake, and I
483
+ have helped many people with this problem.
484
+
438
485
# # Examples
439
486
440
487
Ask yes/ no questions.
@@ -463,7 +510,7 @@ def ask_until_correct(prompt, correct_options,
463
510
while True :
464
511
answer = input (prompt + ' ' )
465
512
if answer in correct_options:
466
- return answer # returning ends the function
513
+ return answer
467
514
print (error_message)
468
515
469
516
@@ -489,6 +536,8 @@ print("Your favorite color is %s!" % choice)
489
536
function does. Returning also ends the function immediately.
<
10000
tr class="diff-line-row">490
537
- Return a value instead of printing it if you need to do something with
491
538
it after calling the function.
539
+ - Remember that `thing` , `thing()` , `print (thing)` and `print (thing())`
540
+ do different things.
492
541
493
542
# # Exercises
494
543
0 commit comments