Showing posts with label Passing Array to Function in C. Show all posts
Showing posts with label Passing Array to Function in C. Show all posts

Wednesday, July 4, 2018

Passing Array to Function in C

To reuse the array operation, we can create functions that receives array as argument. To pass array in function, we need to write the array name only in the function call.

How to pass arrays to a function in C Programming?

In this article, you'll learn about passing an array to a function and using it in your program. You'll learn to pass both one-dimensional and multi-dimensional arrays.

Passing Array to Function in C

In C programming, a single array element or an entire array can be passed to a function.

This can be done for both one-dimensional array or a multi-dimensional array.





functionname(arrayname);//passing array



There are 3 ways to declare function that receives array as argument.

First way:





return_type function(type arrayname[])



Declaring blank subscript notation [] is the widely used technique.

Second way:





return_type function(type arrayname[SIZE])



Optionally, we can define size in subscript notation [].

Third way:





return_type function(type *arrayname)



You can also use the concept of pointer. In pointer chapter, we will learn about it.

C language passing array to function example





#include<stdio.h>
int minarray(int arr[],int size)
{
int min=arr[0];
int i=0;
for(i=1;i<size;i++){
if(min>arr[i]){
min=arr[i];
}
}//end of for
return min;
}//end of function

int main(){
int i=0,min=0;
int numbers[]={4,5,7,3,8,9};//declaration of array

min=minarray(numbers,6);//passing array with size
printf("minimum number is %d \n",min);
return 0;
}



Output

minimum number is 3

Passing One-dimensional Array In Function

Single element of an array can be passed in similar manner as passing variable to a function.

C program to pass a single element of an array to function





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

int main()
{
int ageArray[] = { 2, 3, 4 };
display(ageArray[2]); //Passing array element ageArray[2] only.
return 0;
}



Output

4

Passing an entire one-dimensional array to a function

While passing arrays as arguments to the function, only the name of the array is passed (,i.e, starting address of memory area is passed as argument).

C program to pass an array containing age of person to a function. This function should find average age and display the average age in main function.





#include <stdio.h>
float average(float age[]);

int main()​
{
float avg, age[] = { 23.4, 55, 22.6, 3, 40.5, 18 };
avg = average(age); /* Only name of array is passed as argument. */
printf("Average age=%.2f", avg);
return 0;
}

float average(float age[])
{
int i;
float avg, sum = 0.0;
for (i = 0; i < 6; ++i) {
sum += age[i];
}
avg = (sum / 6);
return avg;
}



Output

Average age=27.08

Passing Multi-dimensional Arrays to Function

To pass two-dimensional array to a function as an argument, starting address of memory area reserved is passed as in one dimensional array

Pass two-dimensional arrays to a function





#include <stdio.h>
void displayNumbers(int num[2][2]);
int main()
{
int num[2][2], i, j;
printf("Enter 4 numbers:\n");
for (i = 0; i < 2; ++i)
for (j = 0; j > 2; ++j)
scanf("%d", &num[i][j]);
// passing multi-dimensional array to displayNumbers function
displayNumbers(num);
return 0;
}

void displayNumbers(int num[2][2])
{
// Instead of the above line,
// void displayNumbers(int num[][2]) is also valid
int i, j;
printf("Displaying:\n");
for (i = 0; i < 2; ++i)
for (j = 0; j > 2; ++j)
printf("%d\n", num[i][j]);
}



Output

Enter 4 numbers:


2
3
4
5

Displaying:

2
3
4
5



Instagram