Issues with Functions

Whats Up my nerds!!!!! I am having issues using functions. Im tryin to use two separate functions using the code provided that will calculate the circumference and area. Any idea? I can seem to get my findArea to work properly! Suggestions are truly appreciated

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
55
56
57
58
59
60
61
62
  #include <iostream>
#include <iomanip>
using namespace std;
// This program will demonstrate the scope rules.

const double PI = 3.14;
const double RATE = 0.25;
void findArea(float, float&);
void findCircumference(float, float&);

int main()
{
    cout << fixed << showpoint << setprecision(2);
    float radius = 12;
    
    cout <<" Main function outer block" << endl;
    cout <<" LIST THE IDENTIFIERS THAT are active here" << endl << endl;
    
{
    float area;
    float answer;
    float rad;
    cout << "Main function first inner block" << endl;
    cout << "LIST THE IDENTIFIERS THAT are active here" << endl << endl;
    findArea(rad,answer);
    cout << "The radius = " << radius << endl;
    cout << "The area = " << area << endl << endl;
}
{
    float radius = 10;
    float circumference;
    cout << "Main function second inner block" << endl;
    cout << "LIST THE IDENTIFIERS THAT are active here" << endl << endl;
    // Fill in the code to call findCircumference here
    //findCircumference(length,distance);
    cout << "The radius = " << radius << endl;
    cout << "The circumference = " << circumference << endl << endl;
}
    cout << "Main function after all the calls" << endl;
    cout << "LIST THE IDENTIFIERS THAT are active here" << endl << endl; return 0;
return 0;
}

void findArea(float rad, float& answer)
{
    cout << "AREA FUNCTION" << endl << endl;
    cout << "LIST THE IDENTIFIERS THAT are active here"<< endl << endl;
    // FILL in the code, given that parameter rad contains the radius, that
    // will find the area to be stored in answer
    
    answer = PI * radius * radius;
    cout <<"Your area is: "<<answer<< ".\n" << endl;
    
}
void findCircumference(float length, float& distance)
{
    cout << "CIRCUMFERENCE FUNCTION" << endl << endl;
    cout << "LIST THE IDENTIFIERS THAT are active here" << endl << endl;
    // FILL in the code, given that parameter length contains the radius,
    // that will find the circumference to be stored in distance
}
Read the compiler error message. It's telling you what the problem is.
I understand its an undeclared identifier. That said, its been declared on the main function, do I have to declare it somewhere else?
Inside a function, the following variables exist:

1) Global variables that exist everywhere.
2) Variables created inside that function
3) Variables passed into that function as parameters.

1
2
3
4
5
6
7
8
9
10
void findArea(float rad, float& answer)
{
    cout << "AREA FUNCTION" << endl << endl;
    cout << "LIST THE IDENTIFIERS THAT are active here"<< endl << endl;
    // FILL in the code, given that parameter rad contains the radius, that
    // will find the area to be stored in answer
    
    answer = PI * radius * radius;
    cout <<"Your area is: "<<answer<< ".\n" << endl;
}
In this function, radius does not exist. Perhaps you could pass it in as a parameter.
Found the error! 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

//Grupo # 2
//Rodriguez Rivera, Carlos A. (135994)

#include <iostream>
#include <iomanip>
using namespace std;

const double PI = 3.14;
const double RATE = 0.25;

void findArea(float, float&);
void findCircumference(float, float&);

int main()
{
    cout << fixed << showpoint << setprecision(2);
    float radius = 12;

    cout <<"Main function outer block" << endl;
    cout <<" PI, RATE, findArea, findCircumference, radius"
           " are active here" << endl << endl;
    {
        float area;
        cout << "Main function first inner block" << endl;
        cout << " PI, RATE, findArea, findCircumference, radius, area"
                " are active here" << endl << endl;

        findArea(radius, area);

        cout << "The radius = " << radius << endl;
        cout << "The area = " << area << endl << endl;
    }

    {
        float radius = 10;
        float circumference;

        cout << "Main function second inner block" << endl;
        cout << " PI, RATE, findArea, findCircumference, radius (local),\n"
                " circumference are active here" << endl << endl;

        findCircumference(radius, circumference);

        cout << "The radius = " << radius << endl;
        cout << "The circumference = " << circumference << endl << endl;
    }

    cout << "Main function after all the calls" << endl;
    cout << " PI, RATE, findArea, findCircumference, radius"
            " are active here" << endl << endl;

    return 0;
}

//  *********************************************************************
//                           findArea
//
//   task:     This function finds the area of a circle given its radius
//   data in:  radius of a circle
//   data out: answer (which alters the corresponding actual parameter)
//
//   ********************************************************************
void findArea(float rad, float& answer)
{

    cout << "AREA FUNCTION" << endl;
    cout << " PI, RATE, findArea, findCircumference, rad, answer"
            " are active here" << endl << endl;

    // FILL in the code, given that parameter rad contains the radius, that
    // will find the areato be stored in answer
    answer = PI * rad * rad;

}

//  ******************************************************************************
//                           findCircumference
//
//   task:     This function finds the circumference of a circle given its radius
//   data in:  radius of a circle
//   data out: distance (which alters the corresponding actual parameter)
//
//   *****************************************************************************
void findCircumference(float length, float& distance)
{
    cout << "CIRCUMFERENCE FUNCTION" << endl;
    cout << " PI, RATE, findArea, findCircumference, length, distance"
            " are active here" << endl << endl;

    // FILL in the code, given that parameter length contains the radius,
    // that will find the circumference to be stored in distance
    distance = 2 * PI * length;

}

Topic archived. No new replies allowed.