Translate

Labels

Showing posts with label FUNCTIONS. Show all posts
Showing posts with label FUNCTIONS. Show all posts

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.

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().

FUNCTION WITH NO ARGUMENTS AND NO RETURN VALUE

#include<stdio.h>
#include<conio.h>
void largest()
{
int a,b,c,l;
printf("Enter three numbers \n");
scanf("%d%d%d",&a,&b,&c);
l=a;
if(b>l)
l=b;
if(c>l)
l=c;
printf("Largest =%d\n",l);
}
void main()
{
clrscr();
largest();
getch();
}

OUTPUT:
Enter three numbers 
5     6     7

Largest=7

EXPLANATION:
The function largest() is to find the largest of three numbers.Its header indicates that it requires no argument and it does not return any value>IN the body of the function four variables are declared a,b and c are to collect three numbers.l is to collect the largest of them.The statements which follow, accept three numbers, find the largest of them and display it.
In the main(),largest() is invoked by just specifying its name and a pair of parentheses.the actual execution starts from and ends with main() only.When largest(); is encountered in main(), control is transferred to the function largest().upon completion of its execution,the control is regained by the main().