More Related Content
Similar to Lesson 9.1 value returning
Similar to Lesson 9.1 value returning (20)
More from MLG College of Learning, Inc
More from MLG College of Learning, Inc (20)
Lesson 9.1 value returning
- 1. © 2016 Cengage Learning®. May not be scanned, copied or
Introduction to Programming in C++
Eighth Edition
Lesson 9.1:
Value-Returning Functions
duplicated, or posted to a publicly accessible website, in whole
or in part.
- 2. • Raiseanumber to apower using the pow function
• Return the square root of anumber using thesqrt
function
• Generate random numbers
• Create and invoke afunction that returns avalue
• Passinformation by value to afunction
• Write afunction prototype
• Understand avariable’s scope and lifetime
© 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
Objectives
- 3. • Afunction is ablock of code that performs atask
• Every C++program contains at least one function
(main)
– Most contain manyfunctions
• Somefunctions are built-in functions (part ofC++):
defined in languagelibraries
• Others, called program-defined functions, arewritten
by programmers; defined in aprogram
• Functions allow for blocks of code to be used many
times in aprogram without having toduplicate code
© 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
Functions
- 4. • Functions also allow large, complex programs to be
broken down into small, manageablesub-tasks
• Eachsub-task is solved by afunction, and thus different
people can write different functions
• Many functions can then be combined intoasingle
program
• Typically, main is used to call other functions, but any
function can call any other function
© 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
Functions (cont’d.)
- 5. Figure 9-1 Illustrations of value-returning and void functions
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.
5An Introduction to Programming with C++, Eighth Edition
- 6. • All functions are either value-returning orvoid
• All value-returning functions perform atask and then
return precisely onevalue
• In most cases,the value is returned to the statement
that called thefunction
• Typically, astatement that calls afunction assignsthe
return value to avariable
– However, areturn value could also be usedin a
comparison or calculation or could be printed to the
screen
© 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
Value-Returning Functions
- 7. • Thepow function is aconvenient tool to raise anumber
to apower (exponentiation)
• Thepow function raises anumber to apower and
returns the result asadouble number
• Syntax is pow(x, y), in which x is the base and y is the
exponent
• At least one of the twoarguments must be adouble
• Program must contain the #include <cmath>
directive to usethe powfunction
© 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
The pow Function
- 8. Figure 9-2 How to use the pow function
The pow 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.
8An Introduction to Programming with C++, Eighth Edition
- 9. • sqrt function is abuilt-in value-returning function that
returns anumber’s square root asadouble
• Definition contained in cmathlibrary
–Program must contain #include <cmath>to useit
• Syntax: sqrt(x), in which x is a double or float
–Here, xis an actualargument, which is an item of
information afunction needs toperform its task
• Actual arguments are passed to afunction whencalled
© 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
The sqrt Function
- 10. Figure 9-3 How to use the sqrt function
The sqrt 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.
10An Introduction to Programming with C++, Eighth Edition
- 11. • Program that calculates and displays the length ofa
right triangle hypotenuse
• Program usesPythagorean theorem
– Requires squaring and taking squareroot
• powfunction can be used to square
• sqrtfunction can be used to take square root
• Both are built-in value-returning functions
© 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
The Hypotenuse Program
- 12. Figure 9-4 Pythagorean theorem
The Hypotenuse 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.
12An Introduction to Programming with C++, Eighth Edition
- 13. Figure 9-5 Problem specification, IPO chart information, and C++
instructions for the Hypotenuse Program
The Hypotenuse 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.
13An Introduction to Programming with C++, Eighth Edition
- 14. Figure 9-6 Beginning of Hypotenuse program
The Hypotenuse 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.
14An Introduction to Programming with C++, Eighth Edition
- 15. Figure 9-6 Completion of Hypotenuse Program and sample run
The Hypotenuse 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.
15An Introduction to Programming with C++, Eighth Edition
- 16. • C++provides apseudo-random numbergenerator
– Producesasequence of numbers that meetcertain
statistical requirements forrandomness
– Numbers chosen uniformly from finite set of numbers
– Not truly random but sufficient for practical purposes
• Random number generator in C++:rand function
–Returns an integer between 0 and RAND_MAX, inclusive
– RAND_MAX is abuilt-in constant (>= 32767)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
16An Introduction to Programming with C++, Eighth Edition
The rand, srand, and time
Functions
- 17. • rand function’s syntax: rand()
– Doesn’t require any actual arguments, but parenthesesare
still required
• Expression:
lowerBound + rand() % (upperBound – lowerBound + 1)
– Allows ranges other than 0 to RAND_MAX to be used
– Rangeis upperBound to lowerBound
• Initialize random number generator eachtime
– Otherwise, will produce the samesequence
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
17An Introduction to Programming with C++, Eighth Edition
The rand, srand, and time
Functions (cont’d.)
- 18. Figure 9-7 How to use the rand function
The rand, srand, and time
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.
18An Introduction to Programming with C++, Eighth Edition
- 19. Figure 9-8 How to generate random integers within a specific range
The rand, srand, and time
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.
19An Introduction to Programming with C++, Eighth Edition
- 20. Figure 9-8 How to generate random integers within a specific range
The rand, srand, and time
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.
20An Introduction to Programming with C++, Eighth Edition
- 21. • Use srand function (a void function) to initialize
random number generator
• Syntax: srand(seed), in which seed is an integer
actual argument that represents the starting point of
the generator
–Commonly initialized using the time function
• Ensuresunique sequence of numbers for eachprogram
run
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
21An Introduction to Programming with C++, Eighth Edition
The rand, srand, and time
Functions (cont’d.)
- 22. • time function is a value-returning function that
returns current time in number of seconds since
January 1, 1970
– Returns a time_t object, so must be cast to an integer
before passingto srand
– Program must contain #include <ctime> directive
to useit
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
22An Introduction to Programming with C++, Eighth Edition
The rand, srand, and time
Functions (cont’d.)
- 23. Figure 9-9 How to use the srand function
The rand, srand, and time
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.
23An Introduction to Programming with C++, Eighth Edition