How do I use reference parameters to return my values back to main?

I have a void function with 3 variables that I would like to output in main. The problem is I get a compiler error when I want to output my values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

void dude(float,float,float,float,float& distance,float& circum,float& area);


int main(int argc, char *argv[])
{
    float x, y, xQ, yQ; 
    
    cout << " Enter your center coordinates x and y seperated by spaces: " 
         << endl;
    cin  >> x >> y;
    
    cout << " Enter your distance coordinates x and y seperated by spaces: " 
         << endl;
    cin  >> xQ >> yQ;
    

    cout << " distance: " << distance << endl;
    

    cout << " radius: " << distance << endl;
    

    cout << " circumference: " << circum << endl;
    

    cout << " Area: " << area << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void dude(float x,float xQ, float y, float yQ,float& distance, float& circum,
          float& area)
{
    distance = sqrt(pow(x-xQ,2)+pow(y-yQ,2));
    
    const float pI = 3.1416;
    circum = 2 * pI * pow(distance,2); 

    area = pI * pow(distance,2);   
}
Last edited on
1
2
3
    cout << " distance: " << distance << endl;
    ...
    cout << " circumference: " << circum << endl;

You have not declared these variables in main.
You are not calling your "dude" function at all.
Last edited on
I was talking to someone from school. They told me I didn't have to declare them in main. I'll try that.
It compiles now, but I am getting 0 for all my values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

void dude(float,float,float,float,float& distance,float& circum,float& area);


int main(int argc, char *argv[])
{
    float x, y, xQ, yQ, distance = 0, circum = 0, area = 0;
    
    cout << " Enter your center coordinates x and y seperated by spaces: " 
         << endl;
    cin  >> x >> y;
    
    cout << " Enter your distance coordinates x and y seperated by spaces: " 
         << endl;
    cin  >> xQ >> yQ;
    
  

    cout << " distance: " << distance << endl;
    

    cout << " radius: " << distance << endl;
    

    cout << " circumference: " << circum << endl;
    

    cout << " Area: " << area << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void dude(float x,float xQ, float y, float yQ,float& distance, float& circum,
          float& area)
{
    distance = sqrt(pow(x-xQ,2)+pow(y-yQ,2));
    
    const float pI = 3.1416;
    circum = 2 * pI * pow(distance,2); 

    area = pI * pow(distance,2);   
}

I was talking to someone from school. They told me I didn't have to declare them in main.

They are wrong.



I'll say it again dude: You aren't calling your "dude" function at all, so nothing will be calculated at all.
Please bare with me, I'm new to creating functions. I called the "dude" function but I get a compiler error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

void dude(float,float,float,float,float& distance,float& circum,float& area);


int main(int argc, char *argv[])
{
    float x, y, xQ, yQ, distance = 0, circum = 0, area = 0;
    
    cout << " Enter your center coordinates x and y seperated by spaces: " 
         << endl;
    cin  >> x >> y;
    
    cout << " Enter your distance coordinates x and y seperated by spaces: " 
         << endl;
    cin  >> xQ >> yQ;
    
    dude(float x,float xQ, float y, float yQ, float& distance, float& circum,
          float& area);

    cout << " distance: " << distance << endl;
    

    cout << " radius: " << distance << endl;
    

    cout << " circumference: " << circum << endl;
    

    cout << " Area: " << area << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void dude(float x,float xQ, float y, float yQ,float& distance, float& circum,
          float& area)
{
    distance = sqrt(pow(x-xQ,2)+pow(y-yQ,2));
    
    const float pI = 3.1416;
    circum = 2 * pI * pow(distance,2); 

    area = pI * pow(distance,2);   
}
Ok, I fixed my errors. Thanks!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*Scott Kime
Lab #11
3/30/14
*/

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

void dude(float,float,float,float,float& distance,float& circum,float& area);


int main(int argc, char *argv[])
{
    float x, y, xQ, yQ, distance = 0, circum = 0, area = 0;
    
    cout << " Enter your center coordinates x and y seperated by spaces: " 
         << endl;
    cin  >> x >> y;
    
    cout << " Enter your distance coordinates x and y seperated by spaces: " 
         << endl;
    cin  >> xQ >> yQ;
    
    dude(x,xQ,y,yQ, distance, circum, area);

    cout << " distance: " << distance << endl;
    

    cout << " radius: " << distance << endl;
    

    cout << " circumference: " << circum << endl;
    

    cout << " Area: " << area << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void dude(float x,float xQ, float y, float yQ,float& distance, float& circum,
          float& area)
{
    distance = sqrt(pow(x-xQ,2)+pow(y-yQ,2));
    
    const float pI = 3.1416;
    circum = 2 * pI * pow(distance,2); 

    area = pI * pow(distance,2);   
}
when you prototype or define a function you write the variable type of the parameters for example

void functionName(int x, int y);

but when you call a function like you did in main, you dont put the types like so
int frst=4;
int scnd= 5;
fuctionName(frst, scnd);

thats probably the error
Topic archived. No new replies allowed.