0
0
Cprogramming~5 mins

Function prototypes - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Function prototypes
O(1)
Understanding Time Complexity

We want to see how the time a program takes changes when it uses function prototypes.

Does adding a function prototype affect how long the program runs?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


#include <stdio.h>

// Function prototype
int add(int a, int b);

int main() {
    int result = add(5, 3);
    printf("Result: %d\n", result);
    return 0;
}

int add(int a, int b) {
    return a + b;
}
    

This code declares a function prototype for add, then calls it in main.

Identify Repeating Operations

Look for loops or repeated calls that take time.

  • Primary operation: One call to the add function.
  • How many times: Exactly once in this example.
How Execution Grows With Input

Since the function is called only once, the time does not grow with input size.

Input Size (n)Approx. Operations
101 call to add
1001 call to add
10001 call to add

Pattern observation: The number of operations stays the same no matter the input size.

Final Time Complexity

Time Complexity: O(1)

This means the program runs in constant time because it calls the function only once.

Common Mistake

[X] Wrong: "Adding a function prototype makes the program slower because it adds extra code."

[OK] Correct: The prototype only tells the compiler about the function; it does not add extra work when running the program.

Interview Connect

Understanding how function prototypes affect program speed shows you know how code structure relates to performance.

Self-Check

"What if the function add was called inside a loop that runs n times? How would the time complexity change?"