Up until now, we have only used the basic arithmetic operators. But what if you want to do calculate something more complex like \(x^3\) or \(asin(x)\text{?}\) You could figure out how to to do the math using basic operations and then write that code. Calculating \(x^3\) would not be hard - just multiply three copies of \(x\) together. But calculating a good approximation of something like \(asin(x)\) would be complex and laborious. It would be silly for every programmer to have to figure out how to perform complex math calculations from basic operations. And it would be painful to write that code each time a programmer wanted to do the math.
Fortunately, C++ allows us to define functions that give a name to a series of statements. We can call a function using that name and it will perform the work defined by those statements.
The idea of a function in programming is similar to the idea of a function in math. A mathematical function takes some input and produces an output based on that input. You may have seen this idea illustrated via a βfunction boxβ - a visualization of a function as a black box (one we canβt see into) that transforms inputs:
Figure4.8.1.A function box transforms inputs into outputs. This function could be \(f(x) = 2x + 5\) or \(f(x) = x + 15\) or anything else that would produce 25 given input of 10.
The notion of a function as a black box is an important one. Functions provide an abstraction that hides the details of how a job is done. I donβt have to know the details of how to calculate a square root, I just have to know that there is a function I can feed my number into and it will produce the value as its output. Someone had to figure out how to do that calculation and write code for it. But that code is hidden from me, the programmer who is using the function.
Part of what βcomesβ with C++ is a set of function libraries that provide functions to accomplish basic tasks. The cmath library provides a number of functions that perform common mathematical operations. One function it provides is the sqrt function, which calculates the square root of a number. To use this function, we need to include the library that contains it (cmath) and then call the function with the number we want to take the square root of. Here is an example that calculates the square root of 17:
We pass the input to a function by putting values into parentheses after the function name. Here, x is the value we are passing to the sqrt function to tell it what value to take the square root of. This value that we pass is known as a parameter for the function.
Many (but not all) functions produce an output, which is known as the return value. We have to do something with that value, like store it in a variable or print it or use it as part of a larger calculation. Here, we assign the result of the function to the variable root.
A subtle point: note that x still has the value 17. In general functions do not change their parameter(s). Taking the square root of x with sqrt(x) does not change x, if just produces a new value.
The βcβ in cmath refers to the C programming language that C++ was based on (and is still closely related to). In C, there is a similar library called math.h. The cmath library is a version of math.h that has been βupgradedβ to use C++ features. Although it is possible to use math.h in C++ code, you should always use cmath instead.