Reusability and maintenance - Time & Space Complexity
When we write reusable and maintainable code, we want to know how it affects how long the program takes to run.
We ask: does making code reusable change how the program grows with bigger inputs?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int data[] = {1, 2, 3, 4, 5};
printArray(data, 5);
return 0;
}
This code uses a reusable function to print elements of an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop inside
printArraythat goes through each array element. - How many times: It runs once for each element in the array, so
sizetimes.
As the array gets bigger, the loop runs more times, directly matching the size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loop steps |
| 100 | 100 loop steps |
| 1000 | 1000 loop steps |
Pattern observation: The number of steps grows evenly as the input size grows.
Time Complexity: O(n)
This means the time to run grows in a straight line with the size of the input.
[X] Wrong: "Making code reusable always makes it slower or more complex."
[OK] Correct: Reusable code often just wraps the same steps in a function, so the main work still grows the same way with input size.
Understanding how reusable functions affect time helps you write clean code without worrying about hidden slowdowns.
"What if the reusable function called another function inside a nested loop? How would the time complexity change?"
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
