3.4. CompositionΒΆ

Just as with mathematical functions, C++ functions can be composed, meaning that you use one expression as part of another. For example, you can use expressions as an argument to a function:

double x = cos (angle + pi / 2);

This statement takes the value of pi, divides it by two and adds the result to the value of angle. The sum is then passed as an argument to the cos function. Note that the order of operations that apply to expressions also apply when used in composition. Composition does not change the nature of an expression.

You can also take the result of one function and pass it as an argument to another:

This program finds the log base e of 10 and raises e to that power. The result of this computation is assigned to x.

Composition of Math Functions
1#include <cmath>
2#include <iostream>
3
4int main () {
5    double x = exp (log (10.0));
6    std::cout << x;
7}

Q1

Which of these statements has proper syntax?

Q2

Which of these statements returns the y-component of the unit vector at 330 degrees?


More to Explore