why are functions for finding square written as
instead ofCode:double square(double x){ return x*x; }
Code:int square(int x){ return x*x; }
why are functions for finding square written as
instead ofCode:double square(double x){ return x*x; }
Code:int square(int x){ return x*x; }
I guess because some folks want to square floating point numbers. I love easy questions!
yeah maybe but according to me, the latter also works fine.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
I think that your question makes an incorrect assumption to begin with: there is no function named square in the C++ standard library. There are examples of functions that coincidentally are named square, e.g.,Originally Posted by V8cTor
And lo and behold, the example is of your latter variety, so in fact "functions for finding square" are written like that! Yet this is just an example. You can define your function named square as you see fit. You can even overload:Originally Posted by C++11 Clause 7.1.5 Paragraph 1 (part)
Or perhaps define a function template:Code:double square(double x) { return x * x; } int square(int x) { return x * x; }
Code:template<typename T> T square(T x) { return x * x; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)