BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

Passing Arrays to Functions in C: 1D & 2D Array UDF | Unit 4 | BKNMU BCA

by BCA School •

BKNMU Junagadh  |  BCA Sem 1  |  Unit 4



Passing Arrays to User-Defined Functions (UDF) in C

Complete Guide to Passing Individual Elements, Passing Entire Arrays by Address, 2D Matrix Parameters, and Mutability Rules

BKNMU Junagadh BCA Sem 1 Unit 4 Array with Functions Call by Reference Base Address Pointer
In simple words: In C programming, arrays and functions frequently work together to modularize complex algorithms like searching, sorting, and matrix transformations. You can supply array data to a User-Defined Function (UDF) in two ways: pass an individual array element (which follows standard Call by Value), or pass an entire array by sending its base address (which inherently functions as Call by Reference). This tutorial covers all calling conventions for BKNMU BCA Semester 1 (Unit 4).
📖 Methods of Passing Arrays to Functions

The parameter passing styles between arrays and UDFs are categorized into four structural mechanics:

Single Element

1. Element-by-Value

Passes a single element like display(arr[2]). It acts as an independent primitive copy; changes inside the UDF do not affect the original array slot.

Entire 1D Array

2. Base Address Passing

Passes the unindexed array name sort(arr, n). The UDF receives the base pointer; any modification alters the original array permanently.

Size Argument

3. Explicit Size Passing

C arrays do not convey their size across function boundaries. The length n must be passed as a companion argument to control loop traversal safely.

2D Array / Matrix

4. Tabular Parameters

When passing matrices matrixFunc(mat), the formal parameter requires an explicit column bound: void f(int a[][COLS]).

1️⃣ Master Table: Function Prototypes & Invocation Styles

A complete architectural reference of syntax signatures, calling forms, and underlying memory mechanisms:

Passing Method Prototype & Calling Syntax Parameter Passing Mechanism
Individual Element void check(int x);
Call: check(marks[i]);
Call by Value: Copy of the element is pushed to the stack. Original element remains safe and unmodified.
Entire 1D Array (Sized) void modify(int arr[], int n);
Call: modify(marks, 5);
Simulated Call by Reference: Passing marks passes &marks[0]. Array slots are directly mutable.
1D Array (Pointer Notation) void modify(int *arr, int n);
Call: modify(marks, 5);
Pointer Reference: Functionally identical to array notation; explicit pointer receives the base address.
Entire 2D Array (Matrix) void show(int m[][3], int r);
Call: show(matrix, 3);
Row-Major Pointer: Column size is mandatory so the compiler can compute the row-jump offset correctly.
ℹ️ Why Passing Entire Arrays Behaves as Call by Reference:
  • In C, copying thousands of contiguous memory blocks into a function stack frame would exhaust RAM and cause serious performance overhead.
  • Instead, C automatically converts the array name in an argument list into a pointer to its first element (&arr[0]). Thus, the UDF operates directly on the original memory block without generating a duplicate array.
💻 Practical Code Examples

1. Passing an Entire 1D Array to a Function to Double Its Elements:

#include <stdio.h>

// Prototype: arr[] receives base address, size holds element count
void doubleArray(int arr[], int size);

int main() {
    int numbers[4] = {5, 10, 15, 20};
    int i;

    printf("Original array values:\n");
    for (i = 0; i < 4; i++) {
        printf("%d ", numbers[i]);
    }

    // Pass the entire array by name (base address) and its size
    doubleArray(numbers, 4);

    printf("\nAfter doubleArray() call (Values MUTATED):\n");
    for (i = 0; i < 4; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\n");

    return 0;
}

void doubleArray(int arr[], int size) {
    int i;
    for (i = 0; i < size; i++) {
        arr[i] = arr[i] * 2; // Directly mutates original memory in caller
    }
}

2. Passing a 2D Array (Matrix) to a Function:

#include <stdio.h>

// Column size (3) MUST be specified in formal parameter
void displayMatrix(int mat[][3], int rows) {
    int r, c;
    for (r = 0; r < rows; r++) {
        for (c = 0; c < 3; c++) {
            printf("%d\t", mat[r][c]);
        }
        printf("\n");
    }
}

int main() {
    int grid[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    printf("Matrix printed via User-Defined Function:\n");
    displayMatrix(grid, 2);

    return 0;
}
🔄 Step-by-Step Flow: Parameter Resolution Lifecycle
1

Base Address Decay

In main(), writing the call doubleArray(numbers, 4) evaluates numbers as the base pointer &numbers[0].

2

Pointer Mapping to Formal Parameter

The formal parameter arr[] receives this memory address. No secondary array copy is constructed on the function's stack frame.

3

Direct Dereference & Mutation

Operations like arr[i] = val calculate memory offset *(arr + i), directly modifying the variable cells inside main().

💡 Protecting Array Data with const: If a function should only read elements (like searching or calculating an average) without altering them, declare the formal parameter with const: void printValues(const int arr[], int n);. Any attempt to modify elements inside the function will trigger a compile-time error.
⚠️ Common Exam Error: Square Brackets in the Function Call: Never include square brackets when passing an entire array in the function call. Writing doubleArray(numbers[], 4); or doubleArray(numbers[4], 4); is invalid syntax. Pass only the naked array identifier: doubleArray(numbers, 4);.
📝 Quick Revision — Key Exam Points
  • Individual Element: Passed by value; changes inside function do not affect original array.
  • Entire Array: Passed by reference (base address); alterations modify original array data.
  • Calling Syntax: Use only the array name without brackets (e.g. myFunc(arr, size);).
  • Formal Parameter: Can use empty brackets (int arr[]) or pointer syntax (int *arr).
  • 2D Arrays: The second dimension (column bound) is strictly mandatory in the formal parameter.