Pages

Showing posts with label FUNCTION. Show all posts
Showing posts with label FUNCTION. Show all posts

Friday, 18 March 2011

PROGRAM OF FUNCTION WITH PARAMETERS AND WITH RETURN VALUE

DESCRIPTION-:Function with parameters and with return value contains a parameters in the "function call" and return the value in the function definition.The value returned in the function definition is the final result or the output of the program.
Here in below we have a program which calculate the sum of two numbers by passing parameters in the function call and returning value in the function defination as output.
 
PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
int max(int,int);
cout<<"enter two numbers";
cin>>a>>b;
c=max(a,b);
cout<<"greatest number is"<<c;
getch();
}
int max(int x,int y)
{
if(x>y)
return(x);
else
return(y);
}

PROGRAM TO CALCULATE THE SUM OF THREE NUMBERS USING FUNCTION

DESCRIPTION-:tThe program calculate the sum of the three numbers using function.In this we pass three arguments or parameters in the function call and the sum of three number is return by the function definition block.
FOR EXAMPLE-:If we entered three number as--------

                              23    45    2
Then program generate output as-------------
                                The sum of x,y,z is=70

PROGRAM-:
#include<iostream.h>
#include<conio.h>
float sum(float,float,float);
void main()
{
clrscr();
float x,y,z;
cout<<"enter the value of three no"<<"\n";
cin>>x>>y>>z;
float total;
total=sum(x,y,z);
cout<<"sum of x,y,z="<<total<<"\n";
getch();
}
float sum(float a,float b,float c)
{
float temp;
temp=a+b+c;
return(temp);
}

PROGRAM TO CALCULATE SQUARE USING FUNCTION

DESCRIPTION-:This program calculate the square of 'n' numbers with the help of function.In this the function call is place under the for loop as we want to calculate the square of 'n' numbers not only one number and function definition returns the square of numbers.
FOR EXAMPLE-: If we enter the value of n-------
                                         3
 Then program generate output as---------------
                          i=0        square=0                       
                          i=1        square=1
                          i=2        square=4
                          i=3        square=9

PROGRAM-:
#include<iostream.h>
#include<conio.h>
void square(int);
void main()
{
clrscr();
int i,n;
cout<<"enter a value for n";
cin>>n;
for(i=0;i<=n;i++)
{
square(i);
getch();
}
}
void square(int x)
{
int y;
y=x*x;
cout<<"i="<<x<<"square="<<y<<"\n";
}

PROGRAM OF FUNCTION WITH NO PARAMETERS AND NO RETURN VALUES

DESCRIPTION-:Program with no parameters and without returning any value contains no parameters in function call and also not return any value in the function definition block.For these type of program also we do not pass any parameter or argument in function declaration.
PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
void print();
print();
cout<<"no parameters and no return type";
print();
getch();
}
void print()
{
for(int i=1;i<=10;i++)
{
cout<<"*";
}
}

PROGRAM TO FIND MINIMUM OF FOUR NUMBER USING FUNCTION

DESCRIPTION-:The program finds the greater between four number using the function.In program we pass the four parameters in the function call but in pair(means in function call we have two pair).This program firstly calculate the minimum number in first pair and then calculate the minimum number in second pair.After this program calculate the minimum number between the output of first and second pair.
FOR EXAMPLE-:If we enter four number as-------

                                     23    34    21    39
Then it generate output as----------
firstly it calculate minimum number between  23    and    34   =23
then it calculate minimum number between    21    and    39   =21
at last it calculate the minimum number between  21    and    23    =21

PROGRAM-:
#include<iostream.h>
#include<conio.h>
int min(int,int);
void main()
{
clrscr();
int a,b,c,d;
cout<<"enter four integers";
cin>>a>>b>>c>>d;
cout<<"minimun no. ="<<min(min(a,b),min(c,d));
getch();
}
int min(int x,int y)
{
return((x<y)?x:y);
}

PROGRAM TO FIND MINIMUM OF FOUR NUMBER USING FUNCTION

PROGRAM-:
#include<iostream.h>
#include<conio.h>
int min(int,int);
void main()
{
clrscr();
int a,b,c,d;
cout<<"enter four integers";
cin>>a>>b>>c>>d;
cout<<"minimun no. ="<<min(min(a,b),min(c,d));
getch();
}
int min(int x,int y)
{
return((x<y)?x:y);
}

PROGRAM OF INLINE FUNCTION

DESCRIPTION-:INLINE function is one which increase the performance of function in very large programs.Actually it replaces the function definition with the function call and with this the program control does not transfer to the function definition at each time when the function call is execute  thus increases the performance.


PROGRAM-:
#include<iostream.h>
#include<conio.h>
inline int max(int x,int y)
{
return(x>y?x:y);
}
void main()
{
clrscr();
int a,b,c;
cout<<"enter two numbers";
cin>>a>>b;
c=max(a,b);
cout<<"greatest of two number is"<<c;
getch();
}

PROGRAM OF FUNCTION OVERLOADING

DESCRIPTION-:IN Function overloading we use same function name for multiple purpose.Thus in function defination we have same function name the only difference is the arguments or parameters.The arguments pass in program defination is different and if we have same arguments in two function definitions then it will generate the error.
Following is the program of the function overloading which calculate the area of square,rectangle and triangle.
In this we have same function name"area" but arguments pass to them are different.
PROGRAM-:
#include<iostream.h>
#include<conio.h>
#include<math.h>
void area(int);
void area(int,int);
void area(int,int,int);
void main()
{
clrscr();
float a,b,c,le,br,side;
cout<<"enter the side of square";
cin>>side;
cout<<"enter length and breadth of rectangle";
cin>>le>>br;
cout<<"enter parameter a,b,c of traingle";
cin>>a>>b>>c;
area(side);
area(le,br);
area(a,b,c);
getch();
}
void area(int x)
{
int a;
a=x*x;
cout<<"area of square is"<<a<<"\n";
}
void area(int x,int y)
{
int b;
b=x*y;
cout<<"area of rectangle is"<<b<<"\n";
}
void area(int x,int y,int z)
{
float s=(x+y+z)/2;
float ar;
ar=sqrt(s*(s-x)*(s-y)*(s-z));
cout<<"area of triangle using HERO formula is"<<ar;
}

PROGRAM TO FIND THE FACTORIAL USING THE FUNCTION

DESCRIPTION-:This program calculate the factorial of the given number using the function.In function call we pass the one parameter and in function definition we use for loop to calculate the factorial of the given number.
FOR EXAMPLE-:If we enter number as--------
                                                            5
Then it generate output as---------------
                                     Factorial of number is =120


PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int fact(int);
int n,i,k;
cout<<"enter any number";
cin>>n;
k=fact(n);
cout<<"factorial of number is=  "<<k;
getch();
}
int fact(int x)
{
int fact=1;
if(x==0||x==1)
{
cout<<"factorial of number is"<<fact;
}
else
{
for(int i=2;i<=x;i++)
{
fact=fact*i;
}
}
return(fact);
}

PROGRAM OF FUNCTION WITH DEFAULT ARGUMENTS

DESCRIPTION-:Default arguments are one who's values are initialized in the function definition(means when we declare the argument in function definition).The value of all the default arguments remains same throughout the program unless it is changed in the function call.
As explain in the below program how function call effect the default arguments.

PROGRAM-:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void volume(int l=10,int w=10,int h=10);
void main()
{
clrscr();
volume();   //equivalent to (10,10,10)//
volume(5);  //equivalent to (5,10,10)//
volume(5,6);  //equivalent to (5,6,10)//
volume(6,4,5);  //equivalent to(6,4,5)//
getch();
}
void volume(int l,int w,int h)
{
cout<<"volume is"<<l*w*h<<"\n";
}

PROGRAM TO FIND THE SUM USING FUNCTION

DESCRIPTION-:This program calculate the sum of two numbers with the help of the function.In this program we pass two arguments in the function call and function definition returns the sum of the entered two numbers.In function call we pass two arguments of which we want to calculate the sum.
FOR EXAMPLE-:If we enter two numbers as-------
                                           2       4

Then it generate output as--------
                                   sum=6                       


PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
void sum(int,int);
cout<<"enter two numbers";
cin>>a>>b;
sum(a,b);
getch();
}
void sum(int x,int y)
{
int z;
z=x+y;
cout<<"sum="<<z;
}
 
Copyright (c) 2010 Concepts Of C++. Design by WPThemes Expert

Blogger Templates and RegistryBooster.