Definition: Passing an array to a function means providing the array to a function so that the function can access and process its elements.
Definition: Passing an array to a function means providing the array to a function so that the function can access and process its elements.
Arrays are often passed to functions when the same collection of values needs to be processed by a separate function.
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.
display(arr, 5); passes the array arr
along with its size to the function.
function_name(array_name, size);
sumArray(numbers, 5);
passes the array numbers and its size
5 to the function.
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.
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;
}
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.
sumArray(arr, 5);
tells the function that it should process 5 elements.
The function can access array elements using their indexes just as they are accessed in the calling function.
arr[i] accesses the element at index i.
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.
arr[0] = 100;, the first element
of the original array is changed to 100.
#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;
}
100
A common function design is to pass both the array and the number of elements.
display(arr, 5);
tells the function exactly how many elements to display.
Write a C program to accept 5 integers in an array and pass the array to a function that calculates and returns the sum.
Create a function sumArray() that accepts the array and
its size. Use a loop inside the function to calculate the sum.
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.
#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
}
Enter 5 numbers: 10 20 30 40 50 Sum = 150
The array itself is passed using its name, while 5
is passed separately to specify the number of elements to process.
| 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. |
| 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. |
Watch a beginner-friendly explanation of passing arrays to functions in C.
A short handwritten-style revision sheet for passing arrays to functions will be provided here.
Use the mind map for quick revision of array parameters, array size, indexing and function processing.