👋 Hi there, coding explorer!
Welcome back! This week, we’re diving into Loops and Functions — from beginner concepts to expert-level techniques perfect for coding interviews and real projects. We’ll break everything into small, easy-to-digest bites, so you can practice and master them step by step.
Part 1: Functions
1️⃣ What is a Function?
A function is a reusable block of code that performs a specific task. Think of it as a recipe: you provide ingredients (inputs), it performs steps (code), and gives a finished dish (output).
2️⃣ Function Terminology
Function Name
Identifies the function so you can call it later.
def greet(): # "greet" is the function name
print("Hello!")
function greet() {
console.log("Hello!");
}
Parameters
Placeholders for inputs the function expects.
def greet(name): # "name" is a parameter
print(f"Hello, {name}!")
function greet(name) {
console.log(`Hello, ${name}!`);
}
Arguments
Actual values passed to the function.
greet("Fahim") # "Fahim" is the argument
greet("Fahim");
Return Value
The output a function gives back.
def add(a, b):
return a + b
print(add(5,3)) # 8
function add(a,b){ return a+b; }
console.log(add(5,3)); // 8
Function Keyword
Python:
def
JavaScript:
function
or arrow functions
const greet = (name) => `Hello, ${name}!`;
Default Parameters
def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # Hello, Guest
greet("Fahim") # Hello, Fahim
function greet(name="Guest"){ console.log(`Hello, ${name}!`); }
greet(); greet("Fahim");
Anonymous Functions
Python:
lambda
JavaScript: arrow functions
square = lambda x: x*x
print(square(5)) # 25
const square = x => x*x;
console.log(square(5)); // 25
Higher-Order Functions
Functions that take other functions or return functions
def apply_func(func, value):
return func(value)
print(apply_func(lambda x:x*2, 5)) # 10
function applyFunc(func, value){ return func(value); }
console.log(applyFunc(x=>x*2,5)); // 10
Recursion
Function calling itself (common in interviews)
def factorial(n):
if n==0: return 1
return n*factorial(n-1)
print(factorial(5)) # 120
function factorial(n){
if(n===0) return 1;
return n*factorial(n-1);
}
console.log(factorial(5)); // 120
Concepts:
Closures (functions “remembering” scope)
Decorators / Wrappers (modifying function behavior)
Generators / Iterators (lazy sequences)
Memoization / DP prep (optimizing recursion)
3️⃣ Built-in Functions
Python: len()
, sum()
, sorted()
, zip()
, map()
, filter()
, reduce()
JavaScript: parseInt()
, Math.max()
, Array.isArray()
, map()
, filter()
, reduce()
nums = [1,2,3,4]
print(list(map(lambda x:x*2,nums))) # [2,4,6,8]
print(list(filter(lambda x:x%2==0,nums))) # [2,4]
let nums=[1,2,3,4];
console.log(nums.map(x=>x*2)); // [2,4,6,8]
console.log(nums.filter(x=>x%2===0)); // [2,4]
4️⃣ Mini Function Challenge
Write
introduce(name, age)
→"Hi, I'm Fahim and I am 30 years old."
Add a default parameter for age = 25
Try lambda / arrow function version
Part 2: Loops
1️⃣ What is a Loop?
Loops let you repeat code multiple times until a condition is met. Think of it as running a conveyor belt: the same action happens for every item until all are processed.
2️⃣ Loop Types
Loop Type | Description | Example |
---|---|---|
| Repeat a block a fixed number of times |
|
| Repeat until a condition is false |
|
| Iterate over arrays or object keys (JS) |
|
3️⃣ Loop Keywords
Break – exit the loop early
Continue – skip to the next iteration
Nested Loops – loops inside loops
for i in range(3):
for j in range(2):
print(i,j)
for(let i=0;i<3;i++){
for(let j=0;j<2;j++){
console.log(i,j);
}
}
4️⃣ Loop Patterns
Two Pointers / Sliding Window – fsequence problems
Loop Optimization – minimize iterations
Loop Invariants – maintain correct logic in each iteration
Nested Loops for Matrix / Grid problems
5️⃣ Mini Loop Challenge
Print all numbers from 1 to 20.
Print even numbers only.
Nested loop: print all pairs from
[1,2,3]
and[a,b]
.Expert: Find the maximum sum of 3 consecutive numbers in an array using a loop.
6️⃣ AI Tip 🤖
Ask AI:
"Write a function that uses a loop to sum all even numbers in an array and return the result."
This combines functions + loops, which is a classic interview problem.
✅ Sign-off
That’s your Loops & Functions Bite — Beginner to Expert! 🎉
Experiment with nested loops, break/continue, sliding windows, and combine with functions to solve real interview-style problems.
Next time, we’ll dive into Arrays, building on what you’ve learned today.