Chapter 6
Functions
A function packs a few lines under a name, so you can run them again with different inputs. This chapter covers def and return, default and keyword arguments, what a function gives back when it has no return, why a variable set inside a function doesn't change the one outside, short lambda functions, docstrings, and the errors you get from a missing argument or a misspelt name.
11 cards
See the 11 cards
- What does double(21) print?42
- What does add() print without a return?None
- What does greet() print with no argument?Hi friend
- What does area() print with keyword arguments?10
- What does x print after calling f()?1
- What does this lambda print for 4?16
- What's wrong here: calling add?TypeError
- What's wrong here: the call on the last line?NameError
- Fill the blank so square(3) prints 9return
- Keyword that starts a function definitiondef
- Text in triple quotes just under defDocstring
Image credits
- What does double(21) print?: Brain Zap, CC0 1.0 Source
- What does add() print without a return?: Brain Zap, CC0 1.0 Source
- What does greet() print with no argument?: Brain Zap, CC0 1.0 Source
- What does area() print with keyword arguments?: Brain Zap, CC0 1.0 Source
- What does x print after calling f()?: Brain Zap, CC0 1.0 Source
- What does this lambda print for 4?: Brain Zap, CC0 1.0 Source
- What's wrong here: calling add?: Brain Zap, CC0 1.0 Source
- What's wrong here: the call on the last line?: Brain Zap, CC0 1.0 Source
- Fill the blank so square(3) prints 9: Brain Zap, CC0 1.0 Source