Chapter 9: Functions

1. What is a Function? (Napkin drawing version)

A function is a named, reusable block of code that does one specific job.

  • You write the instructions once
  • You give it a good name
  • Later, whenever you need that job done → just call the name (like calling your mom’s recipe card)
  • You can give it inputs (ingredients)
  • It can give you back output (ready biryani plate)

Why functions exist (the big reasons):

  1. Avoid repetition (DRY = Don’t Repeat Yourself)
  2. Make code easier to read (good names explain what happens)
  3. Make code easier to change (fix bug in one place → everywhere improves)
  4. Break big problems into small, testable pieces
  5. Let you build big programs from small Lego blocks

Real-life analogy:

Your favorite uncle has a special masala chai recipe. Instead of explaining the full steps every time someone wants chai, he just says: “Make one cup of uncle’s special chai for Webliance” → You follow the same fixed steps, but can adjust sugar/milk if needed.

That’s a function: named reusable instructions with optional customization.

2. Basic Syntax in Python (memorize this pattern!)

Python
  • def = define (keyword)
  • Name → like variable name (snake_case usually)
  • Parentheses () → always there (even empty)
  • Indentation → Python forces it (no braces {} like other languages)

3. First Baby Examples – Try These!

Example 1: No input, no output (simplest)

Python

Output:

text

→ Reused the same print without copy-paste!

Example 2: With input (parameters / arguments)

Python

Output:

text

Example 3: With multiple parameters + return value

Python

→ Functions can return a value (like giving back the cooked dish)

4. Very Useful Real-World Examples (Hyderabad flavor)

Example A: Bill calculator with GST & tip

Python

→ One function → many situations. Change tax rate? Edit one place.

Example B: Temperature converter (Celsius ↔ Fahrenheit)

Python

Example C: Check if number is even/odd (with return boolean)

Python

5. Important Concepts Around Functions

  • Default parameters (optional values)
Python

order_biryani() → medium chicken order_biryani(“mutton”, “hot”) → hot mutton

  • Return multiple values (using tuple)
Python
  • Scope (very important – where variables live)

Variables inside function = local (disappear after function ends) Variables outside = global (can be seen inside, but better avoid changing)

Python

6. Common Beginner Mistakes

  1. Forgetting return when you need output
  2. Calling function without () → greet (does nothing) vs greet() (runs)
  3. Indentation error inside function
  4. Using global variables too much → hard to debug
  5. Forgetting colon : after def line

7. Your Mini Project Challenge (10–20 min – do it!)

Write a small “Hyderabad Daily Helper” program with at least 3 functions:

  1. greet_user(name) → prints personalized welcome
  2. suggest_food(mood) → if “tired” → “Order biryani”, if “happy” → “Street pani puri”, else → “Chai time”
  3. calculate_phone_bill(data_gb, call_min) → base 199 + 10 per GB over 2 + 1 per min over 100

Call them in main code, maybe with input().

Paste your code here when done — I’ll review, suggest improvements, or fix bugs 😄

Any confusion right now?

  • “Explain return vs print”
  • “What are lambda functions?” (we can do later)
  • “Show function inside loop example”
  • “Next topic please” (lists + functions + loops = small real projects!)

Functions are where code stops being messy and starts feeling elegant. You’re doing amazing — keep going champ! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *