Skip to content

Commit bd21f17

Browse files
committed
explaining function stuff
1 parent 89b17d2 commit bd21f17

File tree

1 file changed

+61
-17
lines changed

1 file changed

+61
-17
lines changed

basics/defining-functions.md

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ In this tutorial we'll learn to define a `print_box` function
4444
that prints text in a box. We can write the code for printing the
4545
box 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

@@ -69,7 +69,8 @@ Let's use it to define a function that does nothing.
6969
```
7070

7171
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.
7374

7475
The `pass` is needed here because without it, Python doesn't know when
7576
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.
181182
Fortunately, Python will tell us if something's wrong.
182183

183184
```python
184-
>>> foo = 1
185-
>>> def bar():
186-
... foo += 1
185+
>>> thing = 1
186+
>>> def stuff():
187+
... thing += 1
187188
...
188-
>>> bar()
189+
>>> stuff()
189190
Traceback (most recent call last):
190191
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
193194
>>>
194195
```
195196

@@ -288,8 +289,8 @@ def print_box(message, character):
288289
print(character * len(message))
289290
```
290291

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:
293294

294295
```python
295296
print_box("Hello World", "*")
@@ -360,11 +361,11 @@ need to:
360361
## Output
361362

362363
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?
364365

365366
```python
366-
>>> def times_two(x):
367-
... return x * 2
367+
>>> def times_two(thing):
368+
... return thing * 2
368369
...
369370
>>> times_two(3)
370371
6
@@ -417,7 +418,7 @@ None
417418

418419
## Return or print?
419420

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
421422
something or they can return something. So, should we print or return?
422423

423424
Most of the time **returning makes functions much easier to use**. Think
@@ -440,6 +441,47 @@ hi
440441
>>>
441442
```
442443

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 0x7f997d2a8510>
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 0x7fd913f58488>
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+
443485
## Examples
444486

445487
Ask yes/no questions.
@@ -468,7 +510,7 @@ def ask_until_correct(prompt, correct_options,
468510
while True:
469511
answer = input(prompt + ' ')
470512
if answer in correct_options:
471-
return answer # returning ends the function
513+
return answer
472514
print(error_message)
473515

474516

@@ -494,6 +536,8 @@ print("Your favorite color is %s!" % choice)
494536
function does. Returning also ends the function immediately.
495537
- Return a value instead of printing it if you need to do something with
496538
it after calling the function.
539+
- Remember that `thing`, `thing()`, `print(thing)` and `print(thing())`
540+
do different things.
497541

498542
## Exercises
499543

0 commit comments

Comments
 (0)