Skip to main content

Section 4.9 Finding and Understanding Functions

You can learn about what functions are available to use in a library by checking documentation of it (or by reading the code of the library). One useful source of documentation for C++ is cppreference.com. If you use the search function on that site to search for β€œsqrt”, you should find this page: https://en.cppreference.com/w/cpp/numeric/math/sqrt. On that page is the syntax for the function, the library (header file) you need to include, a description of the function and examples of how to use it.
The information on documentation pages can be a bit overwhelming. They include technical details that you probably don’t need to worry about. (Unless something goes wrong, in which case those details might explain the issue you have!) The most important part for our purpose is the listing of function prototypes at the top of the page. They look something like:
Listing 4.9.1.
float       sqrt ( float num );
double      sqrt ( double num );                    (until C++23)
long double sqrt ( long double num );
---------------------------------------------------------------------------
/* floating-point-type */                           (since C++23)
            sqrt ( /* floating-point-type */ num ); (constexpr since C++26)
...
Again, they can look overwhelming at first. The key is to focus on the information that is useful to us. We are writing code with doubles. The second line in the listing above shows that there is double sqrt ( double num );. There are also versions that work with floats and long doubles, but we are not concerned with those (see SectionΒ 4.6 ).
Once we find the right listing, we just need to figure out what it says. Something like double sqrt ( double num ); is known as a function prototype. A function prototype generally consists of three parts: returnType functionName ( paramters )
returnType
First is listed the type of data that will be returned by the function. sqrt will calculate and return a double.
functionName
Next is the name of the function. You call the square root function using the name sqrt .
parameters
Finally, in parentheses, a list of zero or more parameters separated by commas. Each parameter is described with a type and a name. In this case, sqrt takes one parameter, a double that is referred to as β€œnum”. To use this function, you have to pass it a double.
While using the function, you don’t actually need to know the name of the parameter. But as we will see soon, the name can help you figure out exactly what you are supposed to pass in.

Insight 4.9.1.

You can think of a function prototype as a contract between the caller and the function. The function promises that if the caller gives it the correct parameters (inputs), it will return a specific output.
Lets look at another prototype - this time we will look at the one for the pow function. The prototype for pow is double pow ( double base, double exponent );. (You can check it yourself at https://en.cppreference.com/w/cpp/numeric/math/pow). This tells us that:
  • The function returns a double to the caller
  • It is called β€œpow”
  • It takes two parameters, both of which are doubles and that the first parameter is the β€œbase” and the second is the β€œexponent”.
    Here, having the names is important. They help me figure out which value I need to pass first. If I want to calculate \(2^5\text{,}\) I need to call pow(2, 5). If I called pow(5, 2) I would be asking for \(5^2\)
To call this function, I will need to provide two doubles. When there is more than one parameter to pass, we use commas to separate them. So to calculate \(2.5^3\) I could do:
Again, note that the pow function does not change any parameters. x remains unmodified after being passed to pow. Also, notice that we can pass the integer 3 to the function even though it expects a double. That is because an integer can always safely be converted to a double.

Checkpoint 4.9.1.

You have attempted of activities on this page.