🟢 Day 4 — Functions (Thinking in Reusable Blocks)
1️⃣ Why Do We Need Functions?
Imagine this situation:
You wrote code to:
Check if a number is even And you need it in 5 different places Without Functions (Bad)
if n % 2 == 0:
print("Even")
Repeated again and again.
Problems:
Changes need to be made everywhere 💡 If logic is repeated → it deserves a function
2️⃣ Real-World Analogy
Think of a vending machine.
Input → money + selection Processing → internal logic You don’t care how it works inside.
🧠 A function hides complexity and exposes behavior.
3️⃣ What Is a Function?
A function is:
def add(a, b):
return a + b
4️⃣ How Computer Thinks About Functions
When Python sees:
It:
Jumps to function definition Comes back to where it was called 🧠 Execution jumps, then returns.
5️⃣ Parameters vs Arguments (Important Clarity)
def multiply(x, y): # parameters
return x * y
multiply(3, 5) # arguments
Parameters → variables in function definition Arguments → actual values passed 6️⃣ return vs print (CRITICAL CONCEPT)
❌ Bad Practice
def add(a, b):
print(a + b)
Why bad?
✅ Correct Practice
def add(a, b):
return a + b
🧠 Functions should return values, not print.
7️⃣ Using Functions Inside Functions
def is_even(n):
return n % 2 == 0
def count_even(numbers):
count = 0
for n in numbers:
if is_even(n):
count += 1
return count
💡 This is composition — building bigger logic from smaller ones.
8️⃣ Default Parameters
def greet(name="Guest"):
print("Hello", name)
Usage:
Default values make functions flexible.
9️⃣ Breaking Problems into Functions (Key Skill)
Example Problem
Given a list of numbers, count even numbers.
Bad Thinking
Write everything in one block.
Good Thinking
🧠 Functions = units of thinking
⚠️ Common Beginner Mistakes
Confusing parameters and arguments Writing very large functions 🧪 Practice Questions (Think First)
Q1
Write a function that takes two numbers and returns the larger one.
Q2
Write a function that returns whether a number is positive, negative, or zero.
Q3
Write a function that takes a list and returns the sum of elements.
Q4
Write a function that counts vowels in a string.
Q5
Write a function that checks if a number is prime.
Q6
Write a function that reverses a string.
Q7 (Thinking Question)
Why is returning values better than printing them inside functions?
Answer in words.
🔑 HINTS (Read Together, Only If Stuck)
For max: assume first element Prime: check divisibility from 2 to n-1 String reverse: build new string or loop backwards ✅ What You Should Be Able to Do Now
After Day 4, you should:
Break problems into smaller parts Use return values properly Combine functions to solve bigger problems 🧠 Reflection Questions (Answer in Words)
Why do functions improve code quality? What happens when a function is called? Difference between return and print? Why should functions be small? ⭐ Golden Rule from Day 4
Good programmers don’t write more code.
They write better-structured code.
If you say “Continue with Day 5”, next we’ll cover:
First step toward databases Ready when you are.