@@ -44,9 +44,9 @@ In this tutorial we'll learn to define a `print_box` function
4444that prints text in a box. We can write the code for printing the
4545box once, and then use it multiple times anywhere in the program.
4646
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.
5050
5151## First functions
5252
@@ -68,9 +68,15 @@ Let's use it to define a function that does nothing.
6868>> >
6969```
7070
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.
7480
7581``` python
7682>> > do_nothing()
@@ -176,15 +182,15 @@ integer because immutable values cannot be modified in-place.
176182Fortunately, Python will tell us if something's wrong.
177183
178184``` python
179- >> > foo = 1
180- >> > def bar ():
181- ... foo += 1
185+ >> > thing = 1
186+ >> > def stuff ():
187+ ... thing += 1
182188...
183- >> > bar ()
189+ >> > stuff ()
184190Traceback (most recent call last):
185191 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
188194>> >
189195```
190196
@@ -283,8 +289,8 @@ def print_box(message, character):
283289 print (character * len (message))
284290```
285291
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:
288294
289295```python
290296print_box(" Hello World" , " *" )
@@ -355,11 +361,11 @@ need to:
355361# # Output
356362
357363The 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 ?
359365
360366```python
361- >> > def times_two(x ):
362- ... return x * 2
367+ >> > def times_two(thing ):
368+ ... return thing * 2
363369...
364370>> > times_two(3 )
3653716
412418
413419# # Return or print?
414420
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
416422something or they can return something. So, should we print or return ?
417423
418424Most of the time ** returning makes functions much easier to use** . Think
435441>> >
436442```
437443
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+
438485# # Examples
439486
440487Ask yes/ no questions.
@@ -463,7 +510,7 @@ def ask_until_correct(prompt, correct_options,
463510 while True :
464511 answer = input (prompt + ' ' )
465512 if answer in correct_options:
466- return answer # returning ends the function
513+ return answer
467514 print (error_message)
468515
469516
@@ -489,6 +536,8 @@ print("Your favorite color is %s!" % choice)
489536 function does. Returning also ends the function immediately.
490537- Return a value instead of printing it if you need to do something with
491538 it after calling the function.
539+ - Remember that `thing` , `thing()` , `print (thing)` and `print (thing())`
540+ do different things.
492541
493542# # Exercises
494543
0 commit comments