WPU CS 2300 - Modulatrity Using Functions

Unformatted text preview:

Slide 1ObjectivesFunction and Parameter DeclarationsFunction and Parameter Declarations (continued)Slide 5Slide 6Slide 7Function PrototypesFunction Prototypes (continued)Calling a FunctionCalling a Function (continued)Defining a FunctionDefining a Function (continued)Slide 14Slide 15Slide 16Placement of StatementsPlacement of Statements (continued)Function StubsFunctions with Empty Parameter ListsDefault ArgumentsReusing Function Names - OverloadingReusing Function Names - Overloading (continued)Slide 24Function TemplatesFunction Templates (continued)Slide 27Returning a Single ValueReturning a Single Value (continued)Inline FunctionsInline Functions (continued)Slide 32Pass by ReferencePassing and Using Reference ParametersVariable ScopeScope Resolution OperatorMisuse of GlobalsVariable Storage ClassLocal Variable Storage ClassesLocal Variable Storage Classes (continued)Slide 41Slide 42Global Variable Storage ClassesCommon Programming ErrorsCommon Programming Errors (continued)SummarySummary (continued)Slide 48A First Book of C++: From Here To There, Third Edition 2ObjectivesYou should be able to describe:•Function and Parameter Declarations•Returning a Single Value•Pass by Reference•Variable Scope•Variable Storage Class•Common Programming ErrorsA First Book of C++: From Here To There, Third Edition 3Function and Parameter Declarations•All C++ programs must contain a main() function–May also contain unlimited additional functions•Major programming concerns when creating functions: –How does a function interact with other functions (including main)?–Correctly passing data to function–Correctly returning values from a functionA First Book of C++: From Here To There, Third Edition 4Function and Parameter Declarations (continued)•Function call process: –Give function name–Pass data to function as arguments in parentheses following function name•Only after called function successfully receives data passed to it can the data be manipulated within the functionA First Book of C++: From Here To There, Third Edition 5Function and Parameter Declarations (continued)A First Book of C++: From Here To There, Third Edition 6Function and Parameter Declarations (continued)A First Book of C++: From Here To There, Third Edition 7Function and Parameter Declarations (continued)•Program 6.1 not complete–findMax() must be written and added •Done in slide 15•Complete program components:–main(): referred to as calling program–findMax(): referred to as called program •Complete program can be compiled and executedA First Book of C++: From Here To There, Third Edition 8Function Prototypes•Function Prototype: declaration statement for a function–Before a function can be called, it must be declared to the calling function–Tells the calling function:•The type of value to be returned, if any•The data type and order of values transmitted to the called function by the calling functionA First Book of C++: From Here To There, Third Edition 9Function Prototypes (continued)•Example: the function prototype in Program 6.1void findMax(int, int);–Declares that findMax() expects two integer values sent to it–findMax() returns no value (void)•Prototype statement placement options:–Together with variable declaration statements just above calling function name (as in Program 6.1)–In a separate header file to be included using a #include preprocessor statementA First Book of C++: From Here To There, Third Edition 10Calling a FunctionA First Book of C++: From Here To There, Third Edition 11Calling a Function (continued)A First Book of C++: From Here To There, Third Edition 12Defining a Function•A function is defined when it is written–Can then be used by any other function that suitably declares it•Format: two parts–Function header identifies: •Data type returned by the function•Function name•Number, order and type of arguments expected by the function–Function body: statements that operate on data•Returns one value back to the calling functionA First Book of C++: From Here To There, Third Edition 13Defining a Function (continued)A First Book of C++: From Here To There, Third Edition 14Defining a Function (continued)A First Book of C++: From Here To There, Third Edition 15Defining a Function (continued)findMax() function definition (from program 6.1)void findMax (int x, int y){ // start of function body int maxnum; // variable declaration if (x >= y) // find the maximum number maxnum = x; else maxnum = y; cout << "\nThe maximum of the two numbers is " <<maxnum<< endl;} // end of function body and end of functionA First Book of C++: From Here To There, Third Edition 16Defining a Function (continued)•Order of functions in a program:–Any order is allowed–main() usually first•main() is the driver function•Gives reader overall program concept before details of each function encountered•Each function defined outside any other function–Each function separate and independent–No nesting of function definitions allowedA First Book of C++: From Here To There, Third Edition 17Placement of Statements•Requirement: items that must be either declared or defined before they are used:•Preprocessor directives•Named constants•Variables•Functions•Otherwise, C++ is flexible in requirements for ordering of statementsA First Book of C++: From Here To There, Third Edition 18Placement of Statements (continued)•Recommended ordering of statements–Good programming practicepreprocessor directivesfunction prototypesint main(){symbolic constantsvariable declarationsother executable statementsreturn value}function definitionsA First Book of C++: From Here To There, Third Edition 19Function Stubs•Possible programming approach:–Write main() first and add functions as developed–Program cannot b run until all functions are included•Stub: beginning of a final function–Can be used as a placeholder for a function until the function is completed–A “fake” function that accepts parameters and returns values in proper form–Allows main to be compiled and tested before all functions are completedA First Book of C++: From Here To There, Third Edition 20Functions with Empty Parameter Lists•Extremely limited use•Prototype format:int display ();Int display (void);•Information provided in above prototypes:–display takes no parameters–display returns an integerA First Book of C++: From Here


View Full Document

WPU CS 2300 - Modulatrity Using Functions

Download Modulatrity Using Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Modulatrity Using Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Modulatrity Using Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?