Sami
Sami

Reputation: 1404

Calculating distance: method "must return a value"?

I'm trying to call the dist() method however I keep getting a error saying that dist() must return a value.

// creating array of cities
double x[] = {21.0,12.0,15.0,3.0,7.0,30.0};
double y[] = {17.0,10.0,4.0,2.0,3.0,1.0};

// distance function - C = sqrt of A squared + B squared

double dist(int c1, int c2) {
    z = sqrt ((x[c1] - x[c2] * x[c1] - x[c2]) + (y[c1] - y[c2] * y[c1] - y[c2]));
    cout << "The result is " << z;
}

void main()
{
    int a[] = {1, 2, 3, 4, 5, 6};
    execute(a, 0, sizeof(a)/sizeof(int));

    int  x;

    printf("Type in a number \n");
    scanf("%d", &x);

    int  y;

    printf("Type in a number \n");
    scanf("%d", &y);

    dist (x,y);
} 

Upvotes: 0

Views: 224

Answers (5)

MGwynne
MGwynne

Reputation: 3522

You are outputting "The result is z" to STDOUT but not actually returning it as the result of the dist function.

So

double dist(int c1, int c2) {

    z = sqrt (
         (x[c1] - x[c2] * x[c1] - x[c2]) + (y[c1] - y[c2] * y[c1] - y[c2]));
      cout << "The result is " << z;
}

should be

double dist(int c1, int c2) {

    z = sqrt (
         (x[c1] - x[c2] * x[c1] - x[c2]) + (y[c1] - y[c2] * y[c1] - y[c2]));
      cout << "The result is " << z;
    return(z);
}

(assuming you still want to print it).


Alternatively

You can declare that dist doesn't return a value using void:

void dist(int c1, int c2) {

    z = sqrt (
         (x[c1] - x[c2] * x[c1] - x[c2]) + (y[c1] - y[c2] * y[c1] - y[c2]));
      cout << "The result is " << z;
}

See: C++ function tutorial.

Upvotes: 3

Andrew
Andrew

Reputation: 1027

Since you have defined dist to return double ("double dist"), on the bottom of dist() you should do "return dist;" or change "double dist" to "void dist" - void means it doesnt need to return anything.

Upvotes: 0

innochenti
innochenti

Reputation: 1103

just add following line: return z; -1 for such question.

Upvotes: 0

JaredPar
JaredPar

Reputation: 754545

The dist function is declared to return a double but returns nothing. You need to explicitly return z or change the return type to void

// Option #1 
double dist(int c1, int c2) {
    z = sqrt (
         (x[c1] - x[c2] * x[c1] - x[c2]) + (y[c1] - y[c2] * y[c1] - y[c2]));
      cout << "The result is " << z;
    return z;
}

// Option #2
void dist(int c1, int c2) {
    z = sqrt (
         (x[c1] - x[c2] * x[c1] - x[c2]) + (y[c1] - y[c2] * y[c1] - y[c2]));
      cout << "The result is " << z;
}

Upvotes: 4

Vinnie Falco
Vinnie Falco

Reputation: 5353

Either change the return type to void:

void dist(int c1, int c2) {

  z = sqrt ((x[c1] - x[c2] * x[c1] - x[c2]) +
           (y[c1] - y[c2] * y[c1] - y[c2]));
  cout << "The result is " << z;
}

or return the value at the end of the function:

double dist(int c1, int c2) {

  z = sqrt ((x[c1] - x[c2] * x[c1] - x[c2]) +
           (y[c1] - y[c2] * y[c1] - y[c2]));
  cout << "The result is " << z;
  return z;
}

Upvotes: 7

Related Questions