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):
- Avoid repetition (DRY = Don’t Repeat Yourself)
- Make code easier to read (good names explain what happens)
- Make code easier to change (fix bug in one place → everywhere improves)
- Break big problems into small, testable pieces
- 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!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
def function_name(parameters_if_any): # colon : is MUST # indented block (4 spaces) # your code here # optional: return something # Calling / using the function function_name(arguments_if_any) |
- 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)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
def greet(): print("Namaste Hyderabad! Welcome to functions class! 👋") # Call it (run the function) greet() greet() # call again — same code runs twice greet() # third time |
Output:
|
0 1 2 3 4 5 6 7 8 |
Namaste Hyderabad! Welcome to functions class! 👋 Namaste Hyderabad! Welcome to functions class! 👋 Namaste Hyderabad! Welcome to functions class! 👋 |
→ Reused the same print without copy-paste!
Example 2: With input (parameters / arguments)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
def welcome(name): # name = parameter (placeholder) print("Hello boss,", name, "! How's life in Hyderabad? 😄") # Calling with different arguments welcome("Webliance") welcome("Rahul") welcome("Virat bhai") |
Output:
|
0 1 2 3 4 5 6 7 8 |
Hello boss, Webliance ! How's life in Hyderabad? 😄 Hello boss, Rahul ! How's life in Hyderabad? 😄 Hello boss, Virat bhai ! How's life in Hyderabad? 😄 |
Example 3: With multiple parameters + return value
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def add_two_numbers(a, b): # two parameters result = a + b return result # send answer back # Using it sum1 = add_two_numbers(15, 27) print("15 + 27 =", sum1) # 42 sum2 = add_two_numbers(100, 200) print("100 + 200 =", sum2) # 300 |
→ 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
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def calculate_bill(subtotal, gst_rate=0.18, tip_percent=10): gst = subtotal * gst_rate tip = subtotal * (tip_percent / 100) total = subtotal + gst + tip return total # Different calls print("Family dinner:", calculate_bill(2400)) # default tip 10% print("Quick lunch:", calculate_bill(450, tip_percent=5)) # custom tip print("Swiggy order:", calculate_bill(899, 0.05)) # food GST 5% |
→ One function → many situations. Change tax rate? Edit one place.
Example B: Temperature converter (Celsius ↔ Fahrenheit)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
def celsius_to_fahrenheit(c): f = (c * 9/5) + 32 return f hyderabad_today = 38 print("38°C =", celsius_to_fahrenheit(hyderabad_today), "°F") # 100.4 °F |
Example C: Check if number is even/odd (with return boolean)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def is_even(number): if number % 2 == 0: return True else: return False # or shorter: return number % 2 == 0 print(is_even(42)) # True print(is_even(77)) # False |
5. Important Concepts Around Functions
- Default parameters (optional values)
|
0 1 2 3 4 5 6 7 |
def order_biryani(type="chicken", spice="medium"): print(f"Ordering {spice} {type} biryani!") |
order_biryani() → medium chicken order_biryani(“mutton”, “hot”) → hot mutton
- Return multiple values (using tuple)
|
0 1 2 3 4 5 6 7 8 9 10 |
def min_max(numbers): return min(numbers), max(numbers) small, big = min_max([5, 42, 19, 88, 3]) print("Smallest:", small, "| Largest:", big) |
- 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)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
price = 500 # global def add_tax(): tax = 90 # local – only lives inside function total = price + tax return total print(add_tax()) # 590 # print(tax) # Error – tax doesn't exist here! |
6. Common Beginner Mistakes
- Forgetting return when you need output
- Calling function without () → greet (does nothing) vs greet() (runs)
- Indentation error inside function
- Using global variables too much → hard to debug
- 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:
- greet_user(name) → prints personalized welcome
- suggest_food(mood) → if “tired” → “Order biryani”, if “happy” → “Street pani puri”, else → “Chai time”
- 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! 🚀
