Functions 329
OUTPUT:
Enter Date dd/mm/yy: 12 12 2001
Today = 12/12/2001
Tomorrow = 13/12/2001
Explanation In the above program three values date, month, and year are passed to function
dat (). The function displays date. The function dat () returns next date. The next date is printed
in function main (). Here, function d at () receives arguments and return values.
10.12 Write a program to send values to a user-defined function and receive and display the
return value.
#in clu de< std io.h>
#include<conio.h>
mainO
{
in t sum (in t , in t, in t), a ,b ,c ,s ;
c lrs c r ();
printf ("Enter Three Numbers :");
scanf ("%d %d %d", &a,&b,&c);
s=sum(a,b,c);
printf ("Sum = %d*'',s);
return 0;
}
$um( int x, int y, int z)
I
return (x+y+z);
I
OUTPUT:
Enter Three Numbers: 7 5 4
Sum = 16
Explanation In the above program the function sum () receives three values from function main ().
The sum () calculates the sum of three numbers and returns them to main ().
D) Without arguments but with return values
Table 10.6 Function without arguments but with return values
m ain()
abc()
{
{
int z;
Argument(s) are passed.
int y=5;
z=abc( );
Values are sent back.
}
return(y);
}
330 Programming and Data Structures
1) In the above type of function no arguments are passed through the main () function. But the
called function returns values.
2) The called function is independent. It reads values from the keyboard or generates from
initialization and returns the values.
3) Here both the calling and called function partly communicate with each other.
10.13 Write a program to receive values from a user-defined function without passing any
value through main ().
# in c lu d e < e t d io . h>
#inc lu de <c on io. h>
m ain()
{
int su m (),a,s;
c lrs c r ();
s=sum();
printf ("Sum = %d",s);
return 0;
}
sum( )
(int x,y,z;
printf ("n Enter Three Values
scanf ("% d %d %d",&x,&y,&z);
return (x+y+z);
I
OUTPUT:
Enter Three Values: 3 5 4
Sum * 13
Explanation The user defined function sum () is an independent function. The function sum ()
reads three values through the keyboard and returns their sum to function main (). The main ()
only prints the sum.
10.14 Write a program to read values through a user-defined function and send back the
result in the form of an address.
#in clu de<stdio •h>
#include<conio.h>
main()
{
int *sum (), *s;
c lr s c r ();
s = sum ();
..................Content has been hidden....................

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