## mrar85 Light Poster

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 circumferences(double);
double area(double);
double diameter(double);
void displayresult(double,double,double,double);

int main()
{
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;

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

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 circumferences(double perimeter)
{
return perimeter;
}
double area (double areas)
{
return areas;
}
double diameter(double dia)
{
return dia;
}
void displayresult(double rad,double perimeter,double areas,double dia)
{
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

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, [URL="

## Fbody 682

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.

``````int main()
{
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;

perimeter=[B]circumferences(perimeter)[/B];
areas=[B]area(areas)[/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

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);
}

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.

## mrar85

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;
}
{
}``````

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

## Fbody 682

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;
}
{
}``````

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.

## mrar85

so what you mean is like this:

``````//in calcRadian function
double calcRadian(double x1,double x2,double y1,double y2)
{
}

//in main fucntion
int main()
{
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;