You are using a version without Ads of this website. Please, consider donating:

### Function Return is always true?

This program that I've made works fine to find midpoint, but not distance. The distancefunction always returns a 1 (true). To try to see that it wasn't the math, I added cout.setf(cout.boolalpha) to see and got a result of "true". I'm very confused and am wondering if anyone with a little more seasoning could help me... Thanks

//This program is a start to solve basic coordinatre plane distances and midpoints
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

double x1 ;
double y1 ;
double x2 ;
double y2 ;

double distancefunction(double distance)
{
double distancex ;
double distancey ;
distancex = (x2 - x1) * (x2 - x1) ;
distancey = (y2 - y1) * (y2 - y1) ;
distance = distancex + distancey ;

return distance ;
}
void input()
{
cout << "This is a calculator to find distance and the midpoint between two points.\n" ;
cout << "(\n" ;
cin >> x1 ;
cout << ",\n" ;
cin >> y1 ;
cout << ")\n\n" ;
cout << "(\n" ;
cin >> x2 ;
cout << ",\n" ;
cin >> y2 ;
cout << ")\n\n" ;
}

int main(int nNumberofArgs, char* pzArgs[])
{
input() ;

double midpointy ;
double midpointx ;

midpointx = (x1 + x2) / 2 ;
midpointy = (y1 + y2) / 2 ;

cout << " Midpoint: " << midpointx << "," << midpointy << endl ;
cout << " Distance: SQRT: " << distancefunction<< endl ;
system ("PAUSE") ;
}
cout << " Distance: SQRT: " << distancefunction<< endl ;

You are not calling the function there... you are merely printing a pointer to it.

Calling functions involves giving them (parenthesis).

So:

 distancefunction(0);

Would be actually calling your distance function (with the 'distance' parameter being 0).

Though your distancefunction should not have a distance parameter... because the distance is the output, not the input. distancefunction should take no parameters*... then you call it like this

 12345 distancefunction(); // or in a cout statement: cout << distancefunction() << endl; // <- note the parenthesis

* it actually should take x1,x2,y1,y2 parameters and those should not be globals... but whatever... I'm trying to keep this simple.
@jazpen

For example if your 2 points were (0.0,0.0) and (4.0,3.0), your function returns 25.0, not 5.0

Given you repeatedly ask the same question (in a slightly different form) and seem to refuse to use code tags, I am beginning to wonder whether you are trolling.

 http://www.cplusplus.com/forum/general/111626/
Thank you so much @Disch . I added in the parenthesis and the 0 and the program works like a charm. Thank you very much!

Dear @TheIdeasMan, I know that I did not put the sqrt of distance in. i don't need or want it for what I'm doing. All I need is the number before the squareroot. the larger program I am working on simplifies squareroots. Thank you for your input. I will be adding code tags to larger programs but this I didn't find it necessary. Later posts i do will feature code tags. Thank you.

Thank for the input everyone (or output in your case! )
Topic archived. No new replies allowed.