Functions
As programs start to grow in complexity, the ability to encapsulate pieces of logic under a name that can be invoked on demand becomes invaluable. Functions let you do just that, and can receive inputs (parameters) and produce a result (return). What makes functions particularly useful is their reuse; i.e. you defined them once and reuse them on demand. Perhaps more than code reuse, it is the idea of decomposition — breaking a big problem into small, independently testable pieces — that make functions so useful.
You have already used built-in functions such as print(), input(), exit(), and others.
This module is about user-defined functions. At the end of this lesson, you will be able to:
- use the
defkeyword to define your own functions - write functions that accept inputs (arguments)
- write functions that produce values (return)
- explain when to use positional, keyword and default arguments
- write functions that call other functions (decomposition)
- write functions that call themselves (recursion)