334 Programming and Data Structures
to directly modify the contents of the variable. The memory addresses of the variables are unique.
There is no scope rule for memory address. If we declare the same variable for actual and formal
arguments, their memory addresses will be different from each other.
10.18 Write a program to pass arguments to a user-defined function by value and reference.
#include<8tdio sh>
#include<conio.h>
main()
{
in t k ,m ,o th e r(in t,in t*);
c lr s c r ();
printf ("n Address of k & m in mainO: %u %u",&k,&m);
other(k,&m);
return 0;
}
otherdnt k,int *m)
{
printf ("ti Address ofk&m in other(): %u %u,&k,m);
I
OUTPUT;
Address of k & m in main(): 65524 65522
Address of k & m in otherO: 65518 65522
Explanation In the above example we are passing value as well as reference, therefore, it is a mix
call, that is, calls by value and reference. The first variable is passed by call by value method and
the second by reference.
1) The variables'k'of the functions oth e r () andmainO are different from each other. The compiler
treats them differently. Hence their memory addresses are different.
2) We are passing the address of variable'm'. It is received by the pointer '*m' of the o t h e r ()
function, that is, the pointer'm' of function o th e r () contains the memory location of variable
'm' of main (). The pointer'm' points to variable'm'. Hence, the address printed o f'm ' is the
same as in main (). This is the difference between these two calls.
10.8 FUNCTION AS AN ARGUMENT
Till now we passed values or addresses through the functions. It is also possible to pass a function
as an argument.
10.19 Write a program to pass a user-defined function as an argument to another function,
m ain()
{
int y=2,x;
x=double(square(y));
p rin t f (" x ^ d " , x ) ;
Functions 335
}
double (m)
{
return (m* 2);
}
square(k)
{
return (k*k);
}
Q i m U L
x=8
Explanation In the above example instead of passing a variable or its address a function is passed
as an argument. The innermost function square () is executed first and completed. Its return
value is collected by the double () function. The double function uses the square () function as
an argument. It operates on the return value of square () function and makes the returned value
double and returns to the main () function.
10.20 Write a program to use two functions as arguments for another function.
#inclu de<stdio.h>
#include<conio.h>
#include<math.h>
mainO
{
in t d , x (i n t , i n t ), y ( ), z ();
d=x ( z(), y () );
printf ("n zO - y()= %d",d);
return 0;
}
x(int a, int b) I retum(abs(a-b));}
yo
{
int y;
clrscr();
printf ("n Enter first number: “);
scanf ("% d",& y);
retum(y);
336 Programming and Data Structures
I
z ()
I
int z;
printf ("n Enter second number: ");
scanf ("% d",& z);
retum(z);
I
QUTPUJj
Enter first number : 25
Enter second number : 50
z ()-y ()= 25
Explanation The above program consists of three user-defined functions x ( ) , y() and z (). The
functions z () and y () are passed through the function x () , that is, they are used as arguments.
They read values and the return to the function x () . The function x () carries subtraction and
return to main (). The variable 'd' prints the difference between these two values. The abs ()
function is called through the re tu rn () statement to return only positive value.
Note : abs () function returns absolute value of a given number.
The following program can be used in place of abs ( ) function. It is a user-defined function, we call it as
uabs (). 'U ' stands for user- defined.
10.21 Write a program to return only absolute value like abs () function.
# in c lu d e < s t d io . h>
#inclu de<con±o. h>
main()
{in t uabs (in t),x ;
c lrsc r ();
printf ("Enter a Negative Value:");
scanf("%d",&x)
x=uabs(x);
printf("n X= % d",x);
return 0;
!
uabs(int y)
/
if(y<0)
retum( y * 1);
Functions 337
else
retum(y);
I
OUTPUT;
Enter a Negative Value: - 5
X=5
Working of uabs () function: The uabs () function receives value from the calling function. If
the value is less than zero(O), that is, negative, it is multiplied by -1 to make it absolute otherwise
it is returned as it is.
10.22 Write a program to calculate square and cube of an entered number. Use the function
as an argument.
#include <math.h>
xnainO
{
int m;
c lrs c r O ;
printf ("ntCube: %d",cube(sqr(inputQ)));
i
input 0
(int k;
printf (" Number: ");
scanf ("%d",&k);
return k;
}
sqr (m )
{
printf ( Square : %d
return m;
}
cube (m )
{
return
t
OUTPUT:
Number: 2
Square: 4
Cube: 8
Explanation In the above program, three user-defined functions are defined. They are in put
(), sq r (), and cube ().
..................Content has been hidden....................

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