1-PredefinedFunctionsx 439KB Jan 19 2015 08:16:45 PM

Download Report

Transcript 1-PredefinedFunctionsx 439KB Jan 19 2015 08:16:45 PM

Functions & Libraries
Introduction
• Functions
– Modules of code
• Uses
– Abstraction
• Build complex tasks with simpler ones
• Don't worry about details at lower level
– Reuse
• Avoid rewriting code
Introduction
• 4 things you need to know:
1. Name
2. What information it needs
• 0+ items
3. What answer it produces
• 0-1 items
4. What it does
Introduction
• 4 things you need to know:
1.
2.
3.
4.
Name
What information it needs
What answer it produces
What it does
• Function prototype specifies everything but job
– Get job from: Name, comments, docs
Function Heading
• Prototype for an absolute value function
– Called myAbsoluteValue
– Needs an int (will be called number)
– Type is int (gives us an integer answer)
Function Heading
• Heading for a function
– Called larger
– Needs two doubles (will be called x & y)
– Type is double (gives us a double answer)
Using Function
• Using a function
– Call its name
– Provide actual parameters
• Values to use for formal parameters
• Must be correct type
• May be values or expressions
– Function evaluates to value returned
Ex:
int num = myAbsoluteValue(-12);
int bad = myAbsoluteValue(12.2);
//oops need int
int xDist = myAbsoluteValue(x2 – x1);
//ok if x2 – x1 is int
Using Function
• Using a function
– Call its name
– Provide actual parameters
• Values to use for formal parameters
• Must be correct type
• May be values or expressions
– Function evaluates to value returned
Ex:
double biggest = larger(12.2, 4.5);
double big = larger(10, 2);
//ok implicit cast from int to double
Functions on Variables
• Calling a math function with a variable does
not modify variable
a
double a = 3;
double b = pow(a, 2);
3.0
Functions on Variables
• Calling a math function with a variable does
not modify variable
a
double a = 3;
double b = pow(a, 2);
3.0
b
9.0
Functions on Variables
• This does nothing:
double a = 3;
pow(a, 2);
//no use of answer
Function Composition
• Can compose funcitons
– Order of evaluation of parameters unspecified
Ex:
double x = larger( larger(2, 4),
larger(3, 1) );
Function Composition
• Result of a function call can be actual
parameter
– Order of evaluation of parameters unspecified
Ex:
double x = larger( larger(2, 4),
larger(3, 1) );
Function Composition
• Result of a function call can be actual
parameter
– Order of evaluation of parameters unspecified
Ex:
double x = larger( larger(2, 4),
3.0);
Function Composition
• Result of a function call can be actual
parameter
– Order of evaluation of parameters unspecified
Ex:
double x = larger(4.0,
3.0);
Function Composition
• Result of a function call can be actual
parameter
– Order of evaluation of parameters unspecified
Ex:
double x = 4.0;
Function Libraries
• Standard Libraries:
– Your new best friend:
http://www.cplusplus.com/reference/clibrary/
<cmath>
– Math functions
• cos, sin, pow
<ctime>
– Time/timing
<iostream>
– Console Input/Output
• cout , cin, endl
<cstdlib>
– Random
Radians
• Radians : alternative angle measurement
Essentials
• Trig functions:
Function
Description
sin(radians)
Returns the trigonometric sine of an angle in radians.
cos(radians)
Returns the trigonometric cosine of an angle in radians.
tan(radians)
Returns the trigonometric tangent of an angle in radians.
asin(a)
Returns the angle in radians for the inverse of sine.
acos(a)
Returns the angle in radians for the inverse of cosine.
atan(a)
Returns the angle in radians for the inverse of tangent.
• Radians  Degrees : radians * 180/PI
• Degrees  Radians : degrees * PI/180
Constants
• Constants not a part of
C/C++ standard
• Most compilers include
these with cmath 
• For gcc must
#define _USE_MATH_DEFINES
before:
#include <cmath>
• Does not work in
standards mode:
Must define own
constants
Symbol
M_E
Expression Value
e
2.71828182845904523536
M_LOG2E
log2(e)
1.44269504088896340736
M_LOG10E
log10(e)
0.434294481903251827651
M_LN2
ln(2)
0.693147180559945309417
M_LN10
ln(10)
2.30258509299404568402
M_PI
pi
3.14159265358979323846
M_PI_2
pi/2
1.57079632679489661923
M_PI_4
pi/4
0.785398163397448309616
M_1_PI
1/pi
0.318309886183790671538
M_2_PI
2/pi
0.636619772367581343076
M_2_SQRTPI 2/sqrt(pi)
1.12837916709551257390
M_SQRT2
1.41421356237309504880
sqrt(2)
M_SQRT1_2 1/sqrt(2)
0.707106781186547524401
Essentials
• Exponentials functions:
Function
Description
exp(x)
Returns e raised to power of x (ex).
log(x)
Returns the natural logarithm of x (ln(x) = loge(x)).
log10(x)
Returns the base 10 logarithm of x (log10(x)).
pow(a, b)
Returns a raised to the power of b (ab).
sqrt(x)
Returns the square root of x (
x
) for x >= 0.
Essentials
• Rounding/Comparison:
Function
Description
ceil(x)
x is rounded up to its nearest integer. returned as double
floor(x)
x is rounded down to its nearest integer. returned as double
abs(x)
x is made positive
min(a, b)
returns smaller of a or b
max(a, b)
returns larger of a or b
Special Values
• Floating points have special values for:
– infinity : inf
– not a number : nan
Special Values
• Double constants : INFINITY, NAN
• NAN never == NAN
– Use isnan() to check:
Case Study: Computing Angles of
a Triangle
x2, y2
c
A = acos((a * a - b * b - c * c) / (-2 * b * c))
B = acos((b * b - a * a - c * c) / (-2 * a * c))
C = acos((c * c - b * b - a * a) / (-2 * a * b))
a
B
C
x3, y3
A
b
x1, y1
Write a program that prompts the user to enter
the x- and y-coordinates of the three corner points
in a triangle and then displays the triangle’s angles.