← Python Basics

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
  1. What does double(21) print?42
  2. What does add() print without a return?None
  3. What does greet() print with no argument?Hi friend
  4. What does area() print with keyword arguments?10
  5. What does x print after calling f()?1
  6. What does this lambda print for 4?16
  7. What's wrong here: calling add?TypeError
  8. What's wrong here: the call on the last line?NameError
  9. Fill the blank so square(3) prints 9return
  10. Keyword that starts a function definitiondef
  11. 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