Pass arrays to a function

Pass arrays to a function in C Programming Language

With examples, you will learn in this tutorial about Pass arrays to a function in C —both one-dimensional and multidimensional—to a function in C.

You can supply a full array of functions in C programming. Let’s explore how you can pass certain array elements to functions before we understand that.

Pass Individual Array Elements(Pass arrays to a function in C)

The process of passing variables to a function is similar to the operation of passing array members.

Example 1: Pass Individual Array Elements(Pass arrays to a function in C)

#include <stdio.h>
void display(int age1, int age2) {
  printf("%d\n", age1);
  printf("%d\n", age2);
}

int main() {
  int ageArray[] = {2, 8, 4, 12};

  // pass second and third elements to display()
  display(ageArray[1], ageArray[2]); 
  return 0;
}

Output

8
4

The display() function has been given an array of parameters in this case, just like we would provide a function as a variable.

// pass second and third elements to display()
display(ageArray[1], ageArray[2]);

This is seen in the function specification, where the parameters are listed as separate variables:

void display(int age1, int age2) {
  // code
}

Example 2: Pass Arrays to Functions(Pass arrays to a function in C)

// Program to calculate the sum of array elements by passing to a function 

#include <stdio.h>
float calculateSum(float num[]);

int main() {
  float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};

  // num array is passed to calculateSum()
  result = calculateSum(num); 
  printf("Result = %.2f", result);
  return 0;
}

float calculateSum(float num[]) {
  float sum = 0.0;

  for (int i = 0; i < 6; ++i) {
    sum += num[i];
  }

  return sum;
}

output

Result = 162.50

Only the array name is supplied as a parameter to pass a full array to a function.

result = calculateSum(num);

But take note of how [] is used in the function specification.

float calculateSum(float num[]) {
... ..
}

This tells the compiler that you are handing the function a one-dimensional array.

Pass Multidimensional Arrays to a Function

Only the array name is supplied to a function when passing multidimensional arrays to it (similar to one-dimensional arrays).

Example 3: Pass two-dimensional arrays

#include <stdio.h>
void displayNumbers(int num[2][2]);

int main() {
  int num[2][2];
  printf("Enter 4 numbers:\n");
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j) {
      scanf("%d", &num[i][j]);
    }
  }

  // pass multi-dimensional array to a function
  displayNumbers(num);

  return 0;
}

void displayNumbers(int num[2][2]) {
  printf("Displaying:\n");
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j) {
      printf("%d\n", num[i][j]);
    }
  }
}

output

Enter 4 numbers:
2
3
4
5
Displaying:
2
3
4
5

In both the function prototype and function definition, you’ll see the parameter int num[2][2]:

// function prototype
void displayNumbers(int num[2][2]);

This denotes that a two-dimensional array is accepted as an argument by the function. As a function argument, we can also pass arrays with more than two dimensions.

The number of rows in an array need not be specified when sending a two-dimensional array. The number of columns should, however, always be stated.

For instance,

void displayNumbers(int num[][2]) {
  // code
}

You may like:

JavaScript Variable Scope with Example

Leave a Reply