Skip to content

Day 4 — Functions

🟢 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:
Hard to maintain
Easy to make mistakes
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
Output → item
You don’t care how it works inside.
🧠 A function hides complexity and exposes behavior.

3️⃣ What Is a Function?

A function is:
A named block of code
That takes input
Does some work
Returns output
def add(a, b):
return a + b

4️⃣ How Computer Thinks About Functions

When Python sees:
add(3, 4)
It:
Jumps to function definition
Assigns a = 3, b = 4
Runs function body
Returns value
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?
Cannot reuse result
Cannot test easily

✅ 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:
greet()
greet("Amit")
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

Function to check even
Function to count
🧠 Functions = units of thinking

⚠️ Common Beginner Mistakes

Forgetting return
Printing inside function
Using global variables
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
For sum: start with 0
Vowels: a, e, i, o, u
Prime: check divisibility from 2 to n-1
String reverse: build new string or loop backwards
Avoid global variables

✅ What You Should Be Able to Do Now

After Day 4, you should:
Write reusable code
Break problems into smaller parts
Understand function flow
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

 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.