Translate

Labels

Saturday, 23 March 2013

FUNCTION WITH ARGUMENTS AND NO RETURN VALUE

#include<stdio.h>
#include<conio.h>
void largest(int a,int b,int c)
{
int l;
l=a;
if(b>l)
l=b;
if(c>l)
l=c;
printf("Largest =%d \n",l);
}
void main()
{
int a,b,c;
int x,y,z;
clrscr();
printf("Enter values of a,b and c \n");
scanf("%d%d%d",&a,&b,&c);
largest(a,b,c);
printf("Enter values of x,y and z \n");
scanf("%d%d%d",&x,&y,&z);
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
6   3   2 
Largest=6

EXPLANATION

  • The function largest() is defined with arguments a,b and c of int type.The argument a,b and c are called formal arguments of the function largest().The purpose of the function is to find largest of the three integers values passed to it.Within the function, one more variable l is declared and it collects the largest of a,b and c.
  • In the main() a,b,c,x,y and z are declared to be variable of int type. Initially the values of a,b and c accepted and the function largest() is called by passing the values of a,b and c as largest(a,b,c).As far as the function call largest (a,b,c)  is concerned,the value of a,b and c belonging to main () are called the actual arguments for the function call.Note that a,b and c specified as formal arguments of largest()  and a,b and c(actual arguments) declared in main() even though they have same names they are different in terms of storage.As a consequence of the function call largest(a,b,c) in main(), the values of a,b and c are copied to the formal arguments of largest() a,b and c respectively.
  • After the formal arguments of largest() a,b and c get filled up, the function finds the largest of them and assigns it to the variable,which is then displayed.Control is regained by main().
  • Next three values are accepted into the variable x,y,z and again the function largest() is called by passing x,y and z as largest(x,y,z).Now x,y and z becomes actual arguments. Similar to the previous case as a consequence of the call largest(x,y,z) the value of x,y,z are copied to the formal argument a,b and c respectively.
  • After the formal argument of largest() a,b and c get filled up the function finds the largest of them and assigns it to the variable, which is then displayed.Control is regained by main().

No comments: