Submit Search
Upload
Chapter 9 Value-Returning Functions
•
Download as PPT, PDF
•
1 like
•
1,530 views
M
mshellman
Follow
Value-Returning Functions
Read less
Read more
Education
Report
Share
Report
Share
1 of 67
Download now
Recommended
C++ programming
C++ programming
Emertxe Information Technologies Pvt Ltd
Chapter 8 - More on the Repetition Structure
Chapter 8 - More on the Repetition Structure
mshellman
C++ presentation
C++ presentation
SudhanshuVijay3
History of c++
History of c++
Ihsan Wassan
C++ project
C++ project
Sonu S S
What are variables and keywords in c++
What are variables and keywords in c++
Abdul Hafeez
Functions ppt ch06
Functions ppt ch06
Terry Yoast
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
Salahaddin University-Erbil
Recommended
C++ programming
C++ programming
Emertxe Information Technologies Pvt Ltd
Chapter 8 - More on the Repetition Structure
Chapter 8 - More on the Repetition Structure
mshellman
C++ presentation
C++ presentation
SudhanshuVijay3
History of c++
History of c++
Ihsan Wassan
C++ project
C++ project
Sonu S S
What are variables and keywords in c++
What are variables and keywords in c++
Abdul Hafeez
Functions ppt ch06
Functions ppt ch06
Terry Yoast
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
Salahaddin University-Erbil
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
Md. Imran Hossain Showrov
Managing I/O in c++
Managing I/O in c++
Pranali Chaudhari
Variables in C and C++ Language
Variables in C and C++ Language
Way2itech
Function in C program
Function in C program
Nurul Zakiah Zamri Tan
Module 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in C
Tushar B Kute
structured programming Introduction to c fundamentals
structured programming Introduction to c fundamentals
OMWOMA JACKSON
Increment and Decrement operators in C++
Increment and Decrement operators in C++
Neeru Mittal
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Process
mshellman
C++ programming program design including data structures
C++ programming program design including data structures
Ahmad Idrees
User defined functions in C programmig
User defined functions in C programmig
Appili Vamsi Krishna
Functions in c++
Functions in c++
Rokonuzzaman Rony
Nested loop in C language
Nested loop in C language
ErumShammim
Operator overloading
Operator overloading
Ramish Suleman
Variables in C++, data types in c++
Variables in C++, data types in c++
Neeru Mittal
SPL 2 | Algorithms, Pseudo-code, and Flowchart
SPL 2 | Algorithms, Pseudo-code, and Flowchart
Mohammad Imam Hossain
Python dictionary
Python dictionary
eman lotfy
Function overloading(c++)
Function overloading(c++)
Ritika Sharma
Simple Java Programs
Simple Java Programs
AravindSankaran
Lesson 7 io statements
Lesson 7 io statements
Dr. Rupinder Singh
Intro to programming and how to start that career
Intro to programming and how to start that career
Tarek Alabd
Lesson 9.1 value returning
Lesson 9.1 value returning
MLG College of Learning, Inc
Lesson 9.2 guessing the game program
Lesson 9.2 guessing the game program
MLG College of Learning, Inc
More Related Content
What's hot
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
Md. Imran Hossain Showrov
Managing I/O in c++
Managing I/O in c++
Pranali Chaudhari
Variables in C and C++ Language
Variables in C and C++ Language
Way2itech
Function in C program
Function in C program
Nurul Zakiah Zamri Tan
Module 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in C
Tushar B Kute
structured programming Introduction to c fundamentals
structured programming Introduction to c fundamentals
OMWOMA JACKSON
Increment and Decrement operators in C++
Increment and Decrement operators in C++
Neeru Mittal
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Process
mshellman
C++ programming program design including data structures
C++ programming program design including data structures
Ahmad Idrees
User defined functions in C programmig
User defined functions in C programmig
Appili Vamsi Krishna
Functions in c++
Functions in c++
Rokonuzzaman Rony
Nested loop in C language
Nested loop in C language
ErumShammim
Operator overloading
Operator overloading
Ramish Suleman
Variables in C++, data types in c++
Variables in C++, data types in c++
Neeru Mittal
SPL 2 | Algorithms, Pseudo-code, and Flowchart
SPL 2 | Algorithms, Pseudo-code, and Flowchart
Mohammad Imam Hossain
Python dictionary
Python dictionary
eman lotfy
Function overloading(c++)
Function overloading(c++)
Ritika Sharma
Simple Java Programs
Simple Java Programs
AravindSankaran
Lesson 7 io statements
Lesson 7 io statements
Dr. Rupinder Singh
Intro to programming and how to start that career
Intro to programming and how to start that career
Tarek Alabd
What's hot
(20)
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
Managing I/O in c++
Managing I/O in c++
Variables in C and C++ Language
Variables in C and C++ Language
Function in C program
Function in C program
Module 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in C
structured programming Introduction to c fundamentals
structured programming Introduction to c fundamentals
Increment and Decrement operators in C++
Increment and Decrement operators in C++
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Process
C++ programming program design including data structures
C++ programming program design including data structures
User defined functions in C programmig
User defined functions in C programmig
Functions in c++
Functions in c++
Nested loop in C language
Nested loop in C language
Operator overloading
Operator overloading
Variables in C++, data types in c++
Variables in C++, data types in c++
SPL 2 | Algorithms, Pseudo-code, and Flowchart
SPL 2 | Algorithms, Pseudo-code, and Flowchart
Python dictionary
Python dictionary
Function overloading(c++)
Function overloading(c++)
Simple Java Programs
Simple Java Programs
Lesson 7 io statements
Lesson 7 io statements
Intro to programming and how to start that career
Intro to programming and how to start that career
Similar to Chapter 9 Value-Returning Functions
Lesson 9.1 value returning
Lesson 9.1 value returning
MLG College of Learning, Inc
Lesson 9.2 guessing the game program
Lesson 9.2 guessing the game program
MLG College of Learning, Inc
Lesson 6.2 logic error
Lesson 6.2 logic error
MLG College of Learning, Inc
Lesson 8 more on repitition structure
Lesson 8 more on repitition structure
MLG College of Learning, Inc
Lesson 7.1 repitition structure
Lesson 7.1 repitition structure
MLG College of Learning, Inc
Chapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving Process
mshellman
Lesson 7.2 using counters and accumulators
Lesson 7.2 using counters and accumulators
MLG College of Learning, Inc
Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)
Kamran Zafar
Chapter 5 - The Selection Structure
Chapter 5 - The Selection Structure
mshellman
Lesson 1 introduction to programming
Lesson 1 introduction to programming
MLG College of Learning, Inc
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programming
mshellman
Chapter 7 - The Repetition Structure
Chapter 7 - The Repetition Structure
mshellman
Chapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection Structure
mshellman
Sql9e ppt ch08
Sql9e ppt ch08
Dr. Ahmed Al Zaidy
Chapter 3 - Variables and Constants
Chapter 3 - Variables and Constants
mshellman
Lesson 6.1 more on selection structure
Lesson 6.1 more on selection structure
MLG College of Learning, Inc
chapter4.ppt
chapter4.ppt
MalathyN6
C++ language basic
C++ language basic
Waqar Younis
Open mp
Open mp
Gopi Saiteja
Петро Коренєв, "Presentation state containers"
Петро Коренєв, "Presentation state containers"
Sigma Software
Similar to Chapter 9 Value-Returning Functions
(20)
Lesson 9.1 value returning
Lesson 9.1 value returning
Lesson 9.2 guessing the game program
Lesson 9.2 guessing the game program
Lesson 6.2 logic error
Lesson 6.2 logic error
Lesson 8 more on repitition structure
Lesson 8 more on repitition structure
Lesson 7.1 repitition structure
Lesson 7.1 repitition structure
Chapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving Process
Lesson 7.2 using counters and accumulators
Lesson 7.2 using counters and accumulators
Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)
Chapter 5 - The Selection Structure
Chapter 5 - The Selection Structure
Lesson 1 introduction to programming
Lesson 1 introduction to programming
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programming
Chapter 7 - The Repetition Structure
Chapter 7 - The Repetition Structure
Chapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection Structure
Sql9e ppt ch08
Sql9e ppt ch08
Chapter 3 - Variables and Constants
Chapter 3 - Variables and Constants
Lesson 6.1 more on selection structure
Lesson 6.1 more on selection structure
chapter4.ppt
chapter4.ppt
C++ language basic
C++ language basic
Open mp
Open mp
Петро Коренєв, "Presentation state containers"
Петро Коренєв, "Presentation state containers"
Recently uploaded
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
Chameera Dedduwage
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
EduSkills OECD
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Celine George
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
Steve Thomason
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
dawncurless
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
UmakantAnnand
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
David Douglas School District
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
9953056974 Low Rate Call Girls In Saket, Delhi NCR
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
chloefrazer622
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
anshu789521
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
iammrhaywood
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
UnboundStockton
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
Sarwono Sutikno, Dr.Eng.,CISA,CISSP,CISM,CSX-F
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
Association for Project Management
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
TechSoup
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
Celine George
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
RKavithamani
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
Sayali Powar
Recently uploaded
(20)
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
Chapter 9 Value-Returning Functions
1.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Introduction to Programming in C++ Eighth Edition Chapter 9: Value-Returning Functions
2.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Raise a number to a power using the pow function • Return the square root of a number using the sqrt function • Generate random numbers • Create and invoke a function that returns a value • Pass information by value to a function • Write a function prototype • Understand a variable’s scope and lifetime Objectives An Introduction to Programming with C++, Eighth Edition 2
3.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • A function is a block of code that performs a task • Every C++ program contains at least one function (main) – Most contain many functions • Some functions are built-in functions (part of C++): defined in language libraries • Others, called program-defined functions, are written by programmers; defined in a program • Functions allow for blocks of code to be used many times in a program without having to duplicate code Functions An Introduction to Programming with C++, Eighth Edition 3
4.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Functions also allow large, complex programs to be broken down into small, manageable sub-tasks • Each sub-task is solved by a function, and thus different people can write different functions • Many functions can then be combined into a single program • Typically, main is used to call other functions, but any function can call any other function Functions (cont’d.) An Introduction to Programming with C++, Eighth Edition 4
5.
© 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 5 Figure 9-1 Illustrations of value-returning and void functions Functions (cont’d.)
6.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • All functions are either value-returning or void • All value-returning functions perform a task and then return precisely one value • In most cases, the value is returned to the statement that called the function • Typically, a statement that calls a function assigns the return value to a variable – However, a return value could also be used in a comparison or calculation or could be printed to the screen Value-Returning Functions An Introduction to Programming with C++, Eighth Edition 6
7.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • The pow function is a convenient tool to raise a number to a power (exponentiation) • The pow function raises a number to a power and returns the result as a double number • Syntax is pow(x, y), in which x is the base and y is the exponent • At least one of the two arguments must be a double • Program must contain the #include <cmath> directive to use the pow function The pow Function An Introduction to Programming with C++, Eighth Edition 7
8.
© 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 8 Figure 9-2 How to use the pow function The pow Function (cont’d.)
9.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • sqrt function is a built-in value-returning function that returns a number’s square root as a double • Definition contained in cmath library – Program must contain #include <cmath> to use it • Syntax: sqrt(x), in which x is a double or float – Here, x is an actual argument, which is an item of information a function needs to perform its task • Actual arguments are passed to a function when called The sqrt Function An Introduction to Programming with C++, Eighth Edition 9
10.
© 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 10 Figure 9-3 How to use the sqrt function The sqrt Function (cont’d.)
11.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Program that calculates and displays the length of a right triangle hypotenuse • Program uses Pythagorean theorem – Requires squaring and taking square root • pow function can be used to square • sqrt function can be used to take square root • Both are built-in value-returning functions The Hypotenuse Program An Introduction to Programming with C++, Eighth Edition 11
12.
© 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 12 Figure 9-4 Pythagorean theorem The Hypotenuse Program (cont’d.)
13.
© 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 13 Figure 9-5 Problem specification, IPO chart information, and C++ instructions for the Hypotenuse Program The Hypotenuse Program (cont’d.)
14.
© 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 14 Figure 9-6 Beginning of Hypotenuse program The Hypotenuse Program (cont’d.)
15.
© 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 15 Figure 9-6 Completion of Hypotenuse Program and sample run The Hypotenuse Program (cont’d.)
16.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • C++ provides a pseudo-random number generator – Produces a sequence of numbers that meet certain statistical requirements for randomness – 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 a built-in constant (>= 32767) The rand, srand, and time Functions An Introduction to Programming with C++, Eighth Edition 16
17.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • rand function’s syntax: rand() – Doesn’t require any actual arguments, but parentheses are still required • Expression: lowerBound + rand() % (upperBound – lowerBound + 1) – Allows ranges other than 0 to RAND_MAX to be used – Range is upperBound to lowerBound • Initialize random number generator each time – Otherwise, will produce the same sequence The rand, srand, and time Functions (cont’d.) An Introduction to Programming with C++, Eighth Edition 17
18.
© 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 Figure 9-7 How to use the rand function The rand, srand, and time Functions (cont’d.)
19.
© 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 Figure 9-8 How to generate random integers within a specific range The rand, srand, and time Functions (cont’d.)
20.
© 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 Figure 9-8 How to generate random integers within a specific range The rand, srand, and time Functions (cont’d.)
21.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • 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 • Ensures unique sequence of numbers for each program run The rand, srand, and time Functions (cont’d.) An Introduction to Programming with C++, Eighth Edition 21
22.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • 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 passing to srand – Program must contain #include <ctime> directive to use it The rand, srand, and time Functions (cont’d.) An Introduction to Programming with C++, Eighth Edition 22
23.
© 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 Figure 9-9 How to use the srand function The rand, srand, and time Functions (cont’d.)
24.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Program generates a random number from 1 to 10 and then allows the user as many choices as needed to guess the number. • The srand, time, and rand functions are all utilized in the program. The Guessing Game Program An Introduction to Programming with C++, Eighth Edition 24
25.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Guessing Game Program(cont’d.) An Introduction to Programming with C++, Eighth Edition 25 Figure 9-10 Problem specification, IPO chart information, and C++ instructions for the Guessing Game Program
26.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Guessing Game Program(cont’d.) An Introduction to Programming with C++, Eighth Edition 26 Figure 9-11 Guessing Game Program and sample run
27.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • A program-defined value-returning function definition is composed of a header and a body • Header (first line) contains return data type, name of function, and an optional parameterList – Rules for function names are same as for variables – Good idea to use meaningful names that describe function’s purpose – Memory locations in parameterList are called formal parameters • Each stores an item of information passed to the function when it is called Creating Program-Defined Value- Returning Functions An Introduction to Programming with C++, Eighth Edition 27
28.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Function body contains instructions for performing the function’s assigned task • Surrounded by braces ({}) • Last statement is usually the return statement – Returns one value (must match return data type in function header) • After return statement is processed, program execution continues in calling function • Good idea to include comment (such as //end of functionName) to mark end of function Creating Program-Defined Value- Returning Functions (cont’d.) An Introduction to Programming with C++, Eighth Edition 28
29.
© 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 29 Figure 9-12 How to create a program-defined value-returning function Creating Program-Defined Value- Returning Functions (cont’d.)
30.
© 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 30 Figure 9-12 How to create a program-defined value-returning function Creating Program-Defined Value-Returning Functions (cont’d.)
31.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • A function must be called (invoked) to perform its task • main is automatically called when program is run • Other functions must be called by a statement • Syntax for calling a function: functionName([argumentList]); – argumentList is list of actual arguments (if any) – An actual argument can be a variable, named constant, literal constant, or keyword Calling a Function An Introduction to Programming with C++, Eighth Edition 31
32.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Value-returning functions are typically called from statements that: – Assign the return value to a variable – Use the return value in a calculation or comparison – Display the return value • A call to a void function is an independent statement because void functions do not return values Calling a Function (cont’d.) An Introduction to Programming with C++, Eighth Edition 32
33.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • C++ allows you to pass either a variable’s value or its address to a function • Passing a variable’s value is referred to as passing by value • Passing a variable’s address is referred to as passing by reference • Default is passing by value • Number, data type, and ordering of actual arguments must match the formal parameters in function header – Names do not need to match (different names are better) Calling a Function (cont’d.) An Introduction to Programming with C++, Eighth Edition 33
34.
© 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 34 Figure 9-13 How to call (invoke) a value-returning function Calling a Function (cont’d.)
35.
© 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 35 Figure 9-14 Function call and function definition Calling a Function (cont’d.)
36.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • The program allows the user to enter the initial deposit made into a savings account and the annual interest rate. • The program displays the amount of money in the account at the end of 1 through 3 years, assuming no additional deposits or withdrawals are made. The Savings Account Program An Introduction to Programming with C++, Eighth Edition 36
37.
© 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 37 Figure 9-15 Problem specification, IPO chart information, and C++ instructions for the Savings Account Program The Savings Account Program (cont’d.)
38.
© 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 38 Figure 9-15 Problem specification, IPO chart information, and C++ instructions for the Savings Account Program The Savings Account Program (cont’d.)
39.
© 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 39 Figure 9-16 Flowcharts for the Savings Account Program The Savings Account Program (cont’d.)
40.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • When a function definition appears below the main function, you must enter a function prototype above the main function • A function prototype is a statement that specifies the function’s name, data type of its return value, and data type of each of its formal parameters (if any) – Names for the formal parameters are not required • Programmers usually place function prototypes at beginning of program, after the #include directives Function Prototypes An Introduction to Programming with C++, Eighth Edition 40
41.
© 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 41 Figure 9-17 How to write a function prototype Function Prototypes (cont’d.)
42.
© 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 42 Figure 9-18 Savings Account Program Completing the Savings Account Program
43.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • A variable’s scope indicates where in the program the variable can be used • A variable’s lifetime indicates how long the variable remains in the computer’s internal memory • Both scope and lifetime are determined by where you declare the variable in the program • Variables declared within a function and those that appear in a function’s parameterList have a local scope and are referred to as local variables The Scope and Lifetime of a Variable An Introduction to Programming with C++, Eighth Edition 43
44.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Local variables can be used only by the function in which they are declared or in whose parameterList they appear – Remain in internal memory until the function ends • Global variables are declared outside of any function in the program – Remain in memory until the program ends • Any statement can use a global variable The Scope and Lifetime of a Variable (cont’d.) An Introduction to Programming with C++, Eighth Edition 44
45.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Declaring a variable as global can allow unintentional errors to occur – e.g., a function that should not have access to the variable inadvertently changes the variable’s contents • You should avoid using global variables unless necessary • If more than one function needs to access the same variable, it is better to create a local variable in one function and pass it to other functions that need it The Scope and Lifetime of a Variable (cont’d.) An Introduction to Programming with C++, Eighth Edition 45
46.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Functions – Allow programmers to avoid duplicating code – Allow for large, complex programs to be broken into small, manageable tasks • Some functions are built into the language, and others are program-defined • All functions are either value-returning or void • A value-returning function returns one value – Value returned to statement that called the function • A void function returns no value Summary An Introduction to Programming with C++, Eighth Edition 46
47.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Use the sqrt function to find the square root of a number • Use the pow function to raise a number to a power • Items in parentheses in a function call are called actual arguments • The rand function is used to generate random numbers – Returns an integer between 0 and RAND_MAX • srand function is used to initialize rand function – time function usually used as seed (starting point) Summary (cont’d.) An Introduction to Programming with C++, Eighth Edition 47
48.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Function definition composed of header and body • Header specifies function name, return data type, and formal parameter names and types (if any) – Data types and ordering of formal parameters must match data types and ordering of actual arguments • Body contains instructions for performing the function’s assigned task – Surrounded by braces ({}) • return statement returns the result of an expression to the calling function Summary (cont’d.) An Introduction to Programming with C++, Eighth Edition 48
49.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • You call a function by including its name and actual arguments (if any) in a statement • Variables in C++ are passed by value by default • A function prototype must be provided for each function defined below the main function • Scope of a variable indicates where in the program it can be used • Lifetime of a variable indicates how long it will stay in internal memory Summary (cont’d.) An Introduction to Programming with C++, Eighth Edition 49
50.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Local variables can be used only within the function in which they are declared or in whose parameterList they appear – Remain in memory until the function ends • Global variables can be used anywhere – Remain in memory until the program ends • If more than one memory location have the same name, position of the statement in which the name is used determines which location is used Summary (cont’d.) An Introduction to Programming with C++, Eighth Edition 50
51.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Study the program in Figure 9-26, and then answer the questions Lab 9-1: Stop and Analyze An Introduction to Programming with C++, Eighth Edition 51 Figure 9-26 Code for Lab 9-1
52.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-1: Stop and Analyze (cont’d.) An Introduction to Programming with C++, Eighth Edition 52 Figure 9-26 Code for Lab 9-1
53.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-2: Plan and Create An Introduction to Programming with C++, Eighth Edition 53 Figure 9-27 Problem specification for Lab 9-2
54.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 54 Figure 9-27 Problem specification for Lab 9-2
55.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 55 Figure 9-28 IPO chart for the main function
56.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 56 Figure 9-28 IPO chart for the main function
57.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 57 Figure 9-29 IPO chart for the getPayment function
58.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 58 Figure 9-29 IPO chart for the getPayment function
59.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 59 Figure 9-31 IPO chart information and C++ instructions for the main function
60.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 60 Figure 9-32 IPO chart information and C++ instructions for the getPayment function
61.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 61 Figure 9-34 Beginning of finalized Car Payment Program
62.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 62 Figure 9-34 Completion of finalized Car Payment Program
63.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Modify the program in Lab9-2.cpp. Make the changes listed in Figure 9-35 below. Save the modified program as Lab9-3.cpp • Save, run, and test the program. Lab 9-3: Modify An Introduction to Programming with C++, Eighth Edition 63 Figure 9-35 Modifications for Lab 9-3
64.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • The program is this lab should display the total due, given the quantity purchased and the item price. • Follow the instructions for starting C++ and opening the Lab9-4.cpp file. Put the C++ instructions in the proper order, and then determine the one or more missing instructions. • Test the program appropriately. Lab 9-4: What’s Missing? An Introduction to Programming with C++, Eighth Edition 64
65.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Use the data shown in Figure 9-36 to desk-check the figure’s code. What current total will the code display on the screen? Lab 9-5: Desk-Check An Introduction to Programming with C++, Eighth Edition 65
66.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 9-5: Desk-Check (cont’d.) An Introduction to Programming with C++, Eighth Edition 66 Figure 9-36 Test data and code for Lab 9-5
67.
© 2016 Cengage
Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Test the program in the Lab9-6.cpp file using the data 20500, 3500, and 10 as the asset cost, salvage value, and useful life, respectively. • The depreciation should be $1700.00 • Debug the program Lab 9-6: Debug An Introduction to Programming with C++, Eighth Edition 67
Download now