very much beginner quesstion

im a bit confused on this double constant. its defined as a constant but then used in the program body as different values? i'm just not understanding it i guess. thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
using namespace std;

const double pi = 3.14159;
const char newline = '\n';

int main ()
{
  double r=5.0;               // radius
  double circle;

  circle = 2 * pi * r;
  cout << circle;
  cout << newline;
}
Where do you think it is being "used in the program body as different values"?
const just means that variable will always equal that value. so pi will always = 3.14... you can still use it in other operationss, but you cant change what it equals
You should probably put a return statement like return 0; at the end before the closing bracket. Okay but now what is going on is that you have two different variables of datatype double. One is pi and the other is r. The reason it looks like it is changing is because the value of const pi is multiplied by the variable r and also the number 2. The product of this multiplication is given to the variable circle. Then you cout the value of circle which gave you your answer. if you also printed out the value of pi you would see that it is still the same.
Last edited on
@Gawaine: main is not a function, it has an implicit return statement at the end.

You should probably put a return statement like return 0; at the end before the closing bracket


its implicit. you only need a return statement if it returns something other than zero, or a variable
Ah ok. Your right. I was just always taught to do this out of habit.
It is still debated whether it is good practice to do so or not, however it is not debated whether it is legal to omit the return statement ;)
why would it be bad to omit it? just out of curiousity?
That's what's under debate ;)
first, there is no return statement because i just copied the code from the tutorial on this site and it didnt have one. my confusion was that i was overthinking it and not seeing double as a datatype but as the constant itself, now i understand! thank you
Topic archived. No new replies allowed.