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>
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);
return 0;
otherdnt k,int *m)
printf ("\ti Address ofk&m in other(): %u %u,&k,m);
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.
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;
p rin t f (" x ^ d " , x ) ;
Functions 335
double (m)
return (m* 2);
return (k*k);
Q i m U L
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>
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));}
int y;
printf ("\n Enter first number: “);
scanf ("% d",& y);
336 Programming and Data Structures
z ()
int z;
printf ("\n Enter second number: ");
scanf ("% d",& z);
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>
{in t uabs (in t),x ;
c lrsc r ();
printf ("Enter a Negative Value:");
printf("\n X= % d",x);
return 0;
uabs(int y)
retum( y * 1);

Get Programming and Data Structures now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.