Translate

Labels

Monday, 1 April 2013

FUNCTION WITH ARGUMENTS AND RETURN VALUE

#include<stdio.h>
#include<conio.h>
int largest (int a,int b,int c)
{
int largest;
largest=a;
if(b>largest)
largest=b;
if(c>largest)
largest=c;
return(largest);
}
void main()
{
int a,b,c,lar;
int x,y,z;
clrscr();
printf("Enter value of a,b and c\n");
scanf("%d%d%d",&a,&b,&c);
lar=largest(a,b,c);
printf("Largest =%d\n",lar);
printf("Enter the values of x,y and z \n");
scanf("%d%d%d",&x,&y,&z);
printf("Largest =%d\n ",largest(X,Y,Z));
getch();
}

OUTPUT:
Enter values of a,b and c
3   4   5
Largest=5
Enter values of X,Y and Z
4   5   6 
Largest=6

Explanation
Here as in the case of previous programs the largest()takes three variable a,b and c of int type as formal arguments and finds the largest of the values collected by the argument but unlike in the previous cases it does not display the largest value found out but returns it to the calling function(main()).
In the main(),the values of variables a,b,c are passed in the first call is used on RHS of an assignment statement as 
lar=largest(a,b,c);
The values returned by the function is collected by the variable lar and it is displayed.
In the second call to the function largest(), the values of x,y,z are passed.
Lar=Largest (x,y,z);
As a result the function returns the largest os x,y,z and it is assigned to the variable which is then displayed.

No comments: