More Related Content
Similar to Lesson 9.2 guessing the game program
Similar to Lesson 9.2 guessing the game program (20)
More from MLG College of Learning, Inc
More from MLG College of Learning, Inc (20)
Lesson 9.2 guessing the game program
- 1. © 2016 Cengage Learning®. May not be scanned, copied or
Introduction to Programming in C++
Eighth Edition
Lesson 9.2:
Guessing the Game Program
duplicated, or posted to a publicly accessible website, in whole
or in part.
- 2. • Program generates arandom number from 1 to 10and
then allows the user asmany choices asneeded to
guessthe number.
• Thesrand, time, and randfunctions are all utilized
in the program.
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
2An Introduction to Programming with C++, Eighth Edition
The Guessing Game Program
- 3. The Guessing Game Program(cont’d.)
Figure 9-10 Problem specification, IPO chart information, and C++
instructions for the Guessing Game Program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
3An Introduction to Programming with C++, Eighth Edition
- 4. The Guessing Game Program(cont’d.)
Figure 9-11 Guessing Game Program and sample run
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
4An Introduction to Programming with C++, Eighth Edition
- 5. • Aprogram-defined value-returning function definition
is composed of aheader and abody
• Header (first line) contains return data type,name of
function, and an optional parameterList
– Rules for function namesare sameasfor variables
– Goodidea to usemeaningful namesthat describe
function’s purpose
– Memory locations in parameterList are calledformal
parameters
• Eachstores an item of information passedtothe function
when it iscalled
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
5An Introduction to Programming with C++, Eighth Edition
Creating Program-Defined Value-
Returning Functions
- 6. • Function body contains instructions for performingthe
function’s assigned task
• Surrounded by braces({})
• Laststatement is usually the return statement
– Returns one value (must match return data typein
function header)
• After returnstatement is processed, program
execution continues in callingfunction
• Good idea to include comment (such as//end of
functionName) to mark end offunction
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
6An Introduction to Programming with C++, Eighth Edition
Creating Program-Defined Value-
Returning Functions (cont’d.)
- 7. Figure 9-12 How to create a program-defined value-returning function
Creating Program-Defined Value-
Returning Functions (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
7An Introduction to Programming with C++, Eighth Edition
- 8. Figure 9-12 How to create a program-defined value-returning function
Creating Program-Defined Value-Returning
Functions (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
8An Introduction to Programming with C++, Eighth Edition
- 9. • Afunction must be called (invoked) to perform itstask
• mainis automatically called when program is run
• Other functions must be called by astatement
• Syntax for calling afunction:
functionName([argumentList]);
– argumentList is list of actual arguments (ifany)
– Anactual argument canbe avariable, named constant,
literal constant, orkeyword
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
9An Introduction to Programming with C++, Eighth Edition
Calling a Function
- 10. • Value-returning functions are typically calledfrom
statements that:
– Assignthe return value to avariable
– Usethe return value in acalculation orcomparison
– Display the return value
• Acall to avoid function is an independent statement
because void functions do not returnvalues
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
10An Introduction to Programming with C++, Eighth Edition
Calling a Function (cont’d.)
- 11. • C++allows you to passeither avariable’s value or its
addressto afunction
• Passingavariable’s value is referred to aspassingbyvalue
• Passingavariable’s address is referred to aspassingby
reference
• Default is passing by value
• Number, data type, and ordering ofactual arguments must
match the formal parameters in functionheader
– Namesdo not need to match (differentnamesare better)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
11An Introduction to Programming with C++, Eighth Edition
Calling a Function (cont’d.)
- 12. Figure 9-13 How to call (invoke) a value-returning function
Calling a Function (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
12An Introduction to Programming with C++, Eighth Edition
- 13. Figure 9-14 Function call and function definition
Calling a Function (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
13An Introduction to Programming with C++, Eighth Edition
- 14. • Theprogram allows the user to enter the initial deposit
made into asavings account and the annual interest rate.
• Theprogram displays the amount of money in the
account at the end of1 through 3 years, assumingno
additional deposits or withdrawals aremade.
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
14An Introduction to Programming with C++, Eighth Edition
The Savings Account Program
- 15. The Savings Account Program
(cont’d.)
Figure 9-15 Problem specification, IPO chart information,
and C++ instructions for the Savings Account Program
© 2016 Cengage Learning®. May not be scanned, copied or 15An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole
or in part.
- 16. The Savings Account Program
(cont’d.)
Figure 9-15 Problem specification, IPO chart information,
and C++ instructions for the Savings Account Program
© 2016 Cengage Learning®. May not be scanned, copied or 16An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole
or in part.
- 17. Figure 9-16 Flowcharts for the Savings Account Program
The Savings Account Program
(cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 17
- 18. • When afunction definition appears below the main
function, you must enter afunction prototypeabove the
main function
• Afunction prototype is astatement that specifies the
function’s name, data type of its return value, and data
type of each of its formal parameters (if any)
– Namesfor the formal parameters are notrequired
• Programmers usually place function prototypes at
beginning of program, after the #includedirectives
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 18
Function Prototypes
- 19. Figure 9-17 How to write a function prototype
Function Prototypes (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 19
- 20. Figure 9-18 Savings Account Program
Completing the Savings Account
Program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 20
- 21. • Avariable’s scope indicates where in the programthe
variable can be used
• Avariable’s lifetime indicates how long the variable
remains in the computer’s internalmemory
• Both scope and lifetime are determined by whereyou
declare the variable in theprogram
• Variables declared within afunction and those that
appear in afunction’s parameterList have alocal scope
and are referred to aslocalvariables
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 21
The Scope and Lifetime of a Variable
- 22. • Localvariables can be used only by the function inwhich
they are declared or in whose parameterList theyappear
– Remainin internal memory until the function ends
• Global variables are declared outside of any functionin
the program
– Remainin memory until the programends
• Any statement can useaglobal variable
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 22
The Scope and Lifetime of a Variable
(cont’d.)
- 23. • Declaring a variable as global can allow unintentional
errors to occur
– e.g., a function that should not have access to the variable
inadvertently changesthe variable’s contents
• Youshould avoid using global variables unless necessary
• If more than one function needs to accessthe same
variable, it is better to create a local variable in one
function and pass it to other functions that need it
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 23
The Scope and Lifetime of a Variable
(cont’d.)