Functions & Reusability
EasyIn plain terms
A function is a reusable block of code with a name. Instead of copying the same logic in multiple places, you define it once and call it whenever needed. Functions can take parameters (inputs) and return a value (output).
Good functions do one thing well and have a clear name. They reduce duplication (DRY: Don't Repeat Yourself) and make programs easier to read and change. Breaking a big problem into small functions is a core programming skill.
What you need to know
- •Function: named, reusable block
- •Parameters (inputs) and return (output)
- •Single responsibility helps clarity
Example
Code is language-agnostic in spirit; adapt the idea to your language:
// Define once, use many times
function greet(name) {
return "Hello, " + name;
}
greet("Alice"); // "Hello, Alice"
greet("Bob"); // "Hello, Bob"
function add(a, b) {
return a + b;
}Why this matters
Functions are the backbone of clean code and are tied to Single Responsibility (one function, one job). Interviewers look for candidates who decompose problems into small, testable functions.
How it connects
Functions become methods inside classes (OOP). SOLID and DRY both emphasize avoiding duplication and keeping units of behavior focused—functions are where that starts.
Interview focus
Be ready to explain these; they come up often.
- ▸Explain parameters vs arguments; pass-by-value vs pass-by-reference when relevant.
- ▸Pure functions: same input → same output, no side effects; why they are easier to test.
- ▸Single responsibility: "A function should do one thing and do it well."
Learn more
Dive deeper with these resources: