0
0
Arduinoprogramming~20 mins

Code organization with functions in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of function call with parameters

What will be printed on the Serial Monitor when this Arduino code runs?

Arduino
void setup() {
  Serial.begin(9600);
  printSum(5, 3);
}

void loop() {}

void printSum(int a, int b) {
  Serial.println(a + b);
}
AError: missing return type
B8
C53
D0
Attempts:
2 left
💡 Hint

Remember that Serial.println prints the value passed to it.

🧠 Conceptual
intermediate
1:30remaining
Purpose of the setup() function

What is the main purpose of the setup() function in an Arduino sketch?

ATo run code repeatedly in a loop
BTo define custom functions
CTo declare global variables
DTo run code once at the start of the program
Attempts:
2 left
💡 Hint

Think about what happens when the Arduino first turns on.

🔧 Debug
advanced
2:00remaining
Identify the error in function definition

What error will this Arduino code cause?

Arduino
void setup() {
  Serial.begin(9600);
  greet();
}

void loop() {}

void greet() {
  Serial.println("Hello, world!");
}
ASyntaxError: missing semicolon
BRuntimeError: greet not defined
CNo error, prints 'Hello, world!'
DTypeError: wrong function return type
Attempts:
2 left
💡 Hint

Check punctuation at the end of each statement.

Predict Output
advanced
2:30remaining
Output of nested function calls

What will be printed on the Serial Monitor after running this code?

Arduino
void setup() {
  Serial.begin(9600);
  int result = multiply(add(2, 3), 4);
  Serial.println(result);
}

void loop() {}

int add(int x, int y) {
  return x + y;
}

int multiply(int a, int b) {
  return a * b;
}
A20
B14
CError: multiply not defined
D10
Attempts:
2 left
💡 Hint

Calculate the inner function first, then the outer.

📝 Syntax
expert
1:30remaining
Correct function prototype declaration

Which option correctly declares a function prototype for a function named blinkLED that takes an int parameter and returns nothing?

AblinkLED(void int duration);
Bint blinkLED(int duration);
Cvoid blinkLED(int duration);
Dvoid blinkLED(duration int);
Attempts:
2 left
💡 Hint

Remember the syntax: return_type function_name(parameter_type parameter_name);