Functions program in C++

Contents


1. Program to Simple Function

#include <iostream.h>
#include <conio.h>

void show( int x ); // Function Declaration/Prototype

void main()
{
clrscr();

int a = 10;

show(a); // Calling of Function

getch();
}

void show( int x ) // Function Definition
{
cout << "\n x = " << x;
}

Output:

x = 10




2. Program to calculate Simple Interest using Function ( No Argument and No Return Value )

#include <iostream.h>
#include <conio.h>

void Simp_Int();

void main()
{
clrscr();

Simp_Int();

getch();
}

void Simp_Int()
{
float p, r, t, si;
cout << " Enter Principal, Rate and Time : ";
cin >> p >> r >> t;

si = ( p * r * t ) / 100;

cout << "\n Simple Interest = " << si;
}

Output:

Enter Principal, Rate and Time : 1000 10 3
Simple Interest = 300




3. Program to calculate Simple Interest using Function ( No Argument but Return Value )

#include <iostream.h>
#include <conio.h>

float Simp_Int();

void main()
{
clrscr();
float si;

si = Simp_Int();

cout << "\n Simple Interest = " << si;
getch();
}

float Simp_Int()
{
float p, r, t, si;
cout << " Enter Principal, Rate and Time : ";
cin >> p >> r >> t;

si = ( p * r * t ) / 100;

return si;
}

Output:

Enter Principal, Rate and Time : 1000 10 3
Simple Interest = 300





4. Program to calculate Simple Interest using Function ( Argument but No Return Value )

#include <iostream.h>
#include <conio.h>

void Simp_Int( float, float, float );

void main()
{
clrscr();
float p, r, t;
cout << " Enter Principal, Rate and Time : ";
cin >> p >> r >> t;

Simp_Int( p, r, t );

getch();
}

void Simp_Int( float p, float r, float t)
{
float si;

si = ( p * r * t ) / 100;

cout << "\n Simple Interest = " << si;
}

Output:

Enter Principal, Rate and Time : 1000 5 3

Simple Interest = 150





5. Program to calculate Simple Interest using Function ( Argument and Return Value )

#include <iostream.h>
#include <conio.h>

float Simp_Int( float, float, float );

void main()
{
clrscr();
float p, r, t, si;
cout << " Enter Principal, Rate and Time : ";
cin >> p >> r >> t;

si = Simp_Int( p, r, t );

cout << "\n Simple Interest = " << si;
getch();
}

float Simp_Int( float p, float r, float t)
{
float si;

si = ( p * r * t ) / 100;

return si;
}

Output:

Enter Principal, Rate and Time : 1000 10 5

Simple Interest = 500





6. Program to Eliminate Function Declaration with Function

#include <iostream.h>
#include <conio.h>

// Eliminating declaration.
void show( int x )
{
cout << "\n x = " << x;
}

void main()
{
clrscr();

int a = 10;

show(a);

getch();
}

Output:

X = 10





7. Program to explain Call By Value

#include <iostream.h>
#include <conio.h>

void cube(int);

void main()
{
clrscr();
int a = 10;

cube(a);

cout << "\n a = " << a << endl;
      
getch();
}

void cube(int x)
{
x = x * x * x;
cout << "\n x = " << x << endl;
}

Output:

x = 1000

a = 10





8. Program to explain Call By Reference.

#include <iostream.h>
#include <conio.h>

void cube(int&);

void main()
{
clrscr();
int a = 10;

cube(a);

cout << "\n a = " << a << endl;
      
getch();
}

void cube(int &x)
{
x = x * x * x;
cout << "\n x = " << x << endl;
}

Output:

x = 1000
a = 1000





9. Program to explain Call By Address/Pointer

#include <iostream.h>
#include <conio.h>

void cube(int*);

void main()
{
clrscr();
int a = 10;

cube(&a);

cout << "\n a = " << a << endl;
      
getch();
}

void cube(int *pa)
{
*pa = (*pa) * (*pa) * (*pa);
cout << "\n *pa = " << *pa << endl;
}

Output:

*pa = 1000
  a = 1000





10. Program to find Factorial of given Number

#include <iostream.h>
#include <conio.h>

long fact(long);

void main()
{
clrscr();
long n, f;

cout << " Enter the Number : ";
cin >> n;

f = fact(n);
cout << "\n  Factorial = "<< f;

getch();
}

long fact(long a)
{
long f = 1;
int i;
for( i=1 ; i<=a ; i++ )
{
f = f * i;
}
return f;
}

Output:

  Enter the Number : 5
  Factorial = 120




11. Program to calculate Factorial using Recursion

#include <iostream.h>
#include <conio.h>

long fact(int);

void main()
{
clrscr();
int n;

cout << " Enter any Number : ";
cin >> n;

long f = fact(n);

cout << " Factorial = " << f ;

getch();
}

long fact(int a)
{
if(a == 1)
return 1;
else
return (a * fact(a-1));
}

Output:

Enter any Number : 5
Factorial = 120




12. Program to find nth power of x using Recursion

#include <iostream.h>
#include <conio.h>

int power(int, int);

void main()
{
clrscr();
long x, n, p;

cout << " Enter the Base : ";
cin >> x;
cout << " Enter the Exponent/Power : ";
cin >> n;

p = power(x, n);

cout << n << "th power of << x << " is = " << p;

getch();
}

int  power(int x, int n)
{
if(n == 0)
return 1;
else
return  x * power(x, n-1);
}

Output:

Enter the Base : 5
Enter the Exponent/Power : 4
4th power of 5 is = 625





13. Program to find nth term of Fibonacci Series using Recursion

#include <iostream.h>
#include <conio.h>

int fibo(int);

void main()
{
clrscr();
int n, t;

cout << " Enter term number : ";
cin >> n;

t = fibo(n);

cout << n << "th term of Fibonacci series is = " << t;

getch();
}

int  fibo(int n)
{
if( n==0 || n==1 )
return n;
else
return  fibo(n-1) + fibo(n-2);
}

Output:

  Enter term number : 4
4th term of Fibonacci series is = 3





14. Program to pass 1-D Array to Function

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>

const int SZ = 5;

void display( int a[SZ] );

void main()
{
clrscr();
int ar[SZ], i;

for( i=0 ; i<SZ ; i++ )
{
cout << " Enter Value : ";
cin >> ar[ i ];
}

display( ar );

getch();
}

void display( int a[SZ] )
{
cout << "\n Values are : ";
for( int i=0 ; i<SZ ; i++ )
{
cout << setw(3) << a[ i ];
}
}

Output:

Enter Value : 10
Enter Value : 20
Enter Value : 30
Enter Value : 40
Enter Value : 50

Values are : 10  20  30  40  50






15. Program to pass 2-D Array to Function

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

void display( int[2][3] );

void main()
{
clrscr();

int ar[2][3] = { { 10, 20, 30 },
{ 40, 50, 60 } };

display( ar );

getch();
}

void display( int a[2][3] )
{
int i, j;
cout << "\n Matrix-A: \n";
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << setw(5) << a[ i ][ j ];
}
cout << endl;
}
}

Output:

Matrix-A:
10    20    30
40    50    60





16. Program to explain inline Function

#include <iostream.h>
#include <conio.h>

inline float lbstokg( float pounds )
{
     return 0.453592 * pounds;
}

void main()
{
     clrscr();
     float lbs;

     cout << "\n Enter your weight in pounds : ";
     cin >> lbs;

     cout << " Your weight in kilogram is = " << lbstokg( lbs );

     getch();
}

Output:

Enter your weight in pounds : 120
Your weight in kilogram is = 54.431042





17. Program to explain Inline Function with Constant Argument

#include <iostream.h>
#include <conio.h>

const float PI = 3.1415926;

inline float area( const float r )
{
return ( PI * r * r );
}

main()
{
   clrscr();
float radius;

   cout << "\n Enter the radius of a circle : ";
   cin >> radius;

   cout << "\n The area of circle is " << area( radius );

   getch();
return 0;
}

Output:

Enter the radius of a circle : 10

The area of circle is 314.159241





18. Program to explain Constant Argument to function

#include <iostream.h>
#include <conio.h>

void show(const int);

void main()
{
clrscr();

int a = 10;

show(a);

cout << "\n a = " << a;

getch();
}

void show( const int b )
{
// b = 20; // can't be changed.
cout << "\n b = " << b;
}

Output:

b = 10
a = 10





19. Program to explain Function Overloading

#include <iostream.h>
#include <conio.h>

float  Simp_Int( float, float, float );
float  Simp_Int( float, float );
float  Simp_Int( float );
float  Simp_Int( );

void main()
{
clrscr();
float p, r, t, si;

si = Simp_Int( 2000, 15, 2 );
cout << "\n Simple Interest = " << si;

si = Simp_Int( 2000, 15 );
cout << "\n Simple Interest = " << si;

si = Simp_Int( 2000 );
cout << "\n Simple Interest = " << si;

si = Simp_Int( );
cout << "\n Simple Interest = " << si;

getch();
}

float Simp_Int( float p, float r, float t)
{
float si;

si = ( p * r * t ) / 100;

return si;
}

float Simp_Int( float p, float r )
{
float si;

si = ( p * r * 3 ) / 100;

return si;
}

float Simp_Int( float p )
{
float si;

si = ( p * 10 * 3 ) / 100;

return si;
}

float Simp_Int( )
{
float si;

si = ( 1000 * 10 * 3 ) / 100;

return si;
}

Output:

Simple Interest = 600
Simple Interest = 900
Simple Interest = 600
Simple Interest = 300





20. Program to explain Returning By Reference.

#include <iostream.h>
#include <conio.h>

int &min( int &a, int &b )
{
if (a < b)
return a;
else
return b;
}

void main()
{
clrscr();
int x = 10, y = 20;
cout << "\n x =" << x << ", y = " << y;

min( x, y ) = 100;

cout << "\n x =" << x << ", y = " << y;
getch();
}

Output:

x =10, y = 20
x =100, y = 20





21. Program to Merge Two Array using Function

#include <iostream.h>
#include <conio.h>

void Merge( int [ ], int, int[ ], int, int[ ] );

void main()
{
clrscr();
int A[50], B[50], C[100], MN = 0, M, N;

cout << "\n Enter Number of Elements in First Array  : ";
cin >> M;
cout << "\nEnter First Array elements [ASCENDING]  : ";
for( int i=0 ; i<M ; i++ )
{
cin >> A[i];
}

cout << "\n\nEnter Number of Elements in Second Array  : ";
cin >> N;
cout << "\nEnter Second Array elements [ASCENDING]  : ";
for( i=0 ; i<N ; i++ )
{
cin >> B[i];
}

MN = M + N;

Merge(A, M, B, N, C);

cout << "\n\nThe Sorted Array After Merging is:\n";
for( i=0 ; i<MN ; i++ )
{
cout << C[i] << " ";
}

getch();
}

void Merge( int A[ ], int M, int B[ ], int N, int C[ ] )
{
int a, b, c;
for( a=0, b=0, c=0 ; a<M && b<N ; )
{
if( A[a] < B[b] )
C[c++] = A[a++];
else
C[c++] = B[b++];
}

if( a < M )
{
while( a < M )
C[c++] = A[a++];
}
else
{
while( b < N )
C[c++] = B[b++];
}
}

Output:

Enter Number of Elements in First Array  : 4
Enter First Array elements [ASCENDING]  : 10 40 50 80

Enter Number of Elements in Second Array  : 5
Enter Second Array elements [ASCENDING]  : 20 30 60 70 90

The Sorted Array After Merging is:
10 20 30 40 50 60 70 80 90


Reactions

Post a Comment

0 Comments