Unit V: Arrays, Functions, and Pointers
1. One-dimensional Arrays
An Array is a fixed-size, sequenced collection of elements of the same data type.
- Declaration: To declare an array, you must specify the data type, the array name, and the size in square brackets.
- Initialization: Arrays can be initialized at the time of declaration by providing a list of values enclosed in braces.
2. User-defined Functions
User-defined functions are subprograms created by the programmer to perform specific tasks, promoting modularity and code reuse.
- Need for Functions: Functions avoid repetitive code, make debugging easier, and allow large programs to be broken into manageable parts.
- Elements: Every function involves three main elements: the Function Definition, the Function Call, and the Function Declaration (Prototype).
- Return Values: Functions can return a single value to the calling program, and the data type of this value must be specified.
3. Categories of Functions
Based on the presence of arguments and return values, functions are categorized into four types:
| Category | Description |
|---|---|
| No Arguments & No Return Values | The function simply performs a task without needing input or sending output back. |
| Arguments but No Return Values | Data is sent to the function, but results are printed or used within the function itself. |
| Arguments with Return Values | The most common type; it takes input data, processes it, and returns a result. |
| No Arguments but Returns a Value | Useful for functions that read data from a keyboard or file and return it to the caller. |
Passing Arrays to Functions: Entire arrays can be passed to functions as arguments to perform operations like searching or sorting.
4. Recursion
Recursion is a programming technique where a function calls itself directly or indirectly to solve a problem.
Essential Rule: A recursive function must have a termination condition (base case) to prevent an infinite loop.
5. Introduction to Pointers
Pointers are variables that store the memory address of another variable.
- Basic Concepts: Pointers allow direct manipulation of memory and are essential for dynamic memory allocation and efficient data handling.
- Operators: The address-of operator (&) and the indirection/dereference operator (*) are the primary tools used with pointers.
Exam Tips
- Array Bounds: C does not check if your index is within the array size. Accessing
arr[10]for an array of size 5 will lead to garbage values or crashes. - Prototypes: Always declare your functions (prototypes) before the
main()function if the definition is written aftermain(). - Pointer Initialization: Never use a pointer without initializing it to a valid address or NULL, as it can corrupt memory.
Frequently Asked Questions
Q1: What is the benefit of passing an array to a function?
It allows the function to work on large sets of data without copying the entire data set, as only the address of the first element is usually passed.
Q2: Why is recursion sometimes slower than iteration?
Recursion involves repeated function calls, which add overhead to the system stack, whereas iteration (loops) generally uses less memory and processing time.