1. ## Help with functions

I am brand new to programming and am takinga class in C++. So far I have been able to successfully create the programs requested and get the needed output but this problem has got me stumped. The original problem is to create a program that prompts the user to enter the center and a point on the circle. Then it should output (to screen) the circle's radius, diameter, circumference, and area. I have defined all the parameters and created the functions but I have the following errors in the debugger (btw I am using visual studio2010).

errors:
IntelliSense: no instance of overloaded function "distance" matches the argument list. line 65 column 7

IntelliSense: too few arguments in function call. Line 65, 69, 73, 77 and 81

Because of these errors nothing runs/outputs.

Here is the code that I have:

Code:
```#include <iostream>
#include <cmath>

using namespace std;

//function to figure out the distance
int distance (int x1, int y1, int x2, int y2)
{
int dx = x2 - x1;
int dy = y2 - y1;
double dsquared = dx*dx + dy*dy;
double result = sqrt (dsquared);
return result;
}

//function to figure out the radius
int radius (int x1, int y1, int x2, int y2)
{
double radius = distance (x1, y1, x2, y2);
}

//function to figure out the circumference
{
double circumference = 3.1416 * (radius * 2);
return circumference;
}

//function to figure out the area
{
return area;
}

//function to figure out the diameter
{
double diameter = 2 * radius;
return diameter;

}

int main()
{
int x1;	//variable to store the center point of the circle
int x2;	//variable to store the second point on the circle
int y1;	//variable to store the center point of the circle
int y2;	//variable to store the second point on the circle

cout <<"Enter a center point: ";
cin  >> x1, y1;

cout <<"Enter a point on the circle: ";
cin  >> x2, y2;

//distance return
cout <<"The distance of the circle is: "
<< distance() << endl;

//area return
cout <<"The area of the circle is: "
<< area() << endl;

cout <<"The radius of the circle is: "

//circumference return
cout <<"The circumference of the circle is: "
<< circumference() << endl;

//diameter
cout <<"The diameter of the circle is: "
<< diameter() << endl;

return 0;

}```
Any help or direction at this point would be greatly appricated. I have tried several different things to get it to work but to no avail.

Many thanks.

lostinprogramC+

Code:
`int distance (int x1, int y1, int x2, int y2)`
and then you call your function as:

Code:
`distance()`
Your function call and function definition must agree as to the number an type of arguments.

Also this line will cause an error:

Code:
`cin  >> x1, y1;`
It should be:

Code:
`cin  >> x1 >> y1;`
You are making these mistakes in several places in your code.

Jim

3. So if the function is

Code:
`int distance( int x1, int y1, int x2, int y2)`
then the call to the function should look the same...? Sorry functions have gotten me very confused.

like this
Code:
`distance( int x1, int y1, int x2, int y2)`
Thank you for catching the syntax errors that I had as well. I have been looking at this for 2 days now and should have spotted them. I am going through now to fix those errors.

4. Originally Posted by lostinprogramC+
So if the function is

Code:
`int distance( int x1, int y1, int x2, int y2)`
then the call to the function should look the same...? Sorry functions have gotten me very confused.

like this
Code:
`distance( int x1, int y1, int x2, int y2)`
Thank you for catching the syntax errors that I had as well. I have been looking at this for 2 days now and should have spotted them. I am going through now to fix those errors.
yup, the function you call must always match the function prototype, if it doesn't the compiler doesn't know what to do and starts moaning. however, unless you included the function from another file.

distance()
distance(int,int,int,int) have the same name, but have completely different signatures.
when you get further..you'll learn of overloaded functions.

5. Originally Posted by lostinprogramC+
So if the function is

Code:
`int distance( int x1, int y1, int x2, int y2)`
then the call to the function should look the same...? Sorry functions have gotten me very confused.

like this
Code:
`distance( int x1, int y1, int x2, int y2)`
Thank you for catching the syntax errors that I had as well. I have been looking at this for 2 days now and should have spotted them. I am going through now to fix those errors.
Nope... the function call names the actual variables being passed in...

as in ... distance(a,b,c,d);

Re-listing the various types of the variables would trigger a syntax error.

6. I have changed the call of the function to match the function that has been declared and I receive the following errors:

Error 6 error C2144: syntax error : 'int' should be preceded by ')'
Error 7 error C2660: 'distance' : function does not take 0 arguments
Error 8 error C2059: syntax error : ')'
Error 9 error C2664: 'area' : cannot convert parameter 1 from 'int (__cdecl *)(int,int,int,int)' to 'double'

and there are several of them.

I am following the examples that are in the book but nothing seems to be working correctly. I know that it is my lack of understanding when it comes to functions. Could you please elaborate on how to use them and call them. (act like you are talking to a child maybe then I will understand it. lol ;-)

examples would be great!

7. Originally Posted by lostinprogramC+
I have changed the call of the function to match the function that has been declared and I receive the following errors:

Error 6 error C2144: syntax error : 'int' should be preceded by ')'
Error 7 error C2660: 'distance' : function does not take 0 arguments
Error 8 error C2059: syntax error : ')'
Error 9 error C2664: 'area' : cannot convert parameter 1 from 'int (__cdecl *)(int,int,int,int)' to 'double'

and there are several of them.

I am following the examples that are in the book but nothing seems to be working correctly. I know that it is my lack of understanding when it comes to functions. Could you please elaborate on how to use them and call them. (act like you are talking to a child maybe then I will understand it. lol ;-)

examples would be great!
ok..you are returning doubles instead of ints.
an int simply is a whole number.
a float is a number with decimal part. a double is a float but allows bigger memory.

example
Code:
```   int foo()
{
double a = 10.0f;
return a ;
}```
I am returning a, which is double but the return type is an int..there is a mismatch
so i suggest you change all the return type to the type you're returning.
so use double area() or whatever it is.

also the functions must match the exact order of their prototype

void foo(a,b,c)
cannot be void foo(b,c,a)

8. ok so if I create the following function:

Code:
```double distance (double x1, double y1, double x2, double y2)			{
double dx = x2 - x1;
double dy = y2 - y1;
double dsquared = dx*dx + dy*dy;
double result = sqrt (dsquared);
return result;
}```
this is how it should be called?

Code:
```	cout <<"The distance of the circle is: "
<< distance(double x1, double y1, double x2, double y2)
<< endl;```

9. I don't think you have any variables called "double x1". You might have a variable called "x1", though.

10. Originally Posted by lostinprogramC+
this is how it should be called?

Code:
```	cout <<"The distance of the circle is: "
<< distance(x1, y1, x2, y2)
<< endl;```
see above...

11. I know I need to change my declarations but am still totally frustrated as to why this isn't working. Is the code even close to being written correctly for calling the distance function?

If I throw the cumputer will that make it work properly???????

12. Originally Posted by lostinprogramC+
I know I need to change my declarations but am still totally frustrated as to why this isn't working. Is the code even close to being written correctly for calling the distance function?

If I throw the cumputer will that make it work properly???????
The thing to keep in mind is that your functions aren't any different than anybody else's functions. You call sqrt (correctly) by putting the variable inside the parentheses. So do the same for distance: put the variable names inside the parentheses.

13. If I throw the cumputer will that make it work properly???????
you'll find quickly that functions are the easiest thing next to variables. I think they are.

if you've modified your code. can you repost it?

14. ok changed some code so here is the big sha bang again..... :-P

Code:
```#include <iostream>
#include <cmath>

using namespace std;

double distance (double x1, double y1, double x2, double y2)		//function to figure out the distance
{
double dx = x2 - x1;
double dy = y2 - y1;
double dsquared = dx*dx + dy*dy;
double result = sqrt (dsquared);
return result;
}

double radius (double x1, double y1, double x2, double y2)			//function to figure out the radius
{
double radius = distance (x1, y1, x2, y2);
}

double circumference(double radius)					//function to figure out the circumference
{
double circumference = 3.1416 * (radius * 2);
return circumference;
}

double area(double radius)								//function to figure out the area
{
return area;
}

double diameter(double radius)							//function to figure out the diameter
{
double diameter = 2 * radius;
return diameter;

}

int main()
{
double x1;								//variable to store the center point of the circle
double x2;								//variable to store the second point on the circle
double y1;								//variable to store the center point of the circle
double y2;								//variable to store the second point on the circle

cout <<"Enter a center point: ";
cin  >> x1 >> y1;

cout <<"Enter a point on the circle: ";
cin  >> x2 >> y2;

cout <<"The distance of the circle is: "
<< distance(double x1, double y1, double x2, double y2)
<< endl;

//area return
cout <<"The area of the circle is: "

cout <<"The radius of the circle is: "
<< radius(x1, y1, x2, y2) << endl;

//circumference return
cout <<"The circumference of the circle is: "

//diameter
cout <<"The diameter of the circle is: "

return 0;

}```
ok silly question, can I have more than one variable in the parameter? or is it best ot have one parameter?

ie:

Code:
```distance(double x1, double y1, double x2, double y2)

or

distance(x1)```
*I still think that throwing the cumpter will help ;-P

15. Code:
```	cout <<"The distance of the circle is: "
<< distance(double x1, double y1, double x2, double y2)
<< endl;```
You can't specify the argument type when calling a function. You should have this instead:
Code:
```	cout <<"The distance of the circle is: "
<< distance(x1, y1, x2, y2)
<< endl;```