W2_OOP
W2_OOP
Programming
Functions
Program Components in C++
• Modules: functions and classes
• Programs use new and “prepackaged” modules
• New: programmer-defined functions, classes
• Prepackaged: from the standard library
• Functions invoked by function call
• Function name and information (arguments) it needs
• Function definitions
• Only written once
• Hidden from other functions
2
Functions
• Functions
• Modularize a program
• Software reusability
• Call function multiple times
• Local variables
• Known only in the function in which they are defined
• All variables declared in function definitions are local variables
• Parameters
• Local variables passed to function when called
• Provide outside information
3
Create a Function
Call a Function
Call a Function
Parameters
Local variables passed to function when called
Provide outside information
Parameters and Arguments
Default Parameter Value
Multiple Parameters
The Return Keyword
The Return Keyword
The Return Keyword
1. Function Templates
2. References and Reference Parameters
3. Pointers
4. Header Files