Presentation is loading. Please wait.

Presentation is loading. Please wait.

USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows.

Similar presentations


Presentation on theme: "USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows."— Presentation transcript:

1 USING & CREATING FUNCTIONS

2 MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows For Re-Usability  Within Program  From Program To Program  Top-Down Approach  Break Task Down Into Smaller & Smaller Processes  Stop When A Process Will Not Break Down Further  Write Functions For Lowest Level Processes

3 PREDEFINED FUNCTIONS  Similar To Calculator Functions  Input Value (Parameter)  Press Function Key (Call Function)  Read Result (Use Function Output)  Functions Organized In Libraries  # include  Appendix F Shows Various Libraries And Functions

4 PREDEFINED FUNCTIONS  Example math (cmath) functions  ceil(x) - returns the smallest whole number value that is not less than x  abs(x) - returns the absolute value of integer x  exp(x) - returns the natural logarithm of x  floor(x) - returns the largest whole number value that is not greater than x  sin(x) - returns the sine of x  sqrt(x) - returns the square root of x  cos(x) - returns the cosine of x. Note: In all of the above cmath functions, the input paramter, x, is defined as a floating point value.

5 PREDEFINED FUNCTIONS  Example character (cctype) functions  isalnum(ch) - returns true if the char is an ASCII alphabetic or digit character ('a' - 'z', 'A' - 'Z', or '0' - '9')  char(int) - returns the ASCII character associated with the given number  isdigit(ch) - returns true if the char is an ASCII digit ('0' - '9')  isspace(ch) - returns true if the char is a whitespace character (blank, newline, tab, return)  tolower(ch) - returns the lowercase ASCII equivalent of the given char if it is an uppercase character. Otherwise returns the given char unchanged.  toupper(ch) - returns the uppercase ASCII equivalent of the given char if it is a lowercase character. Otherwise returns the given char unchanged.

6 PREDEFINED FUNCTIONS  Four Ways To Use Functions  Direct Output cout << “The value “ << x << “ raised to the power of “ << y; cout << “ is “ << pow(x,y) << endl;  Assignment Statement temp = pow(x,y); z = z + pow(x,y);  Function Call Parameter temp = pow(x,pow(y,z));  Relational Expression if (pow(x,y) > 15)

7 USER DEFINED FUNCTIONS  Three Categories  Void Function Without Parameters  Void Function With Parameters  Value-Returning Function (with or without parameters)  Void Function  May or May Not Have Parameters  Does Not Return A Value ( unless reference parameters are used )  Value-Returning Function  Typically Has Parameters  Parameters Declared In Function Header  Returns Single Value Of Function Return Type

8 USER DEFINED FUNCTIONS  Void Function Without Parameters functionType functionName() { // Function May Have Local Constants // Function May Have Local Variables statement(s); }  Void Function Call functionName();

9 USER DEFINED FUNCTIONS  Void Function With Parameters void functionName(formal parameter list) { // Function May Have Local Constants // Function May Have Local Variables statement(s) }  Void Function Call functionName(actual parameter list)

10 USER DEFINED FUNCTIONS  Void Function With Parameters (cont.)  Formal Parameter List (in function header) (datatype variable, datatype variable)  Actual Parameter List (in function call) (expression or variable, expression or variable)

11 USER DEFINED FUNCTIONS  Value Returning Function return type functionName(formal parameter list) { // Function May Have Local Constants // Function May Have Local Variables statement(s) return variable; }  Function Call functionName(actual parameter list);

12 USER DEFINED FUNCTIONS  Value Returning Function (cont.)  Formal Parameter List (in function header) (datatype variable, datatype variable)  Actual Parameter List (in function call) (expression or variable, expression or variable)  Return Statement Used To Pass Value Back To Function Call

13 USER DEFINED FUNCTIONS  Where Are They Located In Program?  Can Be Listed Before Or After main()  Most Programmers List After main()  If Listed Before main() Creates Issues  Function Prototypes Eliminates Issues  Function Prototypes Listed Prior To main() functionType functionName(parameter list);  Parameter List Variable Names Are Optional double larger(double x, double y); or double larger(double, double);


Download ppt "USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows."

Similar presentations


Ads by Google