Passing array to function

Array is a variable which can hold multiple values but of the same type(homogeneous).

The way a single value can be passed to a function, similarly, an entire array or even the single value of an array can be passed to the functions.

This section covers:

Passing array as a parameter to the function:

  • Passing a single array element to a function
  • Passing a complete one-dimensional array to a function.
  • Passing a complete two-dimensional array to a function.

Returning array from a function:

  • Returning a single array element to a function
  • Returning a complete one-dimensional array to a function.
  • Returning a complete two-dimensional array to a function.

Passing a single array element to a function:

A simple program where we declare and define an integer array and pass a single element to the function and print the same.

#include <stdio.h>

void fun (int num)
{
  printf ("\n The number is: %d\n", num);
}

int main ()
{
  int arr[5] = {1, 2, 3, 4, 5};
  fun (arr[2]);
  return 0;
}

Output:

The number is: 3

Passing a complete one-dimensional array to a function:

A program to pass a complete one-dimensional array and print all the elements in it.

#include <stdio.h>

void fun (int marks[])
{
  int i = 0;
  for (i = 0; i <5; i++)
    printf ("\n The number is: %d\n", marks[i]);
}

int main ()
{
  int arr[5] = {10, 20, 30, 40, 50};
  fun (arr);
  return 0;
}

Output:

The number is: 10

The number is: 20

The number is: 30

The number is: 40

The number is: 50

The above program can also be done with the help of pointers as we are passing the base address of an array. We will discuss more in the pointer section.

#include <stdio.h>

void fun (int *marks)
{
  int i = 0;
  for (i = 0; i <5; i++)
    printf ("\n The number is: %d\n", *(marks + i));
}

int main ()
{
  int arr[5] = {10, 20, 30, 40, 50};
  fun (arr);
  return 0;
}

Passing a complete two-dimensional array to a function:

A program to pass a complete two-dimensional array and print all the elements in it.

#include <stdio.h>

void fun (int marks[3][3])
{
  int row, col;
  printf ("\n The values are\n:");
  for (row = 0; row <3; row++)
  {
    for (col = 0; col <3; col++)
    { 
      printf("%d",marks[row][col]);
    }
    printf("\n");
  }
}

int main ()
{
  int arr[3][3];
  int row, col;
  printf ("\n Enter the values\n");
  for (row = 0; row <3; row++)
  {
    for (col = 0; col <3; col++)
    { 
      scanf("%d",&arr[row][col]);
    }
  }
  fun (arr);
  return 0;
}

Output:

Enter the values
0
1
2
3
4
5
6
7
8
The values are:

0 1 2 
3 4 5 
6 7 8 

Returning a single array element to a function:

A program that returns a single value of an array from the called function

#include <stdio.h>

int fun ()
{
  int arr[5] = {1, 2, 3, 4, 5};
  return arr[2];
}

int main ()
{
  int ret = fun();
  printf ("\n The return value is: %d\n", ret);
  return 0;
} 

Output:

The return value is: 3

Returning a complete one-dimensional array to a function:

A program that returns the complete one-dimensional array from the called function

#include <stdio.h>

int fun ()
{
  static int arr[5] = {1, 2, 3, 4, 5};
  return arr;
}

int main ()
{
  int *arr = fun();
  int i;
  printf ("\n The values of the array are:\n");
  for (i =0 ;i < 5; i++)
     printf("%d ", *(arr + i));
 
  return 0;
} 

Output:

The values of the array are:
1 2 3 4 5



Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: