Code organization with functions in Arduino - Time & Space Complexity
When we organize Arduino code using functions, it helps keep things neat and clear.
We want to see how the time to run the program changes as we call these functions more or less.
Analyze the time complexity of the following code snippet.
void blinkLED(int times) {
for (int i = 0; i < times; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
blinkLED(5);
delay(1000);
}
This code defines a function to blink an LED a number of times, then calls it repeatedly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop inside the blinkLED function that runs 'times' times.
- How many times: The loop runs exactly as many times as the 'times' argument given (5 in this example).
Explain the growth pattern intuitively.
| Input Size (times) | Approx. Operations |
|---|---|
| 10 | About 10 LED blinks |
| 100 | About 100 LED blinks |
| 1000 | About 1000 LED blinks |
Pattern observation: The time to run grows directly with the number of blinks requested.
Time Complexity: O(n)
This means the time grows in a straight line as you increase the number of times the LED blinks.
[X] Wrong: "Using functions always makes the program run faster."
[OK] Correct: Functions help organize code but do not change how many times loops run or how long delays last.
Understanding how functions affect time helps you write clear code and explain your choices confidently.
"What if we changed the blinkLED function to call itself recursively instead of using a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand what functions do
Functions group code into small blocks that can be used many times.Step 2: Identify the main benefit
This grouping helps keep code clean and easy to manage.Final Answer:
To organize code into reusable parts -> Option BQuick Check:
Functions = reusable code blocks [OK]
- Thinking functions make code run faster
- Believing functions increase program size
- Confusing functions with variables
blinkLED that takes no parameters and returns nothing in Arduino?Solution
Step 1: Recall Arduino function syntax
Functions start with return type, then name, then parentheses for parameters.Step 2: Match syntax for no return and no parameters
Usevoidfor no return and empty parentheses for no parameters.Final Answer:
void blinkLED() { /* code */ } -> Option AQuick Check:
void + name() = correct function declaration [OK]
- Using 'function' keyword (not Arduino style)
- Putting return type after function name
- Omitting parentheses
void setup() {
Serial.begin(9600);
printSum(3, 4);
}
void loop() {}
void printSum(int a, int b) {
Serial.println(a + b);
}Solution
Step 1: Understand function call and parameters
The functionprintSumis called with 3 and 4 as inputs.Step 2: Calculate the sum inside the function
The function adds 3 + 4 = 7 and prints it to Serial Monitor.Final Answer:
7 -> Option CQuick Check:
3 + 4 = 7 printed [OK]
- Thinking '34' is printed as string concatenation
- Expecting a return value instead of print
- Assuming no output because loop is empty
void setup() {
blinkLED();
}
void blinkLED {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}Solution
Step 1: Check function declaration syntax
The functionblinkLEDis declared without parentheses, which is invalid.Step 2: Confirm correct syntax
Function declarations must have parentheses even if no parameters:void blinkLED() { ... }.Final Answer:
Missing parentheses after function name in declaration -> Option DQuick Check:
Function declaration needs () [OK]
- Forgetting parentheses in function declaration
- Adding semicolon after function call (not needed)
- Misunderstanding digitalWrite usage
setLED that turns an LED on or off based on a parameter. Which of these function definitions correctly uses a parameter to control the LED state on pin 13?Solution
Step 1: Understand parameter type for LED state
LED state is usually HIGH or LOW, which are integers in Arduino.Step 2: Check function that uses parameter correctly
void setLED(int state) { digitalWrite(13, state); } takes an int parameter and passes it directly to digitalWrite on pin 13.Step 3: Evaluate other options
B ignores parameter, C wrongly returns digitalWrite result, D inverts boolean state which may confuse usage.Final Answer:
void setLED(int state) { digitalWrite(13, state); } -> Option AQuick Check:
Parameter controls LED state directly [OK]
- Returning digitalWrite result (void function expected)
- Ignoring parameter and hardcoding LED state
- Inverting boolean state unintentionally
Ready to prove it?
Pass the test and this pattern moves unknown
