Function declaration and definition - Time & Space Complexity
When we write functions in C, it's important to know how their running time changes as inputs grow.
We want to see how the work inside a function scales with input size.
Analyze the time complexity of the following code snippet.
// Function declaration
int sumArray(int arr[], int n);
// Function definition
int sumArray(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
This function adds up all elements in an array of size n and returns the total.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that visits each element of the array once.
- How many times: Exactly n times, where n is the size of the array.
As the array size grows, the function does more additions, one per element.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of elements; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the function grows in a straight line with the input size.
[X] Wrong: "The function runs in constant time because it just sums values."
[OK] Correct: Even though the operation is simple, it must visit every element, so time grows with n.
Understanding how function time grows helps you explain your code clearly and shows you know how to write efficient programs.
"What if we changed the function to sum only the first half of the array? How would the time complexity change?"
Practice
Solution
Step 1: Understand function declaration role
A function declaration informs the compiler about the function's name, return type, and parameters so it can be used before its definition.Step 2: Differentiate declaration from definition
Declaration does not contain the function's code; that is done in the definition.Final Answer:
To tell the compiler about the function's name, return type, and parameters before its use -> Option CQuick Check:
Function declaration = Inform compiler [OK]
- Confusing declaration with definition
- Thinking declaration runs the function
- Assuming declaration allocates memory
Solution
Step 1: Identify correct syntax for declaration
A function declaration includes the return type, function name, parameter list, and ends with a semicolon.Step 2: Check each option
int add(int a, int b); matches correct syntax: return type 'int', name 'add', parameters '(int a, int b)', and semicolon. Others have syntax errors or missing semicolon.Final Answer:
int add(int a, int b); -> Option DQuick Check:
Declaration ends with semicolon [OK]
- Missing semicolon at end
- Including function body in declaration
- Wrong parameter syntax
#include <stdio.h>
int multiply(int x, int y);
int main() {
printf("%d", multiply(3, 4));
return 0;
}
int multiply(int x, int y) {
return x * y;
}Solution
Step 1: Understand function declaration and call
The function 'multiply' is declared before main and defined after. main calls multiply(3, 4).Step 2: Calculate function return value
multiply returns 3 * 4 = 12, which is printed by printf.Final Answer:
12 -> Option AQuick Check:
3 * 4 = 12 [OK]
- Thinking declaration runs code
- Confusing output with parameters
- Expecting compilation error due to declaration
int sum(int a, int b)
{
return a + b
}
Solution
Step 1: Check syntax inside function body
The return statement must end with a semicolon in C.Step 2: Identify missing semicolon
Here, 'return a + b' lacks a semicolon, causing a syntax error.Final Answer:
Missing semicolon after return statement -> Option BQuick Check:
Statements end with semicolon [OK]
- Omitting semicolon after return
- Misunderstanding parameter syntax
- Changing return type unnecessarily
Solution
Step 1: Check function declaration matches definition
Declaration and definition must have same return type (float) and parameters (three ints).Step 2: Ensure correct calculation for float average
To get float result, divide by 3.0 (float), not 3 (int).Step 3: Verify other options
Integer division (/ 3) causes truncation. Declaring int return but dividing by 3.0 causes type mismatch. Missing declaration prevents prior use.Final Answer:
float average(int a, int b, int c); float average(int a, int b, int c) { return (a + b + c) / 3.0; } -> Option AQuick Check:
Float division needs 3.0 [OK]
- Using integer division instead of float
- Mismatching declaration and definition types
- Omitting function declaration
Ready to prove it?
Pass the test and this pattern moves unknown
