-->

Passing Arrays to Function

Passing Arrays to Function
Initialization of a one dimensional Array using function
int d[] ={1,2,3,4,5), instead of this, functions can also be directly called to initialize the array.
The following program illustrates this
Initialization of a one dimensional Array using function

Example: Write a program to initialize an array using functions.

#inlcude<stdio.h>
#inlcud<conio.h>
main()
{
Int ge(), k, d[]={ge(), ge(),ge(), ge(),,ge()};
clrscr();
printf(“\n Array d[] elements are:”);
for(k=0; k<5; k++)
printf(“%2d”, d[k]);
retrun(NULL);
}
int ge()
{
static int, m,n;
m++;
printf(“\n Enter Number d [%d:]:”,m);
scanf(“%d”,&n);
retrun(n);
}

OUTPUT
Enter Number d[1]: 4
Enter Number d[2]: 5
Enter Number d[3]: 6 
Enter Number d[4]: 7
Enter Number d[5]: 8
Array d[] elements are: 4 5 6 7 8

Explanation:
Function can be called at the end of the declaration of an array, in the above program d[] is an integer array and ge() is a user-defined function.
The function ge() when called, reads the value through the keyboard. The function ge() is called from an array i.e., the value returned by the function is assigned to the array element.

Related Posts

Subscribe Our Newsletter