Tags: %TAGME{ tpaction="" web="SCMP118/Fall2016" tag="" }% view all tags

Notes on Sections 4.2 & 4.3


  • Function

  • Argument

  • Value returned

  • Function call

  • Type cast

  • Function declaration

  • Formal parameter

  • Function definition

  • Function header

  • Function body

  • Return statement

  • call-by-value


  • #include<cmath>

  • abs(-3);

  • log(2.71828)

  • exp(2.71828)

  • exp(3);

  • static_cast<double>(age);

Take Note

  • Most functions will take only the type of argument they are defined for.

  • The very common exception to the above is most arithmetic functions, which will accept arguments of type int, but will convert them to type double before evaluating.

  • The first argument to the function pow must be positive.

  • Complex arguments will be evaluated before functions are applied, so log(9/5) will return the natural logarithm of 1, which is 0. Similarly, static_cast<double>(9/5) will return 1.0, not 1.8.

  • A common mistake is passing arguments to a function in the wrong order. For example, if you have defined a function to evaluate cost with inflation as

double eventual_cost(double init_cost, double infl_rate, int num_years)

and you end up using the following command in your program:

eventual_cost (rate,cost,time);

your program will think that the first number is the cost and the second number is the inflation rate, since that is the order in which it is expecting inputs.

Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2015-09-17 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback