Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited...
Transcript of Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited...
Functions and Recursion: C++
Program Components
Boss function/worker function relationship
Function and Classes: New and prepackaged
Math Library Functions
• Global Functions: not member of Classes
– No need to create object
• <cmath> header file pow(2,3), sqrt(900.0)
Math Library Functions
Function Prototypes and Argument Coercion
Promotion Hierarchy
C++ Library Header Files
C++ Library Header Files
C++ Library Header Files
Case Study: Random Number Generation
Case Study: Random Number Generation
Case Study: Random Number Generation
Case Study: Random Number Generation
Case Study: Random Number Generation
Storage Classes
• Identifier for variable names (even for functions)
• Attributes of variables: name, type, size and value
• Other attributes: storage class, scope, and linkage
• An identifier’s storage class determines the period during which that identifier exists inmemory. Some exist briefly, some are repeatedly created and destroyed and others exist for the entire execution of a program.
• An identifier’s scope is where the identifier can be referenced in a program. Some identifiers can be referenced throughout a program; others can be referenced from only limited portions of a program
• An identifier’s linkage determines whether it’s known only in the source file where it’s declared or across multiple files that are compiled, then linked together. An identifier’s storage-class specifiers helps determine its storage class and linkage.
Storage Classes
• Storage class specifiers: auto, register, extern, mutable, and static
– Automatic: auto and register
– Static: extern and static
• Auto and register: Such variables are created when program execution enters the block in which they’re defined, they exist while the block is active and they’re destroyed when the program exits the block
• Only local variables of a function can be of automatic storage class. A function’s local variables and parameters normally are of automatic storage class. The storage class specifier auto explicitly declares variables of automatic storage class.
• Data in the machine-language version of a program is normally loaded into registers for calculations and other processing.
Automatic variables
Storage Classes
• Static-storage-class variables exist from the point at which the program begins execution and last for the duration of the program.
• Even though the variables and the function names exist from the start of program execution, this does not mean that these identifiers can be used throughout the program. Storage class and scope (where a name can be used) are separate issues.
• extern: global functions/variables
• Static: local variables (??? Difference automatic variables)
Storage Classes
ScopeStorage
Scope Rules• The portion of the program where an identifier can be used is known as its
scope.
• Function Scope: Identifiers declared in the outermost block of a function have function scope. They can be accessed only in the function that declares them.
• File Scope (global namespace scope): Identifiers declared outside all blocks and functions have file scope. It can be used in all the blocks and functions written inside the file in which the variable declaration appears (Global variables/Function definitions/Function prototypes placed outside all functions)
• Local/Block Scope: Blocks are portions of C++ code contained within curly braces( {....} ).
• Function-prototype Scope: Variables appearing in the parameter lists of function prototypes have function - prototype scope.
Later: Class and namespace scope
Understanding Scoping
Understanding Scoping
Inline Functions• Inline qualifier: advises the compiler to generate a copy of the function’s
code in place (when appropriate) to avoid a function call.
Pass-by-valuePass-by-reference
Unary Scope Resolution Operator
• C++ provides the unary scope resolution operator (::) to access a global variable when a local variable of the same name is in scope.
• The unary scope resolution operator cannot be used to access a local variable of the same name in an outer block.
Function Overloading• C++ enables several functions of the same name to be defined, as long as they have different
signatures (examining the number, types and order of the arguments in the call)
• Function overloading is used to create several functions of the same name that perform similar tasks, but on different data types.
Homework 1
Three Parts (see the uploaded file for Homework 1)
1. Part 1: Briefly Answer the given Questions
2. Part 2: Simulate the Game of Chance
3. Part 3: A simple program to define a class
Date of Submission: March 30, 2017.
Submit PDF file!