Sub topic 12
Parameters and Return Values in Arduino Functions
Prerequisites and the working model
This chapter assumes you can open, verify, and upload an Arduino sketch. You should also understand setup(), loop(), variables, basic types, and Serial.println().
A function can receive data from its caller and send a result back. The received data is called a parameter. The value sent back is the return value.
Think of a function as a small machine:
- The caller supplies arguments.
- The function stores those arguments in parameters.
- The function performs its work.
- The function returns a result, if it has one.
An argument is the actual value placed inside a function call. A parameter is the variable declared in the function definition.
Defining parameters
Place each parameter inside the function's parentheses. Give every parameter a data type and a name.
void setLed(int pin, bool state) {
pinMode(pin, OUTPUT);
digitalWrite(pin, state ? HIGH : LOW);
}This function has two parameters:
pinis anintthat identifies the output pin.stateis aboolthat represents on or off.
The function's return type is void, which means it returns no value. You can call it like this:
setLed(LED_BUILTIN, true);
setLed(LED_BUILTIN, false);The arguments LED_BUILTIN and true are copied into pin and state. Changing a parameter inside the function does not change the original argument.
Returning a value
Replace void with the type of value the function will return. Use return to send that value back.
int readAverage(int pin, byte samples) {
long total = 0;
for (byte i = 0; i < samples; i++) {
total += analogRead(pin);
delay(2);
}
return total / samples;
}
void setup() {
Serial.begin(9600);
}
void loop() {
int average = readAverage(A0, 10);
Serial.print("Average reading: ");
Serial.println(average);
delay(500);
}readAverage() returns an int. The caller stores that result in average.
The return type and the returned expression should agree. A function returning bool should return true or false. A function returning float should return a decimal measurement when appropriate.
A function stops running as soon as it reaches return. Code after that statement in the same execution path does not run.
| Function form | Meaning | Example call |
|---|---|---|
| `void f()` | Performs an action and returns nothing | `setLed(13, true);` |
| `int f()` | Returns a whole-number result | `int value = readValue();` |
| `bool f()` | Returns true or false | `if (isDark())` |
| `float f()` | Returns a decimal result | `float volts = readVoltage();` |
Choosing what a function returns
Return a value when the caller needs a result to make another decision or calculation. For example:
bool isButtonPressed(int pin) {
return digitalRead(pin) == LOW;
}
void setup() {
pinMode(2, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
if (isButtonPressed(2)) {
Serial.println("Pressed");
}
delay(100);
}Here, the function hides the hardware detail while the caller expresses the program's intent.
Common errors
- Wrong argument count: The call must provide one argument for each parameter.
- Wrong type: Passing a floating-point value to an integer parameter can discard its fractional part.
- Missing return: Every path in a non-
voidfunction should produce a value. - Using a result incorrectly: A returned value is not automatically stored. Assign it to a variable or use it in an expression.
- Division by zero: Validate a parameter such as
samplesbefore using it as a divisor.
Exercise
Write a function named largerValue that accepts two int parameters and returns the larger one. Then call it from loop() and print the result.
Show answer
Use int largerValue(int a, int b) { return a > b ? a : b; }, then call Serial.println(largerValue(12, 19));.