Course Content
IoT Engineering Course using ESP32 Wifi Robots

📘 Functions (Modular Programming)


🎯 Lesson Objective

By the end of this lesson, students will:

  • Understand what a function is

  • Learn why functions are important

  • Create custom functions

  • Understand parameters and return values

  • Write modular and structured IoT code

  • Avoid common mistakes


1️⃣ What is a Function?

A function is a block of code that performs a specific task.

In simple words:

A function is a reusable piece of code.

Instead of writing the same code again and again,
we put it inside a function and call it whenever needed.


2️⃣ Why Functions Are Important in IoT

As projects grow:

  • Code becomes long

  • Logic becomes complex

  • Debugging becomes difficult

Functions help by:

✔ Breaking code into smaller parts
✔ Improving readability
✔ Making debugging easier
✔ Making code reusable
✔ Organizing logic properly

Professional IoT systems always use modular programming.


3️⃣ Built-in Functions in Arduino

You already use functions:

 
Serial.begin(115200);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
 

These are functions.

You didn’t write them —
Arduino library provides them.

Now you will learn to create your own.


4️⃣ Structure of a Function

General syntax:

 
returnType functionName(parameters) {
// code block
}
 

Let’s break it down:

  • returnType → What the function returns

  • functionName → Name of the function

  • parameters → Inputs to function

  • code block → Task performed


5️⃣ Simple Function Without Parameters

Example:

 
void blinkLED() {
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
delay(500);
}
 

Explanation:

  • void → Function returns nothing

  • blinkLED → Function name

  • No parameters

Now call it inside loop():

 
void loop() {
blinkLED();
}
 

This is modular programming.


6️⃣ Function with Parameters

Sometimes functions need input.

Example:

 
void blinkLED(int times) {
for(int i = 0; i < times; i++) {
digitalWrite(2, HIGH);
delay(300);
digitalWrite(2, LOW);
delay(300);
}
}
 

Call it:

 
blinkLED(5);
 

This makes LED blink 5 times.

Parameter = input value.


7️⃣ Function with Return Value

Some functions calculate and return a result.

Example:

 
int addNumbers(int a, int b) {
return a + b;
}
 

Usage:

 
int result = addNumbers(10, 5);
 

Function returns value.


8️⃣ Real IoT Example – Temperature Function

 
float readTemperature() {
float temp = analogRead(34);
return temp;
}
 

Usage:

 
void loop() {
float temperature = readTemperature();
Serial.println(temperature);
}
 

This makes code clean and readable.


9️⃣ Modular IoT Example – Smart AC

Instead of writing everything inside loop():

Bad approach:

 
void loop() {
float temp = analogRead(34);
if(temp > 30) {
digitalWrite(26, HIGH);
}
}
 

Better modular approach:

 
float readTemperature() {
return analogRead(34);
}

void controlAC(float temperature) {
if(temperature > 30) {
digitalWrite(26, HIGH);
} else {
digitalWrite(26, LOW);
}
}

void loop() {
float temp = readTemperature();
controlAC(temp);
}

 

This is professional structure.


🔟 Why Modular Code is Powerful

If something goes wrong:

You only debug that function.

Example:

Temperature issue → Check readTemperature()
Relay issue → Check controlAC()

Instead of checking 300 lines of code.


1️⃣1️⃣ Function Declaration Order

Functions must be declared before use.

Either:

Place function above loop()

OR

Declare prototype at top:

 
float readTemperature();
 

For beginners:

Keep function above loop().


1️⃣2️⃣ Scope Inside Functions

Variables declared inside a function are local.

Example:

 
void test() {
int x = 5;
}
 

You cannot access x outside test().

This prevents accidental modification.


1️⃣3️⃣ Passing Multiple Parameters

Example:

 
void controlRelay(int pin, bool state) {
digitalWrite(pin, state);
}
 

Call:

 
controlRelay(26, HIGH);
 

This allows reusable hardware control.


1️⃣4️⃣ Best Practices for Functions

✔ Keep functions small
✔ Give meaningful names
✔ Do one task per function
✔ Avoid very long functions
✔ Reuse logic

Good naming example:

readGasSensor()
checkWaterLevel()
updateDashboard()

Bad naming:

func1()
abc()


1️⃣5️⃣ Common Beginner Mistakes

❌ Forgetting return statement
❌ Using wrong return type
❌ Declaring function inside loop()
❌ Not passing required parameters
❌ Mixing global and local variables incorrectly

Always match return type and returned value.


1️⃣6️⃣ Real Project Thinking

In Mega Project, you will use:

  • readTemperature()

  • readGasSensor()

  • controlAC()

  • controlPump()

  • updateBlynk()

Each system part becomes a function.

That is real IoT architecture.


📌 Lesson Summary

In this lesson, we learned:

  • What a function is

  • Why modular programming is important

  • How to create functions

  • Parameters and return values

  • Writing structured IoT code

  • Debugging using functions

Functions make your code:

Clean
Professional
Scalable

Scroll to Top