Parameter passing mechanisms in Compiler Design - Time & Space Complexity
When a program calls a function, it passes data to it using parameter passing mechanisms. Understanding how these mechanisms affect the time taken helps us see how function calls scale as input grows.
We want to know: how does the cost of passing parameters change when the size or number of parameters increases?
Analyze the time complexity of passing parameters by value and by reference.
void func(int x, int &y) {
x = x + 1; // pass by value
y = y + 1; // pass by reference
}
int main() {
int a = 5, b = 10;
func(a, b);
}
This code shows a function receiving one parameter by value and one by reference, then modifying them.
Look for operations that repeat or scale with input size.
- Primary operation: Copying the parameter passed by value.
- How many times: Once per parameter passed by value, for each function call.
As the number or size of parameters passed by value grows, the copying work grows too.
| Input Size (number or size of parameters) | Approx. Operations (copies) |
|---|---|
| 10 | 10 copies |
| 100 | 100 copies |
| 1000 | 1000 copies |
Pattern observation: The copying work grows directly with the number or size of parameters passed by value.
Time Complexity: O(n)
This means the time to pass parameters grows linearly with the number or size of parameters passed by value.
[X] Wrong: "Passing parameters by reference takes the same time as by value because both send data to the function."
[OK] Correct: Passing by reference only passes an address, which is a small fixed cost, while passing by value copies the entire data, which can be much larger and slower.
Knowing how parameter passing affects time helps you explain function call costs clearly. This skill shows you understand how programs manage data efficiently.
"What if we changed all parameters to be passed by reference? How would the time complexity change?"