I apologize if I sound a bit dim-witted. I'm just diving into C++ (and programming in general) over the last week. I am using C++ Primer Plus as a guide.
I have been messing around with headers and prototypes recently, and have surprisingly found that the following code works, even without including the <cmath> header or a "double sqrt(double);" statement. I am using Visual Studio as my compiler. Any ideas on why I don't need a prototype for this function?
#include <iostream>
int main()
{
double number;
double square;
usingnamespace std;
cout << "Hi. My name is Timmy.\n";
cin.get();
cout << "I have an amazing gift.\n";
cin.get();
cout << "I have memorized the square root of every possible number." <<endl;
cin.get();
cout << "You don't believe me? Try me. Type in any number here: ";
cin >> number;
square = sqrt(number);
cout << "\nThe square root of "
<< number
<< " is "
<< square
<< ".\n";
cin.get();
cin.get();
cout << "Believe me now?";
cin.get();
}
Might be included in the standard namespace. I'm not sure if that's compiler specific - if I try to run your code through the compiler on the forum (click the little gear icon at the top right of the code block), it won't compile because it doesn't recognize sqrt.
The only likely explanation is that the <iostream> header on your platform includes <cmath> internally. You can view the <iostream> header source code through Visual Studio to confirm for yourself.
* Just to be clear, the standard does not guarantee this is the case for all platforms -- so the code you posted above is not portable, even if it does seem to work.