@@ -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
@@ -69,7 +69,8 @@ Let's use it to define a function that does nothing.
69
69
```
70
70
71
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 ` .
72
+ is assigned to a variable called ` do_nothing ` . You can ignore the
73
+ ` 0xblablabla ` stuff for now.
73
74
74
75
The ` pass ` is needed here because without it, Python doesn't know when
75
76
the function ends and it gives us a syntax error. We don't need the
@@ -181,15 +182,15 @@ integer because immutable values cannot be modified in-place.
181
182
Fortunately, Python will tell us if something's wrong.
182
183
183
184
``` python
184
- >> > foo = 1
185
- >> > def bar ():
186
- ... foo += 1
185
+ >> > thing = 1
186
+ >> > def stuff ():
187
+ ... thing += 1
187
188
...
188
- >> > bar ()
189
+ >> > stuff ()
189
190
Traceback (most recent call last):
190
191
File " <stdin>" , line 1 , in < module>
191
- File " <stdin>" , line 2 , in bar
192
- UnboundLocalError : local variable ' foo ' referenced before assignment
192
+ File " <stdin>" , line 2 , in stuff
193
+ UnboundLocalError : local variable ' thing ' referenced before assignment
193
194
>> >
194
195
```
195
196
@@ -288,8 +289,8 @@ def print_box(message, character):
288
289
print (character * len (message))
289
290
```
290
291
291
- Then we could change our existing code to always call `print_box` with
292
- 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:
293
294
294
295
```python
295
296
print_box(" Hello World" , " *" )
@@ -360,11 +361,11 @@ need to:
360
361
# # Output
361
362
362
363
The built- in input function [returns a value](using- functions.md# return-values).
363
- Can our function return a value also ?
364
+ Can our function return a value too ?
364
365
365
366
```python
366
- >> > def times_two(x ):
367
- ... return x * 2
367
+ >> > def times_two(thing ):
368
+ ... return thing * 2
368
369
...
369
370
>> > times_two(3 )
370
371
6
417
418
418
419
# # Return or print?
419
420
420
- 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
421
422
something or they can return something. So, should we print or return ?
422
423
423
424
Most of the time ** returning makes functions much easier to use** . Think
440
441
>> >
441
442
```
442
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
+
443
485
# # Examples
444
486
445
487
Ask yes/ no questions.
@@ -468,7 +510,7 @@ def ask_until_correct(prompt, correct_options,
468
510
while True :
469
511
answer = input (prompt + ' ' )
470
512
if answer in correct_options:
471
- return answer # returning ends the function
513
+ return answer
472
514
print (error_message)
473
515
474
516
@@ -494,6 +536,8 @@ print("Your favorite color is %s!" % choice)
494
536
function does. Returning also ends the function immediately.
495
537
- Return a value instead of printing it if you need to do something with
496
538
it after calling the function.
539
+ - Remember that `thing` , `thing()` , `print (thing)` and `print (thing())`
540
+ do different things.
497
541
498
542
# # Exercises
499
543
0 commit comments