Bird
Raised Fist0
Arduinoprogramming~10 mins

Code organization with functions in Arduino - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function named blinkLED.

Arduino
void [1]() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}
Drag options to blanks, or click blank then click option'
AblinkLED
BstartBlink
CledBlink
DflashLED
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than blinkLED.
Misspelling the function name.
2fill in blank
medium

Complete the code to call the function blinkLED inside loop().

Arduino
void loop() {
  [1]();
}
Drag options to blanks, or click blank then click option'
AstartBlink
Bdelay
CdigitalWrite
DblinkLED
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a function that does not exist.
Forgetting the parentheses after the function name.
3fill in blank
hard

Fix the error in the function definition by completing the return type.

Arduino
[1] blinkLED() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}
Drag options to blanks, or click blank then click option'
Avoid
Bboolean
Cint
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using a return type other than void when no value is returned.
Omitting the return type completely.
4fill in blank
hard

Fill in the blank to create a function that takes an integer parameter times and blinks the LED that many times.

Arduino
void blinkLED([1] times) {
  for (int i = 0; i < times; i++) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
  }
}
Drag options to blanks, or click blank then click option'
Aboolean
Bvoid
Cint
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as a parameter type.
Using a floating-point type like float when an integer is needed.
5fill in blank
hard

Fill in the blanks to call blinkLED with the argument 3 inside loop().

Arduino
void loop() {
  [1]([2]);
}
Drag options to blanks, or click blank then click option'
AdigitalWrite
B3
CblinkLED
Ddelay
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a wrong function name.
Passing the argument without parentheses.

Practice

(1/5)
1. What is the main purpose of using functions in Arduino programming?
easy
A. To increase the size of the program
B. To organize code into reusable parts
C. To make the program run faster
D. To avoid using variables

Solution

  1. Step 1: Understand what functions do

    Functions group code into small blocks that can be used many times.
  2. Step 2: Identify the main benefit

    This grouping helps keep code clean and easy to manage.
  3. Final Answer:

    To organize code into reusable parts -> Option B
  4. Quick Check:

    Functions = reusable code blocks [OK]
Hint: Functions group code for reuse and clarity [OK]
Common Mistakes:
  • Thinking functions make code run faster
  • Believing functions increase program size
  • Confusing functions with variables
2. Which of the following is the correct way to declare a function named blinkLED that takes no parameters and returns nothing in Arduino?
easy
A. void blinkLED() { /* code */ }
B. int blinkLED() { /* code */ }
C. blinkLED void() { /* code */ }
D. function blinkLED() { /* code */ }

Solution

  1. Step 1: Recall Arduino function syntax

    Functions start with return type, then name, then parentheses for parameters.
  2. Step 2: Match syntax for no return and no parameters

    Use void for no return and empty parentheses for no parameters.
  3. Final Answer:

    void blinkLED() { /* code */ } -> Option A
  4. Quick Check:

    void + name() = correct function declaration [OK]
Hint: Use 'void' for no return and () for no parameters [OK]
Common Mistakes:
  • Using 'function' keyword (not Arduino style)
  • Putting return type after function name
  • Omitting parentheses
3. What will be the output on the Serial Monitor after running this Arduino code?
void setup() {
  Serial.begin(9600);
  printSum(3, 4);
}

void loop() {}

void printSum(int a, int b) {
  Serial.println(a + b);
}
medium
A. Error: missing return
B. 34
C. 7
D. Nothing

Solution

  1. Step 1: Understand function call and parameters

    The function printSum is called with 3 and 4 as inputs.
  2. Step 2: Calculate the sum inside the function

    The function adds 3 + 4 = 7 and prints it to Serial Monitor.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    3 + 4 = 7 printed [OK]
Hint: Add parameters inside function and print result [OK]
Common Mistakes:
  • Thinking '34' is printed as string concatenation
  • Expecting a return value instead of print
  • Assuming no output because loop is empty
4. Identify the error in this Arduino code snippet:
void setup() {
  blinkLED();
}

void blinkLED {
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}
medium
A. delay function requires a return value
B. Missing semicolon after function call
C. digitalWrite cannot be used inside functions
D. Missing parentheses after function name in declaration

Solution

  1. Step 1: Check function declaration syntax

    The function blinkLED is declared without parentheses, which is invalid.
  2. Step 2: Confirm correct syntax

    Function declarations must have parentheses even if no parameters: void blinkLED() { ... }.
  3. Final Answer:

    Missing parentheses after function name in declaration -> Option D
  4. Quick Check:

    Function declaration needs () [OK]
Hint: Always include () after function name in declaration [OK]
Common Mistakes:
  • Forgetting parentheses in function declaration
  • Adding semicolon after function call (not needed)
  • Misunderstanding digitalWrite usage
5. You want to create a function 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?
hard
A. void setLED(int state) { digitalWrite(13, state); }
B. void setLED() { digitalWrite(13, HIGH); }
C. int setLED(int state) { return digitalWrite(13, state); }
D. void setLED(boolean state) { digitalWrite(13, !state); }

Solution

  1. Step 1: Understand parameter type for LED state

    LED state is usually HIGH or LOW, which are integers in Arduino.
  2. 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.
  3. Step 3: Evaluate other options

    B ignores parameter, C wrongly returns digitalWrite result, D inverts boolean state which may confuse usage.
  4. Final Answer:

    void setLED(int state) { digitalWrite(13, state); } -> Option A
  5. Quick Check:

    Parameter controls LED state directly [OK]
Hint: Use int parameter for HIGH/LOW and pass to digitalWrite [OK]
Common Mistakes:
  • Returning digitalWrite result (void function expected)
  • Ignoring parameter and hardcoding LED state
  • Inverting boolean state unintentionally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard