Slide 11 Advantages of functions separate the concept(what is done) from the implementation how it is done) make programs easier to understand make programs easier to modify can be called several times in the same program, allowing the code to be reused
Slide 11 Advantages of Functions separate the concept (what is done) from the implementation (how it is done). make programs easier to understand. make programs easier to modify. can be called several times in the same program, allowing the code to be reused
Slide 12 Function Input and Output Function Output esults Input parameters
Slide 12 Function Input and Output Function Input parameters Output results
Slide 13 C++ Functions C++ allows the use of both pre defined and user-defined functions Predefined functions(e.g, cin, cout, rand, etc. ) are usually grouped into specialized libraries (e.g, iostream, cstdlib, cmath, etc
Slide 13 C++ Functions C++ allows the use of both predefined and user-defined functions. Predefined functions (e.g., cin, cout, rand, etc.) are usually grouped into specialized libraries (e.g., iostream, cstdlib, cmath, etc.)
Slide 14 Mathematical functions #include <cmath> double log(double x) natural logarithm double log 10(double x base 10 logarithm a double exp(double x) e to the power x double pow(double x, double y x to the power y double sqrt(double x) positive square root of X double ceil(double x) smallest integer not less than x double floor(double x) largest integer not greater than x double sin(double x, cos(double x), tan(double x),etc
Slide 14 Mathematical Functions #include <cmath> double log(double x) natural logarithm double log10(double x) base 10 logarithm double exp(double x) e to the power x double pow(double x, double y) x to the power y double sqrt(double x) positive square root of x double ceil(double x) smallest integer not less than x double floor(double x) largest integer not greater than x double sin(double x), cos(double x), tan(double x), etc
Slide 15 How to use a pre-defined function Distance between two points Output: distance Input: two points:(xl, y1),and (x2,y2) Distance sqrt((x2-x1)*(x2-X1)+(y2 y1)*(y2-y1)) xl,yl, x2, y2 are input data, so we are done
Slide 15 How to use a pre-defined function: Distance Between Two Points Output:distance Input: two points: (x1,y1), and (x2,y2) Distance = sqrt((x2-x1)*(x2-x1) + (y2- y1)*(y2-y1)) x1,y1, x2, y2 are input data, so we are done!