A function is a self-contained block of code that performs a specific task. Using functions makes code modular, reusable, and easier to debug.
// Function Definition
float calculateAverage(int a, int b) {
float avg = (a + b) / 2.0;
return avg;
}
int main() {
int x = 10, y = 5;
// Function Access (Call)
float result = calculateAverage(x, y);
printf("Average: %.2f\n", result);
return 0;
}In C, arguments are passed by Pass-by-Value by default. This means the function receives a copy of the argument's value, and the original variable in the calling function is not changed. (We will see how to change this with pointers in Unit 3).
A function prototype is a declaration of a function that tells the compiler about its name, return type, and parameters. It is crucial if you define the function after main().
#include <stdio.h>
// Function Prototype
float calculateAverage(int a, int b);
int main() {
float result = calculateAverage(10, 5); // Compiler already knows what this is
printf("Result: %.2f\n", result);
return 0;
}
// Function Definition
float calculateAverage(int a, int b) {
return (a + b) / 2.0;
}Functions can return any data type, such as float, double, char, or even pointers (Unit 3) and structures (Unit 4). The calculateAverage function above is an example of a function returning a float.
Storage classes determine a variable's scope, lifetime, and storage location.
| Storage Class | Keyword | Storage | Default Value | Scope | Lifetime |
|---|---|---|---|---|---|
| Automatic | auto (rarely used) |
Stack | Garbage | Block (local) | Within the block |
| External | extern |
Data Segment | Zero | File (global) | Entire program |
| Static | static |
Data Segment | Zero | Block or File | Entire program |
| Register | register |
CPU Register (hint) | Garbage | Block (local) | Within the block |
{ ... } is only accessible within that block.Files (e.g., stdio.h, math.h) containing function prototypes and definitions that can be included in our program using #include.
Recursion is a process where a function calls itself. A recursive function must have two parts:
long factorial(int n) {
// 1. Base Case
if (n == 0 || n == 1) {
return 1;
}
// 2. Recursive Step
else {
return n * factorial(n - 1);
}
}The preprocessor is a program that processes your source code before it is passed to the compiler. Its directives start with #.
#include: Pastes the content of a header file into your code.#define: Creates a macro, which is a fragment of code that is given a name.
#define PI 3.14159#define SQUARE(x) (x * x)#define SQUARE(x) ((x) * (x)).