Unit II: Functions and Program Structure

Table of Contents

1. Defining and Accessing Functions

Functions are the basic building blocks of a C program, allowing for modularity and code reuse. A function definition specifies the task the function performs.


2. Function Prototypes and Returns

Functions must be declared before they are used so the compiler knows how to call them.


3. Storage Classes and Scope Rules

Storage classes define the scope (visibility) and lifetime of variables within a C program.

Storage Class Keyword Lifetime Scope
Automatic auto Function execution time. Local to the block.
External extern Program execution time. Global (across files).
Static static Program execution time. Local to function but retains value.
Register register Function execution time. Local (stored in CPU register).

4. Block Structure and Header Files


5. Recursion in C

Recursion is a technique where a function calls itself to solve smaller instances of the same problem.

Recursion is essential for solving problems that can be naturally divided into sub-problems, such as calculating factorials or traversing tree structures.

6. The C Preprocessor

The C preprocessor is a program that processes the source code before it is passed to the compiler.

Exam Tips

Common Mistakes