Function Calls
- Functions are small bits of code that can be called anywhere in the code.
- Function usually solve small problems.
- They can take input
- They can produce output
- The math library provides a set of functions
- Let's look at the square root function.
- reference
- First, we see that it has changed over the years.
- C++11 and beyond it needs
#include < cmath>
- It takes a single parameter or input value.
- It returns a single value.
- Note a function prototype
-
double sqrt (double x);
- Lists the return type
- The function name.
- At least the types of any parameters or arguments.
- The standard library supplies a large number of functions.
- If you need a function, searching is probably the best way to find it.
- Beware you might need to add a library for some.
- a list
- Memorizing all of the math functions is not a good idea.
- Our author discusses several functions
-
int abs(int)
-
float fabs(float)
-
double cos(double)
- (but float, long double, ...
- the angle is expected to be in radians
- But
M_PI
is a constant from cmath
-
float pow(float base, float exponent)
- Eventually we will write our own functions.
- /usr/include/math.h has additional constants.
- We will need
M_E
for the next problem.
- An approximation for n! is $e^{-n}n^n\sqrt{2\pi n}$.
- Compute 15! directly and using this approximation.
- This is problem 3 from page 134