What if you could fix a bug once and have it fixed everywhere instantly?
Why Reusability and maintenance? - Purpose & Use Cases
Imagine writing the same block of code over and over again every time you need to perform a simple task, like calculating the area of a rectangle, in your C program.
Every time you want to change how the area is calculated, you have to find and update each copy manually.
This manual approach is slow and tiring because you repeat yourself a lot.
It is easy to make mistakes by forgetting to update some copies, causing bugs.
Also, the code becomes messy and hard to understand or fix later.
By using functions and reusable code blocks, you write the calculation once and call it whenever needed.
This keeps your code clean, easy to update, and less error-prone.
int area1 = length1 * width1; int area2 = length2 * width2; // repeated code
int area(int length, int width) {
return length * width;
}
int area1 = area(length1, width1);
int area2 = area(length2, width2);It enables you to build programs that are easier to fix, update, and expand without rewriting code.
Think of a recipe book where you write a recipe once and use it many times instead of rewriting the whole recipe every time you cook.
Writing reusable code saves time and effort.
It reduces mistakes by avoiding repeated code.
It makes programs easier to maintain and improve.
Practice
Solution
Step 1: Understand the purpose of functions
Functions allow you to group code that performs a task so you can reuse it multiple times without rewriting.Step 2: Connect reusability to maintenance
Using functions reduces repeated code, making it easier to update and maintain.Final Answer:
You can write code once and use it many times -> Option CQuick Check:
Functions improve reusability = You can write code once and use it many times [OK]
- Thinking functions always make code faster
- Confusing functions with variables
- Believing functions fix all bugs automatically
add that takes two integers and returns an integer in C?Solution
Step 1: Identify correct function declaration syntax
In C, a function declaration specifies the return type, function name, and parameter types inside parentheses.Step 2: Check each option
int add(int a, int b); matches the correct syntax:int add(int a, int b);. Others have wrong order, keywords, or syntax.Final Answer:
int add(int a, int b); -> Option AQuick Check:
Correct C function declaration = int add(int a, int b); [OK]
- Using 'function' keyword (not in C)
- Wrong parameter syntax
- Missing return type
int square(int x) {
return x * x;
}
int main() {
int result = square(4);
printf("%d", result);
return 0;
}Solution
Step 1: Understand the function behavior
The functionsquarereturns the input multiplied by itself, sosquare(4)returns 16.Step 2: Check the main function output
The main function callssquare(4), stores 16 inresult, and prints it withprintf.Final Answer:
16 -> Option DQuick Check:
square(4) = 16 [OK]
- Confusing multiplication with addition
- Expecting 8 instead of 16
- Thinking code has syntax errors
int multiply(int a, int b) {
return a * b;
}
int main() {
int result = multiply(5);
printf("%d", result);
return 0;
}Solution
Step 1: Check function definition and call
The functionmultiplyrequires two integer arguments, but inmainit is called with only one argument.Step 2: Identify the error type
This mismatch causes a compilation error because the number of arguments does not match the function signature.Final Answer:
multiply is called with only one argument instead of two -> Option AQuick Check:
Argument count mismatch = multiply is called with only one argument instead of two [OK]
- Ignoring argument count mismatch
- Thinking printf format is wrong
- Assuming main must return void
Solution
Step 1: Understand reusability principles
Reusable code should be modular, accept inputs, and avoid global state to be flexible and maintainable.Step 2: Evaluate each option
Create a function that takes an integer array and its size, then returns the average creates a function with parameters for array and size, making it reusable for any array. Others either duplicate code or rely on globals, reducing reusability.Final Answer:
Create a function that takes an integer array and its size, then returns the average -> Option BQuick Check:
Function with parameters improves reusability = Create a function that takes an integer array and its size, then returns the average [OK]
- Using global variables reduces flexibility
- Duplicating code increases errors
- Writing code only inside main limits reuse
Ready to prove it?
Pass the test and this pattern moves unknown
