Page 12 - Sample_test_Float
P. 12
12 Functions and Pointers
void square(int j,int k)
{
j=j*j;
k=k*k;
return(j,k);//Error: a function cannot return two values at a time.
}
Variable (Actual Parameter) (Formal Parameter)
a b j k
Value 10 5 100 25
Address 1010 1012 1014 1016
The above example illustrates that it is not possible to return two values and without returning the values, the
updated values will not be reflected in the main function.
So using pointer, we pass the address of the value and in the called function when the value is updated, it will
be directly updating in the memory location and so it will be reflected in the main function.
Example 1
#include <stdio.h>
void main()
{
int a=10,b=5;
square(&a,&b);//the address is passed
printf(“%d %d”,a,b);
}
void square(int *j,int *k)
{
*j=(*j)*(*j); // updation is done directly in the memory location of variablea.
*b=(*b)*(*b); // updation is done directly in the memory location of variableb.
}
Variable (actual parameter) (ptr variable)
a b *j *k
Value 10 5 100 25
Address 1010 1012 1010 1012
Explanation:
&a and j refer to same memory location and hence any updates done using the pointer variable j will affect the
value of a.
&b and k refer to same memory location and hence any updates done using the pointer variable k will affect
the value of b.
Output
100 25

