Unit II: Functions and Program Structure
Course: Programming with C
Code: CADSM101
Defining and Accessing Functions
A function is a self-contained block of code that performs a specific task. Using functions helps in modularizing code and promoting reusability.
- Definition: Includes the function name, return type, and the body of the function.
- Accessing: Functions are "called" from other parts of the program, such as
main(), to execute their tasks.
Passing Arguments and Data Types
Functions can receive data through arguments. In C, you must specify the data type for each argument passed to a function.
- Actual Arguments: The values passed to the function during a call.
- Formal Arguments: The variables declared in the function definition to receive those values.
Function Prototypes and Return Types
A Function Prototype tells the compiler about the function's name, return type, and parameters before the actual definition is provided.
- Non-integer returns: Functions can return types other than
int, such asfloat,double, orchar.
Storage Classes
Storage classes define the scope, visibility, and lifetime of variables within a C program.
| Storage Class | Keyword | Location | |
|---|---|---|---|
| Automatic | auto | Stack (Memory) | Local to the block. |
| External | extern | Data Segment | Global; persists for the entire program. |
| Static | static | Data Segment | Value persists between function calls. |
| Register | register | CPU Register | Fast access; local to the block. |
Scope Rules and Block Structure
The scope refers to the region of the program where a variable is accessible.
- Local Scope: Variables declared inside a block or function are only accessible within that block.
- Global Scope: Variables declared outside all functions are accessible everywhere.
- Block Structure: C supports nested blocks using curly braces
{}, where inner blocks can access outer variables (unless shadowed).
Recursion in C
Recursion is a technique where a function calls itself to solve smaller instances of the same problem.
Example: Factorial of n is n * Factorial(n-1).
- Must have a Base Case to prevent infinite loops.
- Useful for problems like Tower of Hanoi and Fibonacci series.
The C Preprocessor and Header Files
The preprocessor processes the source code before it is passed to the compiler.
- Header Files: Files ending in
.h(likestdio.h) containing function declarations and macros. - Macros: Used for defining constants or small code snippets using
#define.
Exam Focus & Tips
- Exam Tip: Expect questions on Static variables. Remember: they retain their value even after the function finishes execution.
- Common Mistake: Forgetting the base case in a recursive function, which leads to a "Stack Overflow" error.
- Programming Task: Practice writing a recursive function for the Fibonacci sequence.
Frequently Asked Questions
Q: What is a function prototype?
A: It is a declaration of the function that informs the compiler about the return type and arguments before the function is called.
Q: What is the difference between auto and static variables?
A: auto variables are destroyed when the function ends, while static variables keep their value for the next call.