codescracker
c

C Program to Pass Array to Function



« C Tutorial C Examples »

In this tutorial, we will learn about how to pass an individual array elements or whole array to a function in C. First we have created a program that passes whole array to a function. And then created a program that passes individual array elements to a function.

Passing Whole Array to Function

Let's learn with the program given below that how to pass whole array to any function in C. You can use the passed array inside a function just like inside the main function

// Passing Whole Array to a Function
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
void func(int arr[]);
int main()
{
    int arr[10], i;
    printf("Enter 10 array elements: ");
    for(i=0; i<10; i++)
        scanf("%d", &arr[i]);
    printf("\nPassing array to the function...\n");
    func(arr);
    getch();
    return 0;
}
void func(int arr[])
{
    int i;
    printf("\nThe array is:\n");
    for(i=0; i<10; i++)
        printf("%d ", arr[i]);
}

As you can see from the above program, the whole array (everything inside the array) gets passed in a function named func() and performed the operation inside that function.

The above program was written inside Code::Blocks IDE, therefore after successful build and run, here is the sample output:

c program pass whole array to function

Now provide any 10 array elements (numbers) and then press ENTER key. After this, you will get a message says Passing array to the function... and then the complete array gets passed to the function named func(). Inside that function, all the 10 entered array elements get printed back on the output screen, here is the second screenshot of the sample run:

pass whole array to function in c

Program Explained

Passing Individual Array Elements to Function

Now let's create another program that passes individual array elements to a function one by one.

// Passing Individual Array Elements to a Function
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
void check(int num);
int main()
{
    int arr[10], i;
    printf("Enter 10 Array elements:\n");
    for(i=0; i<10; i++)
        scanf("%d", &arr[i]);
    printf("\nChecking each elements for Even/Odd..\n");
    for(i=0; i<10; i++)
        check(arr[i]);
    getch();
    return 0;
}
void check(int num)
{
    if(num%2==0)
        printf("%d is Even\n", num);
    else
        printf("%d is Odd\n",num);
}

As you can see here, all the 10 elements of the array gets passed to the function check() that checks whether the element is an even number or an odd number. Here is its sample run:

c program pass array element to function

Now enter all the 10 numbers (array elements) and then press ENTER. Here is the sample output:

pass array element to function c

Program Explained

Here is the modified version of the above program. User is allowed to define the size for array before entering elements for it:

// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
void checkEven(int e);
void checkOdd(int o);
static int n1=0, n2=0;
int main()
{
    int arr[100], i, size;
    printf("How many elements you want to store: ");
    scanf("%d", &size);
    printf("Enter all %d Array elements:\n", size);
    for(i=0; i<size; i++)
        scanf("%d", &arr[i]);
    printf("\nChecking each elements for Even/Odd..\n");
    printf("\nList of all Even numbers: ");
    for(i=0; i<size; i++)
        checkEven(arr[i]);
    printf("\nList of all Odd numbers: ");
    for(i=0; i<size; i++)
        checkOdd(arr[i]);
    getch();
    return 0;
}
void checkEven(int e)
{
    if(e%2==0)
    {
        if(n1==0)
            printf("%d", e);
        else
            printf(", %d", e);
        n1++;
    }
}
void checkOdd(int o)
{
    if(o%2 != 0)
    {
        if(n2==0)
            printf("%d", o);
        else
            printf(", %d", o);
        n2++;
    }
}

This program is designed to ask from the user at run-time that how many number of elements he/she want to store say 10, and then ask to enter all elements (say 10 elements). Finally, the program will list out all the even and odd numbers. Let's take a look at its output with the sample run given below:

c program pass array to function

Now enter 10 as size for array, then provide all the 10 elements, and then press ENTER to see all the even and odd numbers from those entered 10 numbers:

pass array to function c program

Note - A variable of static types holds or remembers its previous value throughout the program.


« C Tutorial C Examples »