Functions
You encountered functions when you studied algebra. In general, you use a function in an expression by writing the function name followed by the arguments, in parentheses. The parentheses are required. In algebra, functions typically have names that are a single letter. C++ supports functions, but their names can have any length. Each argument of a function can be any expression. For example, the value of expression sqrt(9.0 + 16.0) is approximately 5.0. If there are two or more arguments, separate them by commas. For example, function pow(x, y) produces xy C++ expression pow(2+3, 4/2) has value 25.0. |
Evaluating expressions
Evaluate an expression from the inside out. For example, if f (x) = x + 1, then expression f (3+2) + 1 has value 7, computed as follows.
and
|
Terminology: calling a function
An expression that uses a function is referred to as a function call, and we say that expression sqrt(2.0) calls sqrt. |
Nested function calls
The argument of a function can be any expression, including an expression that uses a function. For example, if f (x) = x + 1, then
and if g(x) = 2*x + 4, then
|
Arguments/parameters
If E is an expression and f is a function, then we say that E is the argument of f in expression f (E). I will use term parameter interchangeably with argument. |
double q = sqrt(36.0);declares variable q and stores 6.0 into q.
Suppose that g(x) = 3x − 2. What is the value of expression g(g(9) + 1)? Answer
Suppose that g(x) = 3x − 2. What is the value of expression g(g(9 + 1))? Answer
Suppose that g(x) = 3x − 2. What is the value of expression g(g(9)) + 1? Answer