Functions & Arrays
Expert Answer & Key Takeaways
Comprehensive guide to creating and using Functions (including recursion and scope) and Arrays (1D and 2D) in C programming.
Functions & Arrays in C
As programs grow larger, placing all code inside the main() function becomes unmanageable. To solve this, C allows us to break code into smaller, reusable blocks called Functions, and group related data items together using Arrays.
1. Functions in C
A function is a self-contained block of code that performs a specific task. Every C program has at least one function:
main().1.1 Advantages of Functions
- Reusability: Code can be written once and called multiple times.
- Modularity: Large programs can be broken down into smaller, manageable chunks.
- Readability: Makes the code cleaner and easier to understand and debug.
1.2 Types of Functions
- Standard Library Functions: Built-in functions provided by C (e.g.,
printf(),scanf(),sqrt()). - User-Defined Functions: Custom functions created by the programmer.
1.3 Elements of a User-Defined Function
A function generally involves three aspects:
- Function Declaration (Prototype): Tells the compiler about the function's name, return type, and parameters. Placed before
main().- Syntax:
returnType functionName(parameterList);
- Syntax:
- Function Definition: The actual body containing the block of code to execute.
- Function Call: Executing the function by calling its name inside another function (like
main).
// 1. Declaration
int addNumbers(int a, int b);
int main() {
int sum = addNumbers(5, 10); // 3. Call
printf("Sum: %d", sum);
return 0;
}
// 2. Definition
int addNumbers(int a, int b) {
return a + b;
}1.4 Call by Value vs Call by Reference
- Call by Value: A copy of the actual argument is passed to the function. Changes made inside the function do NOT affect the original variable.
- Call by Reference: The memory address (pointer) of the argument is passed. Changes made inside the function DO affect the original variable.
1.5 Recursion
A function that calls itself is known as a recursive function. It must have a Base Case to stop the recursion, otherwise it creates an infinite loop (Stack Overflow).
int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}2. Scope and Lifetime of Variables
Scope defines where a variable can be accessed.
- Local Variables: Declared inside a block/function. Cannot be accessed outside it. Destroyed when the block exits.
- Global Variables: Declared outside all functions. Can be accessed from anywhere in the program.
- Static Variables: A local variable that retains its value between multiple function calls. It is initialized only once.
3. Arrays in C
An array is a collection of data items of the same data type stored in contiguous (continuous) memory locations. It is a derived data type.
3.1 One-Dimensional (1D) Arrays
Declaration:
dataType arrayName[arraySize];int marks[5] = {90, 85, 78, 92, 88};- Index (Subscript): Arrays in C are zero-indexed. The first element is at index
0, and the last is at indexsize - 1. - Accessing Elements:
marks[0]gives90.marks[4]gives88. - Memory Calculation: Total bytes =
size * sizeof(dataType). E.g.,int arr[5]takes5 * 4 = 20bytes.
3.2 Two-Dimensional (2D) Arrays
Used to represent matrices or grids (rows and columns).
Declaration:
dataType arrayName[rows][columns];int matrix[2][3] = {
{1, 2, 3}, // Row 0
{4, 5, 6} // Row 1
};- Accessing the element '6': It is at row 1, column 2 ->
matrix[1][2].
3.3 Passing Arrays to Functions
When an array is passed to a function, the base address (pointer to the first element) is passed, effectively passing it by reference. Any changes made to the array inside the function will modify the original array.
4. Strings (Character Arrays)
C does not have a native
string data type. A string is simply a 1D array of characters terminated by a special null character \0.char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
// Or simpler:
char greeting[] = "Hello"; // The compiler automatically adds '\0'- The
<string.h>library provides functions likestrlen()(length),strcpy()(copy), andstrcmp()(compare).
Course4All Editorial Board
Verified ExpertSubject Matter Experts
Comprising experienced educators and curriculum specialists dedicated to providing accurate, exam-aligned preparation material.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.