Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited...

26
Functions and Recursion: C++

Transcript of Functions and Recursion: C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_05.pdflimited...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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!