Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions & Libraries. Introduction Functions – Modules of code Uses – Abstraction Build complex tasks with simpler ones Don't worry about details at.

Similar presentations


Presentation on theme: "Functions & Libraries. Introduction Functions – Modules of code Uses – Abstraction Build complex tasks with simpler ones Don't worry about details at."— Presentation transcript:

1 Functions & Libraries

2 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

3 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

4 Introduction 4 things you need to know: 1.Name 2.What information it needs 3.What answer it produces 4.What it does Function prototype specifies everything but job – Get job from: Name, comments, docs

5 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)

6 Function Heading Heading for a function – Called larger – Needs two doubles (will be called x & y) – Type is double (gives us a double answer)

7 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

8 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

9 Functions on Variables Calling a math function with a variable does not modify variable double a = 3; double b = pow(a, 2); a 3.0

10 Functions on Variables Calling a math function with a variable does not modify variable double a = 3; double b = pow(a, 2); a 3.0 b 9.0

11 Functions on Variables This does nothing: double a = 3; pow(a, 2); //no use of answer

12 Function Composition Can compose funcitons – Order of evaluation of parameters unspecified Ex: double x = larger( larger(2, 4), larger(3, 1) );

13 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) );

14 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);

15 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);

16 Function Composition Result of a function call can be actual parameter – Order of evaluation of parameters unspecified Ex: double x = 4.0;

17 Function Libraries Standard Libraries: – Your new best friend: http://www.cplusplus.com/reference/clibrary/ http://www.cplusplus.com/reference/clibrary/ – Math functions cos, sin, pow – Console Input/Output cout, cin, endl – Time/timing – Random

18 Radians Radians : alternative angle measurement

19 Essentials Trig functions: Radians  Degrees : radians * 180/PI Degrees  Radians : degrees * PI/180

20 Constants Constants not a part of C/C++ standard Most compilers include these with cmath  For gcc must #define _USE_MATH_DEFINES before: #include Does not work in standards mode: Must define own constants Symbol Expression Value M_Ee 2.71828182845904523536 M_LOG2Elog2(e) 1.44269504088896340736 M_LOG10Elog10(e) 0.434294481903251827651 M_LN2ln(2) 0.693147180559945309417 M_LN10ln(10) 2.30258509299404568402 M_PIpi 3.14159265358979323846 M_PI_2pi/2 1.57079632679489661923 M_PI_4pi/4 0.785398163397448309616 M_1_PI1/pi 0.318309886183790671538 M_2_PI2/pi 0.636619772367581343076 M_2_SQRTPI2/sqrt(pi) 1.12837916709551257390 M_SQRT2sqrt(2) 1.41421356237309504880 M_SQRT1_21/sqrt(2) 0.707106781186547524401

21 Essentials Exponentials functions:

22 Essentials Rounding/Comparison:

23 Special Values Floating points have special values for: – infinity : inf – not a number : nan

24 Special Values Double constants : INFINITY, NAN NAN never == NAN – Use isnan() to check:

25 Case Study: Computing Angles of a Triangle 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.


Download ppt "Functions & Libraries. Introduction Functions – Modules of code Uses – Abstraction Build complex tasks with simpler ones Don't worry about details at."

Similar presentations


Ads by Google