6

Lesson 6: Functions with Arduino

Organize repeated jobs into clear, reusable blocks of code

32-Lesson Course • Semester 1

You already know how Arduino makes decisions with conditionals and repeats actions with loops. Now you will learn how to organize code into functions so a program becomes easier to read, reuse, and improve.

Learning Objectives

By the end of this Lesson, you will:

  • Understand what a function is and why programmers use functions
  • Write simple functions that perform one clear job
  • Call functions from setup() and loop()
  • Pass values into a function using parameters

Skills Developed:

  • Breaking a larger task into smaller steps
  • Making code easier to read and edit
  • Reusing code instead of copying the same lines again and again
  • Preparing for more advanced Arduino programs later

Section 1: What Is a Function?

A function is a named block of code

A function is a group of instructions that performs one job. Arduino already gives you two built-in functions: setup() and loop(). In this lesson, you will start making your own functions too.

Why functions help

  • They reduce repetition: write the code once and call it many times
  • They improve readability: a name like printResult() explains the job
  • They make editing easier: change the function once instead of changing repeated code everywhere
  • They help you think clearly: each function should do one simple task

A function definition has a pattern

returnType functionName(parameters) {
  // code the function will run
}

Important parts

  • returnType: often void for now
  • functionName: the name you choose
  • parameters: extra information sent into the function
  • braces: the lines that belong to the function

What void means

For now, most of your functions will use void. That means the function does a job, but it does not send a value back.

Section 2: Your First Custom Function

Start with a simple printing function

You have already used the Serial Monitor many times. Instead of writing the same printing lines over and over, you can place them inside a function.

void setup() {
  Serial.begin(9600);
  printWelcome();
}

void loop() {
}

void printWelcome() {
  Serial.println("Welcome to Lesson 6!");
  Serial.println("We are learning functions.");
}

What happens here?

  • setup() runs once: it starts Serial communication and then calls printWelcome()
  • printWelcome() runs its own code: it prints two lines
  • The function name acts like a command: when Arduino sees printWelcome();, it jumps to that function and runs it

Section 3: Functions Make Repeated Work Easier

Without a function

void setup() {
  Serial.begin(9600);

  Serial.println("=== Result ===");
  Serial.print("Answer: ");
  Serial.println(12);

  Serial.println("=== Result ===");
  Serial.print("Answer: ");
  Serial.println(25);
}

With a function

void setup() {
  Serial.begin(9600);

  printResult(12);
  printResult(25);
}

void loop() {
}

void printResult(int answer) {
  Serial.println("=== Result ===");
  Serial.print("Answer: ");
  Serial.println(answer);
}

New idea: parameters

The answer variable is a parameter. It receives the value sent into the function. When you call printResult(12);, the function uses 12. When you call printResult(25);, the same function uses 25.

Section 4: Functions and Calculations

Use a function to display a conversion

This connects back to variables and formulas. You still use variables and calculations, but now you organize the work using a function.

void setup() {
  Serial.begin(9600);

  showTemperatureConversion(20.0);
  showTemperatureConversion(25.0);
  showTemperatureConversion(30.0);
}

void loop() {
}

void showTemperatureConversion(float celsius) {
  float fahrenheit = (celsius * 9.0 / 5.0) + 32.0;

  Serial.print(celsius);
  Serial.print(" C = ");
  Serial.print(fahrenheit);
  Serial.println(" F");
}

Why this is better

  • The formula is written once
  • The print format is written once
  • You can test many values by changing only the function call

What to notice

  • celsius is the parameter
  • fahrenheit is created inside the function
  • The function performs both a calculation and clear Serial output

Section 5: Functions with Hardware

Functions are especially helpful with Arduino hardware because they let you give clear names to repeated actions. Instead of repeating `digitalWrite()` lines, you can create functions like turnLedOn() and turnLedOff().

const int ledPin = 8;
const int buttonPin = 2;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  checkButton();
}

void checkButton() {
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    turnLedOn();
  } else {
    turnLedOff();
  }
}

void turnLedOn() {
  digitalWrite(ledPin, HIGH);
}

void turnLedOff() {
  digitalWrite(ledPin, LOW);
}

Why this code is clearer

  • loop() stays short and easy to read
  • checkButton() describes the decision job
  • turnLedOn() and turnLedOff() describe the output jobs

Section 6: Common Beginner Mistakes

Mistake 1

Forgetting the parentheses when calling a function.

Mistake 2

Using a function name that does not describe the job clearly.

Mistake 3

Copying repeated code instead of placing it in a function.

Mistake 4

Trying to make one function do too many different jobs.

Hands-On Activity: Function-Based LED Program

Build or simulate a button-controlled LED program that uses functions. Your program should:

  • Use a function to check the button
  • Use one function to turn the LED on
  • Use one function to turn the LED off
  • Keep loop() short and easy to read

Extension: add another function that blinks the LED three times when the button is pressed.

Assessment and Homework

Lesson 6 quiz topics

  • What a function is
  • How to define and call a function
  • What void means
  • What parameters do
  • How functions help with hardware code

Homework ideas

  • Write one function that prints a welcome message
  • Write one function that prints a score using a parameter
  • Rewrite a repeated LED action using a function
  • Be ready to use functions in bigger projects later
Good programmers name functions clearly and let each function do one simple job.