im' confuse with this function...how do i call one function so it can be used in another function??

``````#include <iostream>
#include <cmath>
using namespace std;

double pi=3.1416;

double getinput();
double distance(double,double,double,double);
double radian(double);
double circumferences(double);
double area(double);
double diameter(double);
void displayresult(double,double,double,double);

int main()
{
double x1,x2,y1,y2;
double rad,perimeter,areas,dia;

cout<<"Please enter center point of a circle :";
cin>>x1>>y1;

cout<<"Please enter a point on a circle :";
cin>>x2>>y2;

rad=radian(rad);
perimeter=circumferences(perimeter);
areas=area(areas);

displayresult(rad,perimeter,areas,dia);

system("pause");
return 0;
}

/*double getinput()
{
double x1,x2,y1,y2;

cout<<"Please enter center point of a circle ";
cin>>x1>>y1;

cout<<"Please enter a point on a circle ";
cin>>x2>>y2;

}*/
double distance(double x1,double x2,double y1, double y2)
{
double dist;
dist = sqrt(pow(x2-x1,2)+pow(y2-y1,2));
return dist;
}
double radian(double rad)
{
rad=distance(x1,x2,y1,y2);
return rad;
}
double circumferences(double perimeter)
{
//double rad;
perimeter=2*pi*rad;
return perimeter;
}
double area (double areas)
{
//double rad;
areas=pi*pow(rad,2);
return areas;
}
double diameter(double dia)
{
//double rad;
dia=2*rad;
return dia;
}
void displayresult(double rad,double perimeter,double areas,double dia)
{
cout<<"The radian of the cirlce is : "<<rad<<endl;
cout<<"The circumferences of the circle is : "<<perimeter<<endl;
cout<<"The area of the circle is : "<<areas<<endl;
cout<<"The diameter of the circle is : "<<dia<<endl;

return;
}``````

help me please....teach me how to call

2
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by mrar85

Hmmm... This comes across as more of a general question about function use, not a question about using functions to calculate values related to a circle. Therefore, I'm going to reply with a little different slant than is perhaps suggested by your thread title.

In C++, every operator you use is some sort of function that gets called to perform the action requested. However, for the purposes of your question, see below.

Your main():

``````int main()
{
double x1,x2,y1,y2;
double rad,perimeter,areas,dia;

cout<<"Please enter center point of a circle :";
cin>>x1>>y1;

cout<<"Please enter a point on a circle :";
cin>>x2>>y2;

rad=[B]radian(rad)[/B];
perimeter=[B]circumferences(perimeter)[/B];
areas=[B]area(areas)[/B];

[B]displayresult(rad,perimeter,areas,dia)[/B];

[B]system("pause")[/B];
return 0;
}``````

Within the context of your question, each of the pieces of code that I bolded is a function call. To call a function, all you need to do is write the function's name and appropriate arguments where you need the function executed.

An example:

``````#include <iostream>

using std::cout;
using std::cin;
using std::endl;

double doubleMe(double);                 //prototype of doubleMe() function

int main() {
double inValue = 0.0;                  //define an input variable
double doubleValue = 0.0;              //accepts function return value

cout << "Enter the value to double: "; //prompt for an input
cin >> inValue;                        //get the input

doubleValue = [B]doubleMe(inValue)[/B];       //call doubleMe() function
cout << inValue << " doubled is: " << doubleValue << endl;
cout << "and " << doubleValue << " doubled is: " << [B]doubleMe(doubleValue)[/B] << endl;

return 0;
}

double doubleMe(double value) {          //function implementation
return (2.0 * value);
}``````

In this example, I called the function doubleMe() twice. The bold parts are function calls. If you want to take it one step further, you could do something like this:

``````#include <iostream>

using std::cout;
using std::cin;
using std::endl;

double doubleMe(double);                  //prototype of doubleMe() function
double quadMe(double);                    //prototype of quadMe() function

int main() {
double inValue = 0.0;                   //define an input variable
double doubleValue = 0.0;               //accepts function return value

cout << "Enter the value to double: ";  //prompt for an input
cin >> inValue;                         //get the input

doubleValue = [B]doubleMe(inValue)[/B];        //call doubleMe() function
cout << inValue << " doubled is: " << doubleValue << endl;
cout << "and " << doubleValue << " doubled is: " << [B]quadMe(inValue)[/B] << endl;

return 0;
}

double doubleMe(double value) {           //doubleMe() function implementation
return (2.0 * value);
}

double quadMe(double value) {             //quadMe() function implementation
return (2.0 * [B]doubleMe(value)[/B]);
}``````

In this example, I called the function doubleMe() twice, and the function quadMe() once. The bold parts are function calls.

Edited by Fbody: n/a

i understand that. but what i want to do is, call a function within a function,

for example :

``````double distance(double x1,double x2,double y1, double y2)
{
double dist;
dist = sqrt(pow(x2-x1,2)+pow(y2-y1,2));
return dist;
}
double radian(double rad)
{
rad=distance(x1,x2,y1,y2);
return rad;
}``````

why does the second function ask for the declaration of the x1...y2?
shouldn't it just call from the previous function?

i understand that. but what i want to do is, call a function within a function,

for example :

``````double distance(double x1,double x2,double y1, double y2)
{
double dist;
dist = sqrt(pow(x2-x1,2)+pow(y2-y1,2));
return dist;
}
double radian(double rad)
{
rad=distance(x1,x2,y1,y2);
return rad;
}``````

why does the second function ask for the declaration of the x1...y2?
shouldn't it just call from the previous function?

From a strictly structural standpoint, the syntax you have used is correct. But, from a logical/structural standpoint the call is not valid because of scoping rules.

C++'s scoping rules dictate that one function's local variables are not visible to another function. The variables x1, x2, y1, y2 are declared in main(), not radian(). This makes the variables local to main() and not visible to any other function. For your particular situation, you would be better off making the variable "rad" in main() directly receive the return from distance rather than trying to do it through another function (such as your radian function). If you have to use an intermediate function, I would recommend re-declaring it as `double calcRadius(double, double, double, double);` . I'll leave the new implementation to you.

Edited by Fbody: n/a

so what you mean is like this:

``````//in calcRadian function
double calcRadian(double x1,double x2,double y1,double y2)
{
double rad;
rad=distance(x1,x2,y1,y2);
return rad;
}

//in main fucntion
int main()
{
double x1,x2,y1,y2;
double rad;

cout<<"Please enter center point of a circle : ";
cin>>x1>>y1;

cout<<"Please enter a point on a circle : ";
cin>>x2>>y2;

rad=calcRadian(x1,x2,y1,y2);``````

so what is this scoping rules? im really curious

>>so what is this scoping rules? im really curious
Look back through the section in your notes/book concerning functions. I would be extremely surprised if you didn't already cover this.

Otherwise, read this.

aha...now i understand...thnx fbody...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.