Functions and Variables

📑 Navigation

  1. What is a Function?
  2. Defining Functions
  3. Parameters
  4. Return Statement
  5. Type Hints for Functions
  6. Arguments: Positional vs Keyword
  7. Variable Scope
  8. Best Practices
  9. Key Takeaways

What is a Function?

Why Do We Need Functions?

Previously, we learned two control structures:

But sometimes these aren't enough. Think about a coffee machine analogy:

With loops: You'd have to write out every single step of making coffee each time you need one (brew, mix, pour, deliver). After one day, your script would be extremely long!

With functions: You just press a button (like "Latte" or "Espresso"), and the machine does all the steps for you.