Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 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 any expression 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.
You can also take the result of one function and pass it as an argument to another:
Listing 3.4.1. This program finds the log base e of 10 and raises e to that power. The result of this computation is assigned to x.
Checkpoint 3.4.1 .
Which of these statements has proper syntax?
log6
is not a built in cmath function, but you could write an implementation for it if you wanted!
double val = abs(tan(1.57));
This correctly uses cmath functions!
double num = exp(cosine(0.86667));
cosine
is not a built in cmath function, but cos
is!
double y = exp(cos(1.047)) + exp(tan(2.094))
This would be correct if it ended in a semi-colon.
Checkpoint 3.4.2 .
Which of these statements returns the y-component of the unit vector at 330 degrees?
You must always convert to radians before using sinusoidal functions.
y = cos(330 * 2 * pi / 360);
cos
will return the x-component.
You must always convert to radians before using sinusoidal functions.
y = sin(330 * 2 * pi / 360);
sin
returns the y-component, cos
returns the x-component.
y = tan(330 * 2 * pi / 360);
tan
is not the proper function to use here.
You have attempted
of
activities on this page.