Skip to content
Zeni Study
  • Pages
    • Learn Python In 7 Days
      • Day 1 : Variable + if/else
      • Day 2: Loops
      • Day 3 : Array + String
      • icon picker
        Day 4 — Functions
      • Day 5 — Dictionaries - Hashmap
      • Day 6 — Files & Errors
      • Day 7 — Mini Project & Programmer Thinking
    • Machine Coding:
      • Day 1 — Bank account + Wallet :
      • Day 2 : RateLimiter + seat booking
      • Day 3 : Library Management + Classroom Attendance System
      • Day 4 : Bank account and notification system.
      • Day 5 : Key-value and payment processor.
      • Day 6 : Discount engine + Seat booking( in depth)
      • Day 7: Parking lot System :
    • Machine coding extended:
      • Day 7.5 — Parking Lot (Constraint-Heavy Variant)
      • Day 8 : Order + Payment System
      • Day 8 — Extension Round (Mid-Interview Change)
      • Day 9 — Ticket Booking with Expiry (Simulated Time)
      • Day 9 — Extension Round (Seat Lock Ownership + Forced Unlock)
    • SQL + DB Thinking course
      • Day 1 — Tables, Rows & Thinking in Data
      • Day 2 — Relationships & Foreign Keys (Thinking in Connections)
      • Day 3 — Filtering, Aggregation & Answering Data Questions
      • Day 4 — JOINs (Combining Tables the Right Way)
      • Day 5 — Subqueries, EXISTS & NOT EXISTS (Thinking in Layers)
      • Day 6 — Indexes, Constraints & How Databases Think
      • Day 7 — Full SQL Interview Simulation (SDE-1 Ready)
      • Extended Day 1 — Core Business Systems (Deep Practice)
      • Extended Day 2 — Booking & Platform Systems (High-Depth Practice)
      • Extended Day 3 — Content & Learning Platforms (Capstone Practice)
    • LLD: Java
      • DAY 1 — Core Foundations (SDE-2 Level)
      • DAY 2 — State, Time & Domain Correctness
      • DAY 3 — Extensibility & Failure Handling (SDE-2 Core Signal)
      • DAY 4 — Concurrency, Scheduling & Consistency (SDE-2 Critical)
      • DAY 5 — Workflow Design & Config-Driven Systems (SDE-2+)
      • DAY 6 — Eventing & Usage-Based Systems (Advanced SDE-2)
      • DAY 7 — Interview Simulation (Hard, Production-Grade)
      • Hidden Edge Cases Interviewers Expect (Day-wise)
      • Detailed Testing file:
    • Assignment:
      • If else Practice Set

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

Good programmers don’t write more code. They write better-structured code.
If you say “Continue with Day 5”, next we’ll cover:
Dictionaries
Key-value thinking
Real-world data modeling
First step toward databases
Ready when you are.
 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.