functions and recursion: c++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited...
TRANSCRIPT
![Page 1: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/1.jpg)
Functions and Recursion: C++
![Page 2: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/2.jpg)
Program Components
Boss function/worker function relationship
Function and Classes: New and prepackaged
![Page 3: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/3.jpg)
Math Library Functions
• Global Functions: not member of Classes
– No need to create object
• <cmath> header file pow(2,3), sqrt(900.0)
![Page 4: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/4.jpg)
Math Library Functions
![Page 5: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/5.jpg)
Function Prototypes and Argument Coercion
![Page 6: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/6.jpg)
Promotion Hierarchy
![Page 7: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/7.jpg)
C++ Library Header Files
![Page 8: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/8.jpg)
C++ Library Header Files
![Page 9: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/9.jpg)
C++ Library Header Files
![Page 10: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/10.jpg)
Case Study: Random Number Generation
![Page 11: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/11.jpg)
Case Study: Random Number Generation
![Page 12: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/12.jpg)
Case Study: Random Number Generation
![Page 13: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/13.jpg)
Case Study: Random Number Generation
![Page 14: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/14.jpg)
Case Study: Random Number Generation
![Page 15: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/15.jpg)
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.
![Page 16: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/16.jpg)
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
![Page 17: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/17.jpg)
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)
![Page 18: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/18.jpg)
Storage Classes
ScopeStorage
![Page 19: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/19.jpg)
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
![Page 20: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/20.jpg)
Understanding Scoping
![Page 21: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/21.jpg)
Understanding Scoping
![Page 22: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/22.jpg)
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.
![Page 23: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/23.jpg)
Pass-by-valuePass-by-reference
![Page 24: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/24.jpg)
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.
![Page 25: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/25.jpg)
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.
![Page 26: Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited portions of a program • An identifier’s linkagedetermines whether it’s known](https://reader033.vdocuments.mx/reader033/viewer/2022042112/5e8e293ba3d3fb09d7605c76/html5/thumbnails/26.jpg)
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!