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
Functions 339
Explanation The function can be called in the declaration of array. In the above program d[ ] is
an integer array and c () is a user-defined function. The function c (), when called, reads the
value through the keyboard. The function c () is called from an array, that is, the value returned by
the function is assigned to the array.
Tip: Compile and execute this program in C++.
b ) Passing array elements to a function: Arrays are a collection of one or more elements of the
same data type. An array element can be passed to the function by value or reference. The
following programs explain both the ways.
10.24 Write a program to pass an array element to the function. Use call by value method
# include <stdio.h>
# include <conio.h>
void mainO
{
in t k, show (int,int) ;
in t num []{1 2 ,1 3 ,1 4 ,1 5,16 ,1 7 ,1 8 };
c l r s c r ( ) ;
for (k=0;k<7;k++)
show(k^ium[k]);
}
show(int m,int u)
{
printf (" num[%d] = %d ",m+l,u);
}
OUTPUT:
num[l]=12
num[2]=13
num[3]=14
num[4]=15
num[5]=16
num[6]=17
num[7]=18
Explanation show () is a user-defined function. The array num [ ] is initialized with seven elements.
Using 'for' loop the show () function is called for seven times and one element is sent per call. The
function show () prints the element.
10.25 Write a program to pass an array element to the function. Use call by reference.
# include <stdio.h>
# include <conio.h>
340 Programming and Data Structures
void main()
{
void s h o w (in t*);
int num[]-{12 ,13 ,14 ,1 5,16 ,17,18 };
c lr s c r ();
show(num);
)
void show(int *u)
t
int m=0;
printf ("n num[7]={ ");
while (m !-7 )
{
printf ("% 2 d /',*(u++));
m++;
I
printf ("b }");
OUTPUT;
num[7]={12,13,14,15, 16,17,181
Explanation In the above program the base address of the zeroth element is passed to function
show () . The pointer *u contains base address of array num [ ]. The pointer notation prints the
elements.
c) Passing reverse array to a function
1(L26 Write a program to display array elements in reverse order.
# include <stdio.h>
tf include <conio.h>
void main()
{
int show(int*);
int num[]-{12,13,14,15,16,17,18};
c l r s c r ( ) ;
show (&nuxn [ 6 ] ) ;
}
showtint *u)
{
int m=6;
Functions 341
w h ile ( mlss~l)
f
printf ("nnum[%d] = %d ",m,*u);
u - -,m ~
I
return (NULL);
I
QUTFUT;
num[6]=18
num[5]=17
num[4]=16
num[3]=15
num[2j=14
num[l]=13
num[0]=12
Explanation Increment or decrement in any pointer points next or previous location of its type,
respectively. In the above program instead of the address of the zeroth element, address of the last
element is passed. The sixth element is the last element of the array. Decrement in the pointer
points to the address of the previous element of array.
d) Copying an array: We have already studied call by reference in which one can change the
contents of any local variable of another function, provided that its reference or address should
be available. Using the call by reference method, copying contents of one array to another array
s possible.
10.27 Wnte a program to copy array elements using a user-defined function.
# include <stdio.h>
# include <conio.h>
void mainO
{
in t c p y (in t*, in t * ),h ;
int a l []= {l , 2 , 3 ,4 ,5 } ,a 2 [5 ] ;
c lr s c r ( ) ;
cpy (&al[0],&a2[01);
printf ("Source Target ");
for (h=0;h<5;h++)
printf ("n%5dtt%d",al[h],a2[h]);
»
342 Programming and Data Structures
cpy ( int *p,int *m)
{
int j*0;
while (j!=5)
I
p++;
m++;
j++;
»
retum(NULL);
}
OUTPUT:
Source Target
1 1
2 2
3 3
4 4
5 5
Explanation The function cpy () collects base addresses of both the arrays sent by main ()
function. The pointer *p points to array al[ ] and the pointer '*m' points to array a2 [ ]. Assigning
value of *p to *m, elements of array al[ ] are copied to array a2[ ]. The 'for' loop used in the
main () function displays the contents of both the arrays. Here, the function cpy () is called only
once. Using base addresses of the array the function ' cpy () ' performs this task independently.
e) Reading a private array
10.28 Write a program to read an array of another function in main ().
# in clud* <atdio.h>
# include cconio.h>
void main()
{
in t kj
in t a r ry () ;
c l r s c r ( ) ;
for (k=0;k<5;k++)
I
printf ("t% d "rarry());
I
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.168.203