Unit 17A: Passing Arrays to Functions | Introduction to C Programming — BCA First Year
Unit 17A: Passing Arrays to Functions
Introduction to C Programming | BCA First Year
Topics: 1D Arrays • 2D Arrays • Pass by Reference • Common Patterns
1. Why Pass Arrays to Functions?
In C, functions work on data. When we have a large collection of data stored in an array, we need
to pass that array to a function so the function can process it — for example, to find a sum, sort
values, or print elements.
Instead of passing each element one by one, we can pass the entire array to a function in a single
step.
📝 Exam Tip: Understanding how arrays are passed to functions — especially that C
passes the ADDRESS of the array, not a copy — is one of the most important and
most tested concepts in this chapter.
2. How Arrays are Passed — The Key Concept
In C, when you pass an array to a function, you are NOT passing a copy of the array. Instead, you
are passing the ADDRESS (memory location) of the first element. This is called passing by
reference.
This means:
• The function receives a pointer to the array's first element.
• Any changes made to the array inside the function WILL affect the original array.
• This is different from normal variables, which are passed by value (a copy is made).
Feature Normal Variable Array
What is passed? A copy of the value Address of first element
Changes affect No Yes
original?
Mechanism Pass by Value Pass by Reference (automatic)
📝 Exam Tip: This is a very common exam question: 'How are arrays passed to
functions in C?' Answer: By passing the address of the first element (call by
reference). Changes inside the function affect the original array.
BCA — Introduction to C Programming | Page 1
Unit 17A: Passing Arrays to Functions | Introduction to C Programming — BCA First Year
3. Passing a 1D (One-Dimensional) Array
3.1 Syntax
Function Declaration (Prototype):
return_type function_name(data_type array_name[], int size);
// OR equivalently:
return_type function_name(data_type *array_name, int size);
Function Call:
function_name(array_name, size); // just write the array name, no []
Function Definition:
return_type function_name(data_type array_name[], int size) {
// use array_name[0], array_name[1], etc.
}
💡 Note: When calling, write only the array name without []. The array name itself represents the
address of the first element.
3.2 Example 1 — Print Array Elements
#include <stdio.h>
void printArray(int arr[], int n) {
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int nums[] = {10, 20, 30, 40, 50};
printArray(nums, 5); // pass array name only
return 0;
}
Output:
10 20 30 40 50
3.3 Example 2 — Find Sum of Array Elements
#include <stdio.h>
int findSum(int arr[], int n) {
int i, sum = 0;
for (i = 0; i < n; i++)
sum += arr[i];
return sum;
BCA — Introduction to C Programming | Page 2
Unit 17A: Passing Arrays to Functions | Introduction to C Programming — BCA First Year
int main() {
int a[] = {5, 10, 15, 20};
int result = findSum(a, 4);
printf("Sum = %d\n", result);
return 0;
}
Output:
Sum = 50
3.4 Example 3 — Modify Array Inside Function (shows pass by reference)
#include <stdio.h>
void doubleAll(int arr[], int n) {
int i;
for (i = 0; i < n; i++)
arr[i] = arr[i] * 2; // modifies original array!
}
int main() {
int a[] = {1, 2, 3, 4, 5};
doubleAll(a, 5);
int i;
for (i = 0; i < 5; i++)
printf("%d ", a[i]); // prints modified values
return 0;
}
Output:
2 4 6 8 10
💡 Note: The original array 'a' in main() was changed because the function received the address
of the array, not a copy.
3.5 Example 4 — Find Largest Element
#include <stdio.h>
int findMax(int arr[], int n) {
int i, max = arr[0];
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
BCA — Introduction to C Programming | Page 3
Unit 17A: Passing Arrays to Functions | Introduction to C Programming — BCA First Year
int main() {
int a[] = {3, 7, 1, 9, 4};
printf("Max = %d\n", findMax(a, 5));
return 0;
}
Output:
Max = 9
4. Passing a 2D (Two-Dimensional) Array
When passing a 2D array, you must specify the number of columns in the function parameter. The
number of rows can be left empty or passed as a separate parameter.
4.1 Syntax
void function_name(int arr[][COLS], int rows);
// COLS must be specified — rows can be omitted in []
📝 Exam Tip: For 2D arrays, the number of COLUMNS must always be specified in the
function parameter. Forgetting this is a very common mistake. The number of rows is
passed as a separate integer.
4.2 Example — Print a 2D Array
#include <stdio.h>
#define COLS 3
void print2D(int arr[][COLS], int rows) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < COLS; j++)
printf("%d\t", arr[i][j]);
printf("\n");
}
}
int main() {
int matrix[2][3] = {{1,2,3},{4,5,6}};
print2D(matrix, 2);
return 0;
}
Output:
1 2 3
4 5 6
BCA — Introduction to C Programming | Page 4
Unit 17A: Passing Arrays to Functions | Introduction to C Programming — BCA First Year
4.3 Example — Find Sum of 2D Array
#include <stdio.h>
#define COLS 3
int sumMatrix(int arr[][COLS], int rows) {
int i, j, sum = 0;
for (i = 0; i < rows; i++)
for (j = 0; j < COLS; j++)
sum += arr[i][j];
return sum;
}
int main() {
int m[2][3] = {{1,2,3},{4,5,6}};
printf("Sum = %d\n", sumMatrix(m, 2));
return 0;
}
Output:
Sum = 21
5. Key Differences — 1D vs 2D Array Passing
Feature 1D Array 2D Array
Parameter syntax int arr[], int n int arr[][COLS], int rows
Columns needed? No Yes — MUST specify
Accessing elements arr[i] arr[i][j]
Example call func(a, 5); func(matrix, 3);
6. Common Exam Questions & Model Answers
Q1. How are arrays passed to functions in C? Explain with an example.
In C, arrays are passed to functions by passing the address of the first element. This is called
passing by reference. The function receives a pointer to the array, so any changes made inside the
function directly affect the original array in the calling function.
#include <stdio.h>
void addOne(int arr[], int n) {
int i;
for (i = 0; i < n; i++)
arr[i]++;
}
BCA — Introduction to C Programming | Page 5
Unit 17A: Passing Arrays to Functions | Introduction to C Programming — BCA First Year
int main() {
int a[] = {1, 2, 3};
addOne(a, 3);
printf("%d %d %d", a[0], a[1], a[2]); // 2 3 4
return 0;
}
Q2. What is the difference between passing an array and passing a normal
variable?
Aspect Normal Variable Array
What is sent? Copy of the value Address of first element
Changes affect No Yes
original?
Type of passing Call by value Call by reference
Example func(x); func(arr, n);
Q3. Write a function to find the minimum element in an array.
#include <stdio.h>
int findMin(int arr[], int n) {
int i, min = arr[0];
for (i = 1; i < n; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
int main() {
int a[] = {8, 3, 5, 1, 9};
printf("Min = %d", findMin(a, 5)); // Min = 1
return 0;
}
Q4. Why must column size be specified when passing a 2D array?
The column size must be specified because C needs to know how many elements are in each row
to correctly calculate the memory address of each element. The formula used internally is: address
= base_address + (i * COLS + j) * element_size. Without knowing COLS, C cannot calculate where
arr[i][j] is stored in memory.
⭐ KEY RULES TO NEVER FORGET
• Arrays are passed by reference — the address of the first element is sent.
BCA — Introduction to C Programming | Page 6
Unit 17A: Passing Arrays to Functions | Introduction to C Programming — BCA First Year
• Changes inside the function WILL change the original array.
• Function call: write array name only — no [] brackets: func(arr, n);
• For 2D arrays: column count MUST be specified in the parameter.
• Size is always passed as a separate int parameter (e.g., int n).
Master these rules and you can answer any exam question on this topic!
BCA — Introduction to C Programming | Page 7