Function prototypes - Time & Space 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?
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.
Look for loops or repeated calls that take time.
- Primary operation: One call to the
addfunction. - How many times: Exactly once in this example.
Since the function is called only once, the time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 call to add |
| 100 | 1 call to add |
| 1000 | 1 call to add |
Pattern observation: The number of operations stays the same no matter the input size.
Time Complexity: O(1)
This means the program runs in constant time because it calls the function only once.
[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.
Understanding how function prototypes affect program speed shows you know how code structure relates to performance.
"What if the function add was called inside a loop that runs n times? How would the time complexity change?"
Practice
function prototype in C?Solution
Step 1: Understand what a function prototype declares
A function prototype tells the compiler the function's name, return type, and parameter types without the body.Step 2: Differentiate prototype from definition and execution
It does not define the function's body or execute it; it only declares its interface.Final Answer:
To declare the function's name, return type, and parameters before its definition -> Option BQuick Check:
Function prototype = declaration before definition [OK]
- Confusing prototype with function definition
- Thinking prototype runs the function
- Believing prototype allocates memory
sum that takes two integers and returns an integer?Solution
Step 1: Check the syntax of a function prototype
A prototype includes return type, function name, parameter types, and ends with a semicolon.Step 2: Evaluate each option
int sum(int a, int b); matches the correct syntax. int sum(int a, int b) {} has braces (function body), so it's a definition. void sum(int a, int b); has wrong return type (void). int sum(a, b); lacks parameter types.Final Answer:
int sum(int a, int b); -> Option AQuick Check:
Prototype = return type + name + parameters + semicolon [OK]
- Adding braces {} in prototype
- Omitting parameter types
- Using wrong return type
int add(int x, int y);
int main() {
int result = add(3, 4);
printf("%d", result);
return 0;
}
int add(int a, int b) {
return a + b;
}
Solution
Step 1: Understand the prototype and function call
The prototype declaresaddbeforemain, so the compiler knows its signature.Step 2: Trace the function execution
add(3, 4)returns 3 + 4 = 7, which is printed byprintf.Final Answer:
7 -> Option CQuick Check:
Function call returns sum 3+4=7 [OK]
- Expecting compilation error without prototype
- Confusing concatenation with addition
- Ignoring return value
int multiply(int a, int b);
int main() {
int x = multiply(2, 3);
printf("%d", x);
return 0;
}
int multiply(int a, b) {
return a * b;
}
Solution
Step 1: Compare prototype and definition parameters
The prototype declaresint multiply(int a, int b);but the definition hasint multiply(int a, b)missing type forb.Step 2: Understand C parameter declaration rules
Each parameter must have a type. Missing type forbcauses a syntax error.Final Answer:
Incorrect parameter declaration in function definition -> Option DQuick Check:
All parameters need types in definition [OK]
- Omitting type for one parameter
- Assuming prototype fixes definition errors
- Missing semicolon after prototype
processData that takes an array of integers and its size, then returns a float average. Which prototype is correct?Solution
Step 1: Identify correct parameter types for array and size
In C, an array parameter can be declared asint arr[]orint *arr, but size must be separate.Step 2: Check return type and parameters
Function returns a float average, so return type isfloat. float processData(int arr[], int size); matches this withint arr[]andint size.Final Answer:
float processData(int arr[], int size); -> Option AQuick Check:
Array parameter + size + float return = correct prototype [OK]
- Using wrong return type
- Omitting size parameter
- Incorrect array parameter syntax
Ready to prove it?
Pass the test and this pattern moves unknown
