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 ) ;