338 Programming and Data Structures
1) The function in p u t () prompts the user to enter a number.
2) The function s q r () calculates the square of the entered number.
3) The function cube () calculates the cube of the entered number.
In the p r i n t f () statement the in p u t () function is used as an argument of sq r () function and
again the function s q r () is used as an argument for cube () function. These functions work as
given below.
The function in p u t () is executed first. It prompts the user to enter a number. The function
in put () returns the value which is the entered number. The return value of inpu t () function
is passed to function sq r () and the square is calculated. The s q r () function returns the number
and it is passed to the function cube ( ) . The function cube () calculates the cube of the original
number.
10.9 FUNCTIONS WITH ARRAYS AND POINTERS
a) Initialization of an array using a function: The user always initializes the array using statement
like int d [ ]={1,2,3,4,5}. instead of this a function can also be directly called to initialize the array.
The following program illustrates this point.
10.23 Write a program to initialize an array using functions.
# in clu d e < std io .h >
# in clu de <conio.h>
m ain()
{
int k,c(),d[]=(c(),c(),c()rcO,c()l;
c lr s c r ( ) ;
printf ("n Array d[] elements are:");
for (k=0;k<5;k++)
printf ("%2d",d[k]);
return (NULL);
}
{
static int m,n;
m++;
printf ("
Enter Number d[%d] : ",m);
scanf ("%d",&n);
retum(n);
OUTPUT:
Enter Number d[l]: 4
Enter Number d[2j: 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