As your programs grow, you’ll notice the same kind of code appearing again and again. Functions let you:
- Group related code under a name.
- Avoid repetition (write it once, use it many times).
- Organize your program into small, understandable pieces.
- Give names to operations so your code reads more like English.
You’ve already used built-in functions like print() and input(). In this chapter, you’ll learn how to create your own.
A function is like a small machine:
- You call it by name.
- You give it inputs (optional).
- It does something.
- It may give you back a result.
This idea is used everywhere in Python, from tiny scripts to huge applications.