In this tutorial, we are going to learn about how to pass an array (1-D or 2D) to a function.

Understanding Functions with 1-D arrays:

Arrays can also be passed as arguments to the functions like normal variables.

The main difference between passing the normal variable and an array is that the copies of the values of the normal variables are passed from the calling function to called function, but whereas the passing parameter is an array, then the base address of that entire array has to be passed.

In the case of an array, elements are not copied, but the function works with a different array name on the original array itself. Hence, any modifications done to the array within the function, are reflected in the array of the caller too.

Let’s consider an example to understand how an array work within function calls.

#include<stdio.h>
void read(int[], int);
void display(int[], int);
void main() {
  int a[4], n;
  printf("Enter number of elements : ");
  scanf("%d", &n);
  read(a, n);
  display(a, n);
}
void read(int x[4], int n) {
  int i;
  printf("Enter %d elements : \n", n);
  for(i = 0; i < n; i++) {
    printf("Enter value for x[%d] : ", i);
    scanf("%d", &x[i]);
  }
}
void display(int y[4], int n) {
  int i;
  printf("The elements in array are : \n");
  for(i = 0; i < n; i++) {
    printf("y[%d] = %d\n", i, y[i]);
  }
}

Output:

Enter number of elements : 3
Enter 3 elements : 
Enter value for x[0] : 9
Enter value for x[1] : 18
Enter value for x[2] : 27
The elements in array are : 
y[0] = 9
y[1] = 18
y[2] = 27

In the below example a[4] is an array declared with in the main() function and the function calls read(a, n) and display(a, n) are made in the main() function.

In the function call read(a, n), a is the array name that represents the base address of the entire array a[4], and n is the local variable in the main() function.

The function definition of read() receives the base address of the array a from main(), i.e., placed an alias name for the array a in the read(), so any modifications made to the array in the read() function will also be reflected the array in the main() function.

The function definition of read() also receives the copy of the value of n, i.e., it receives only value, so any modifications made to the value does not reflect the value within the main() function. Whenever display() is called, it displays the values present in the array.

Understanding Functions with 2-D arrays:

Like one-dimensional arrays, a two-dimensional array can also be passed as an argument to the functions. Either one-dimension or two-dimension array is passed as an argument, it will send the base address of the entire array.

In the case of arrays, elements are not copied, but the function works with a different array name on the original array itself. Hence, any modifications done to the array within the function, are reflected in the array of the caller too.

Let’s consider an example to understand how an array work within function calls.

In the below sample code a[5][5] is an array declared within the main() function and the function calls read(a, m, n) and display(a, m, n) are made in the main() function.

In the function call read(a, m, n), a is the array name which represents the base address of the entire array a[5][5] and m, n are the local variables in the main() function representing the row and column sizes.

#include 
void read(int [5][5], int, int);
void display(int [5][5], int, int);
void main() {
     int a[5][5], m, n;
     printf("Enter the row and column sizes of matrix: ");
     scanf("%d%d", &m, &n);
     read(a, m, n);
     display(a, m, n);
}
void read(int x[5][5], int m, int n) {
     int i,j;
     printf("Enter %d elements : ", m*n);
     for (i = 0; i < m; i++) {
         for(j = 0; j < n; j++) {
             scanf("%d", &x[i][j]);
         }
     }
}
void display(int y[5][5], int m, int n) {
     int i, j;
     printf("Elements in the matrix are \n");
     for (i = 0; i < m; i++) {
         for(j = 0; j < n; j++) {
             printf("%d ", y[i][j]);
          }
          printf("\n");
      }
}
}

Output:

Enter the row and column sizes of matrix: 2                                                                                                     
2                                                                                                                                               
Enter 4 elements : 1                                                                                                                            
2                                                                                                                                               
3                                                                                                                                               
4                                                                                                                                               
Elements in the matrix are                                                                                                                      
1 2                                                                                                                                             
3 4

 

The function definition of read() receives the base address of the array a from main(), i.e., placed an alias name for the array a in the read(), so any modifications made to the array in the read() function will also be reflected the array in the main() function.

The function definition of read() also receives the copy of the values of m and n, i.e., it receives only values, so any modifications made to the value does not reflect the value within the main() function.

In the function declaration, the representation of the row and column sizes of a matrix are mandatory. Whenever display() is called, it displays the values present in the matrix.

REFERENCES:

Happy Learning 🙂