Passing an Array Through a Function

 


Hello Tech Explorers 👋
Welcome back to another deep dive into programming fundamentals. Today we’re going to explore a topic that often confuses beginners: how do you pass an array to a function? We’ll look at the “why”, the “how”, and some useful patterns (and pitfalls) — in the classic C language (which is still highly relevant for the understanding of arrays), and then mention how things differ in more modern languages.

Why passing an array is different

When you call a function and pass a simple variable (like int x), you’re passing a value (in many languages) or a reference/pointer (in some languages) depending on the semantics. But an array adds extra nuance:

  • An array is a contiguous collection of similar elements, so we might want the function to operate on all of them.
  • In C (for example), arrays decay to pointers when passed to functions, so what you pass is essentially a pointer to the first element — meaning the function can modify the original array.
  • You often need to tell the function how many elements are in the array (or some sentinel) so it knows how far to iterate.

Understanding this properly helps you avoid bugs like buffer overflows, unwanted side-effects, or wasteful copying of large arrays.

Passing an array in C

Let’s look at a typical example in C.

#include <stdio.h>
void printArray(int arr[], int size) {
printf("Array contents: ");
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main(void) {
int numbers[] = { 2, 4, 6, 8, 10 };
int n = sizeof(numbers) / sizeof(numbers[0]);
printArray(numbers, n);
return 0;
}

Explanation:

  • In main, we declare numbers[] with 5 elements.
  • We compute n as the number of elements (size of the entire array divided by size of one element).
  • We call printArray(numbers, n);.
  • In printArray, the parameter int arr[] effectively means “a pointer to int, pointing at the first element of an integer array”, and int size tells the function how many elements there are.
  • The loop iterates i from 0 to size-1, printing each arr[i].

Key points:

  • We didn’t pass the whole array by value (which would require copying all elements) — we passed a pointer/reference.
  • Because of this, the function can also modify the contents of the original array if it wanted to:

void doubleValues(int arr[], int size) {

    for(int i = 0; i < size; i++) {

        arr[i] *= 2;

    }

}

Then in main you could do:

doubleValues(numbers, n);

printArray(numbers, n);  // will now show 4,8,12,16,20

Passing a multidimensional array

If you work with 2D arrays (arrays of arrays) in C, passing to functions is slightly more elaborate:

#include <stdio.h>
void printMatrix(int matrix[][3], int rows) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main(void) {
int mat[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
printMatrix(mat, 2);
return 0;
}

Here:

  • matrix[][3] means each sub-array has 3 elements.
  • We pass the number of rows (2) so the function knows how many sub-arrays to iterate over.
  • The number of columns (3) must be known (compile-time constant) so pointer arithmetic works.

 

Best practices & tips

  • Always pass along the size (or upper bound) of the array when your function will iterate over it. Otherwise you risk reading out of bounds.
  • If the function should not modify the array, mark it accordingly (use const int arr[]).
  • If the array is large and you don’t want to copy the whole thing, use references/pointers (as we did) rather than copying element-by-element.
  • Consider whether you need one function to both traverse and modify the array — or separate read and write functions — for clarity.
  • When working with dynamic memory (malloc/new), ensure ownership (who frees) and lifetime are well defined.
  • For readability and portability: prefer using named sizes, #define MAX_SIZE 100, or dynamic arrays via malloc, rather than “magic numbers”.

Full working example combining both reading and modifying

#include <stdio.h>
void printArray(const int arr[], int size) {
printf("Array contents: ");
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void scaleArray(int arr[], int size, int factor) {
for(int i = 0; i < size; i++) {
arr[i] *= factor;
}
}
int main(void) {
int values[] = { 3, 6, 9, 12, 15 };
int count = sizeof(values) / sizeof(values[0]);
printArray(values, count);
scaleArray(values, count, 5);
printArray(values, count);
return 0;
}
What happens:
  • We print the original array: 3 6 9 12 15.
  • We scale each element by factor 5: array becomes 15 30 45 60 75.
  • We print again, showing the effect of passing the array and modifying it in the function.

Why this matters

Learning how to pass arrays into functions is more than syntax — it’s about managing data structures, memory usage, side-effects, and writing clean modular code. Once you master this, you’ll be more comfortable moving on to pointers, dynamic memory, data structures like linked lists, matrices, etc.

 








Post a Comment

Previous Post Next Post