Function calling in C++ - Time & Space Complexity
When we call a function in a program, it takes some time to run. Understanding how this time grows helps us write faster code.
We want to know: how does the time to run change when we call a function many times?
Analyze the time complexity of the following code snippet.
int square(int x) {
return x * x;
}
int main() {
int sum = 0;
int n = 100;
for (int i = 1; i <= n; i++) {
sum += square(i);
}
return 0;
}
This code calls a function square inside a loop to calculate the sum of squares from 1 to n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop runs from 1 to n. - How many times: The
squarefunction is called once each loop, so n times total.
As n grows, the number of times we call the function grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to square |
| 100 | 100 calls to square |
| 1000 | 1000 calls to square |
Pattern observation: The total work grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of times we call the function.
[X] Wrong: "Calling a simple function inside a loop adds no extra time."
[OK] Correct: Even a simple function call takes time, and calling it n times adds up, so it affects total time.
Knowing how function calls add up helps you explain code speed clearly and shows you understand how programs work step by step.
"What if the function square called another function inside it? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a function call means
Calling a function means telling the program to run the code inside that function.Step 2: Identify the effect of calling a function
When called, the function's instructions execute, performing its task.Final Answer:
Runs the code inside the function -> Option AQuick Check:
Function call = run function code [OK]
- Thinking calling creates or deletes functions
- Confusing function call with function definition
- Assuming function call changes function name
printMessage in C++?Solution
Step 1: Recall the syntax for calling a function
In C++, to call a function, write its name followed by parentheses and a semicolon.Step 2: Check each option
printMessage(); uses the correct syntax:printMessage();. Others are incorrect or invalid syntax.Final Answer:
printMessage(); -> Option AQuick Check:
Function call syntax = name + () + ; [OK]
- Omitting parentheses when calling functions
- Adding keywords like 'call' or 'function' incorrectly
- Missing semicolon at the end
void greet() {
std::cout << "Hello!" << std::endl;
}
int main() {
greet();
return 0;
}Solution
Step 1: Understand what the function greet() does
The function prints "Hello!" followed by a new line.Step 2: See what main() does
Main calls greet(), so "Hello!" is printed to the screen.Final Answer:
Hello! -> Option DQuick Check:
Function call prints "Hello!" [OK]
- Expecting function name printed instead of output
- Thinking no output because no return value
- Assuming compilation error without syntax issues
void sayHi() {
std::cout << "Hi!" << std::endl;
}
int main() {
sayHi;
return 0;
}Solution
Step 1: Check how sayHi is called
In main(), sayHi is written without parentheses, so it is not called.Step 2: Understand correct function call syntax
To call sayHi, it must besayHi();with parentheses.Final Answer:
Missing parentheses when calling sayHi -> Option BQuick Check:
Function call needs parentheses [OK]
- Forgetting parentheses on function calls
- Thinking function name alone calls it
- Ignoring compiler errors about function usage
int add(int a, int b) {
return a + b;
}What is the correct way to call
add to get the sum of 5 and 3 and store it in result?Solution
Step 1: Check the function parameters
The function add requires two integers as input: a and b.Step 2: Match the call with two arguments
int result = add(5, 3); passes both 5 and 3 correctly. Others pass wrong number or format of arguments.Final Answer:
int result = add(5, 3); -> Option CQuick Check:
Function call needs correct number of arguments [OK]
- Passing fewer or more arguments than function needs
- Passing expressions without commas for multiple args
- Calling function without any arguments when required
Ready to prove it?
Pass the test and this pattern moves unknown
