📚 Introducing Functions and Libraries in C
🎯 Learning Objectives
By the end of this lesson, you will:
- Understand the purpose and syntax of functions in C
- Prepare for modular design by introducing the concept of libraries
- Generalize the delay using a parameterized function
1️⃣ Conceptual Warm-Up: Why Functions?
Start with a relatable analogy: “Imagine writing the same instructions over and over to boil water. Wouldn’t it be easier to say ‘boilWater()’ and let that do the work?”
Explain that functions:
- Reduce repetition
- Improve readability
- Enable modular design
- Make debugging easier
2️⃣ What Is a Function?
A function is a reusable block of code that performs a specific task. It can take inputs (parameters), perform operations, and optionally return a result. Example:
int add(int a, int b)
{
return a + b;
}
3️⃣ Function Prototypes: The Blueprint
A function prototype tells the compiler:
- The function’s name
- Its return type
- The types of its parameters It’s like declaring, “This function exists—trust me, I’ll define it later.” Example:
int add(int a, int b); // Prototype
- This allows you to call
add()before its actual definition appears in the code.
4️⃣ Function Prototypes: The Blueprint
| Type | Location | Visibility | Purpose |
|---|---|---|---|
| Local | Inside .c file |
Private to that file | Used for internal helper functions |
| Public | Inside .h file |
Visible to other files | Used to expose functionality to other modules |
🔒 Local Prototype Example
// delay.c
static void calibrateDelay(void); // Only used inside delay.c
void delay1ms(void) {
calibrateDelay();
}
-
statickeyword restricts visibility to the current file -
Keeps internal logic hidden from other modules
🌐 Public Prototype Example
// delay.h void delay1ms(void); void delayMs(int ms);
-
These are visible to any
.cfile that includesdelay.h -
Promotes modularity and reuse
5️⃣ Why Use Header Files?
Header files (.h) act as interfaces:
- They define what functions are available
- They hide implementation details
- They allow multiple
.cfiles to share functionality cleanly Example usage:
#include "delay.h"
int main(void) {
delayMs(500); // Uses public function from delay.h
}
6️⃣ #include <library.h> vs #include "library.h"
🔍 #include <library.h> — System or Standard Headers
-
Used for: Standard libraries or system headers (e.g.,
, ) -
Search path: The compiler looks in system directories predefined by your development environment
-
Example:
#include <stdio.h> // Brings in standard I/O functions like printf
This tells the compiler: “Look in your built-in include paths for this header.”
🛠️ #include "library.h" — User-Defined or Local Headers
-
Used for: Your own header files or project-specific libraries
-
Search path: The compiler first looks in the current directory (where the source file is), then in system directories
-
Example:
#include "gpio.h" // Brings in your custom GPIO library
This tells the compiler: “Start by looking in the same folder as this source file. If not found, check system paths.”
🧪 Why It Matters
| Type | Location | Visibility | Purpose |
|---|---|---|---|
| Local | Inside .c file |
Private to that file | Used for internal helper functions |
| Public | Inside .h file |
Visible to other files | Used to expose functionality to other modules |