C++ | Function using constant argument

Introduction

This is a program that uses constant argument in the function. The program given below uses pi as a constant argument. The program is extendable. Go enjoy the program. Lets begin…..

Program to find the area of circle using function with constant argument

#include<iostream.h>
#include<conio.h>
//function prototype
float cir(float,float pi=3.142);
//main function
void main()
{
//clear the screen.
clrscr();
//declare variable as float.
float r,area;
//Input the radius.
cout<<“Enter the radius”<<endl;
cin>>r;
//calculate area using function.
area=cir(r);
//print area
cout<<“Area of circle is “<<area;
//get character
getch();
}

//function
float cir(float r,float pi)
{
float ar;
ar=pi*r*r;
return(ar);
}

Output

Enter the radius.
1
Area of circle is 3.142

How does it work

  1. You enter the radius.
  2. The function calculates the area.
  3. The area is printed.

Extending it

The program can be extended as this is a basic of c++ and this concept can be used in any function that requires a constant argument like here pi is constant i.e value of pi does not change. For ex. you can make a program of simple interest where rate is constant. Go extent it…..

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Declare the function prototype with a constant argument
  3. Add void main.
  4. Start program by first clearing the screen.
  5. Declare the variables as float (name them as you want.)
  6. Add the cout and cin of necessary inputs.
  7. Add the cout of area and call the function.
  8. Add the function definition outside the void main().

At the end

You learnt creating the C++ program of Area of different circle using function with constant argument pi. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s