C Programming • Arrays & Functions
C Programming / Passing arrays to functions

Passing arrays to functions

Notes 3 Arrays & Functions

Definition: Passing an array to a function means providing the array to a function so that the function can access and process its elements.

Notes

Passing Arrays to Functions

Definition: Passing an array to a function means providing the array to a function so that the function can access and process its elements.

💡 Example: An array containing student marks can be passed to a function that calculates their total.

Why Pass an Array to a Function?

Arrays are often passed to functions when the same collection of values needs to be processed by a separate function.

💡 Example: One function can read the marks while another function calculates the sum of those marks.

Passing a One-Dimensional Array

Definition: A one-dimensional array is passed to a function by providing the array name in the function call. The function parameter is declared as an array or an equivalent pointer parameter.

💡 Example: display(arr, 5); passes the array arr along with its size to the function.

Basic Syntax

function_name(array_name, size);
💡 Example: sumArray(numbers, 5); passes the array numbers and its size 5 to the function.

Array Parameter

A function can declare an array parameter to receive the array. The size is commonly supplied separately because the function does not automatically know the number of elements in the array.

💡 Example: int sumArray(int arr[], int n) accepts an integer array and its number of elements.
int sumArray(int arr[], int n)
{
    int sum = 0;

    for (int i = 0; i < n; i++)
        sum += arr[i];

    return sum;
}

Passing Array Size

Definition: When an array is passed to a function, the number of elements is usually passed separately so that the function knows how many elements it should process.

💡 Example: sumArray(arr, 5); tells the function that it should process 5 elements.

Accessing Elements Inside the Function

The function can access array elements using their indexes just as they are accessed in the calling function.

💡 Example: arr[i] accesses the element at index i.

Important Point: Array Changes

Definition: In a normal function parameter, an array argument provides access to the same underlying array elements, so changes made to elements inside the function are visible to the calling function.

💡 Example: If a function executes arr[0] = 100;, the first element of the original array is changed to 100.

Simple Example

#include <stdio.h>

void changeFirst(int arr[])
{
    arr[0] = 100;          // modify the first array element
}

int main()
{
    int numbers[3] = {10, 20, 30};

    changeFirst(numbers);  // pass the array

    printf("%d
", numbers[0]);

    return 0;
}

Output

100
📌 Remember: When an array is passed to a function in C, the function can access the original array elements through the parameter.

Passing Array with Size

A common function design is to pass both the array and the number of elements.

💡 Example: display(arr, 5); tells the function exactly how many elements to display.

Practical Example — Find Sum of Array Elements Using a Function

Problem Statement

Write a C program to accept 5 integers in an array and pass the array to a function that calculates and returns the sum.

Learning Outcomes

  • Pass a one-dimensional array to a function.
  • Pass the size of the array as a separate parameter.
  • Process array elements inside a function.
  • Return the calculated result to the calling function.

Hint

Create a function sumArray() that accepts the array and its size. Use a loop inside the function to calculate the sum.

Theory

A one-dimensional array can be passed to a function by writing its name in the function call. The function can then access the array elements using indexes. The array size is usually passed separately.

Program

#include <stdio.h>

int sumArray(int arr[], int n);       // function declaration

int main()
{
    int arr[5];
    int i, sum;

    printf("Enter 5 numbers: ");

    // read array elements
    for (i = 0; i < 5; i++)
        scanf("%d", &arr[i]);

    sum = sumArray(arr, 5);            // pass array and its size

    printf("Sum = %d
", sum);

    return 0;
}

int sumArray(int arr[], int n)
{
    int sum = 0;

    // calculate the sum of all array elements
    for (int i = 0; i < n; i++)
        sum += arr[i];

    return sum;                         // return the calculated sum
}

Expected Output

Enter 5 numbers: 10 20 30 40 50
Sum = 150

Note

The array itself is passed using its name, while 5 is passed separately to specify the number of elements to process.

Passing an Array to a Function: Flow

Array created in main() ↓ Function call ↓ Pass array name + size ↓ Function accesses array elements ↓ Processing performed ↓ Result returned

Advantages of Passing Arrays to Functions

  • Large arrays can be processed by separate functions.
  • Programs become more modular and easier to understand.
  • The same function can be reused for different arrays.
  • Array elements can be processed using loops inside the function.

Passing Array vs Passing Single Variable

Single Variable Array
Passes one value. Provides access to multiple elements.
Example: square(x) Example: sumArray(arr, 5)
Usually processed as one value. Usually processed using a loop.

Important Points for Exam

  • An array can be passed to a function using its name.
  • The array size is usually passed separately.
  • The function can access elements using indexes.
  • Changes made to array elements inside the function can affect the original array.
  • Arrays are commonly processed inside functions using loops.
  • Passing arrays to functions improves modularity and code reuse.
🎯 Easy Rule:

Pass Array Name + Size → Function Processes Elements

Quick Revision

Concept Remember
Array parameter int arr[]
Function call function(arr, n)
Array size Usually passed separately.
Element access arr[i]
Modification Changes to elements can affect the original array.

Important Exam Questions

Short Answer Questions

  1. How is a one-dimensional array passed to a function?
  2. Why is the array size commonly passed separately?
  3. How are array elements accessed inside a function?
  4. Can a function modify an array passed to it?
  5. Write the syntax for passing an array to a function.

Long Answer Questions

  1. Explain how one-dimensional arrays are passed to functions with a suitable example.
  2. Write a C program to pass an array to a function and calculate the sum of its elements.
  3. Explain how a function can modify the elements of an array passed to it.
🎥 Recommended Learning

Watch a beginner-friendly explanation of passing arrays to functions in C.

▶ Watch: Passing Arrays to Functions — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for passing arrays to functions will be provided here.

🧠 Mind Map

Use the mind map for quick revision of array parameters, array size, indexing and function processing.