SlideShare a Scribd company logo
An Introduction To Software
Development Using C++
Class #19:
Functions
Let Us Imagine An
Automobile Factory…
• When an auto is manufactured, it is not made
from raw parts.
• It is put together from previously
manufactured parts.
• Some parts are made by
the auto company.
• Some parts are made by
other firms.
Image Credit: www.porschepedia.org
What Are C++ Functions?
• You can think of functions as being like building blocks.
• They let you divide complicated programs into manageable
pieces.
• Advantages of functions:
– Working on one function allows you to just focus on it and
construct and debug it.
– Different people can work on different functions simultaneously.
– If a function is needed in more than one place in a program or in
different programs, you can write it once and use it multiple
times.
– Using functions greatly enhances
a program’s readability.
Image Credit: commons.wikimedia.org
Predefined C++ Functions
• Examples: pow(x,y), sqrt(x), floor(x)
• The power function calculates xy.
pow(2.0,3) = 2.03 = 8. Is of type double and has two
parameters.
• Square Root calculates the square root:
sqrt(2.25) = 1.5. Is of type double, only has one parameter.
• floor(x) calculates the largest whole number that is less than
or equal to x.
floor(48.79) = 48.0. Is of type double with one parameter.
Image Credit: www.drcruzan.com
How To Use Predefined Functions
• You must include the header file that contains
the function’s specifications via the include
statement.
• To use the pow function, a program must
include:
#include <cmath>
Image Credit: nihima.deviantart.com
User-Define Functions
• C++ does not provide every function that you
will ever need.
• Designer’s can’t know a user’s specific needs.
• This means that you need to learn to write
your own functions.
Image Credit: lisatilmon.blogspot.com
Two Types Of User-Defined Functions
• Value-Returning Functions: These are
functions that have a return type. They return
a value of a specific type using the return
statement.
• Void Functions: Functions that do not have a
return type. These functions DO NOT use a
return statement to return a value.
Image Credit: www.hiveworkshop.com
Value-Returning Functions
• pow, sqrt, and floor are examples of value-returning
functions
• You need to include the header file in your program
using the header statement.
• You need to know the following items:
– The name of the function
– The parameters, if any
– The data type of each parameter
– The data type of the value returned by the function.
– The code required to accomplish the task.
Image Credit: www.creativebradford.co.uk
What Do You Do With The Value Returned
By A Value-Returning Function?
• Save the value for further calculation:
Ex: x = pow(3.0, 2.5);
• Use the value in some calculation:
Ex. Area = PI * pow(radius, 2.0);
• Print the value:
Ex. cout << abs(-5) << endl;
Image Credit: www.fenwayfrank.com
Defining A Value-Returning Function
• You need to know the following items:
1. The name of the function
2. The parameters, if any
3. The data type of each parameter
4. The data type of the value returned by the function.
5. The code required to accomplish the task.
int abs (int number)
1 234
“formal parameter” Image Credit: www.platform21.nl
Suntax Of A Value-Returning Function
Type of the value that the function returns
Image Credit: secure.bettermaths.org.uk
Function Call
• A function’s formal parameter list can be empty.
• The empty parentheses are still needed ()
• In a function call, the number of actual
parameters (and their data types) must match
with the formal parameters in the order given.
This is called a one-to-one correspondence.
A variable or an expression listed
in the call to a function.
Image Credit: www.behance.net
Let’s Create A Function!
• Create a function that will determine which of two numbers is
bigger.
• Function has two parameters. Both parameters are numbers.
• Assume that the data type of the numbers is floating point –
double.
• Because the larger number will be double,
the type of the function will need to be
double also.
• Let’s call our function larger
Image Credit: www.pinterest.com
Our Function!
Image Credit: obdtooles.blogger.ba
Other Ways To Code Our Function
Note: no need for a
local variable!
Image Credit: www.youtube.com
Using Our Function
• Assume num, num1, and num2 are double variables.
• Assume num1 = 45.75, num2 = 35.50
• Various ways that we can call larger:
Image Credit: obdtooles.blogger.ba
What Order Should User-Define
Functions Appear In A Program?
• Should we place our function larger before or
after the main function?
• You must declare an identifier before you use it.
We use larger in main. We must declare larger
before main.
• C++ programmers often place the main function
before all other user-defined functions.
• This will create a compilation error.
Image Credit: www.autopartslib.com
Function Prototype
• To prevent compilation errors when we place main first, we
place function prototypes before any function definition
(including the definition of main).
• The function prototype is NOT a definition.
• It gives the compiler the name of the function, the number
and data types of the parameters, and the data type of the
returned value – just enough to let C++ use the function.
• The prototype is a promise that a full definition of the
function will occur later on in the program. Bad things may
happen if this does not occur.
Image Credit: manuals.deere.com
Example Of A Function Prototype
1 Image Credit: thecoolimages.net
Example Of Multiple
Return Statements// File: Return.cpp
// Description: Use of "return"
#include <iostream>
using namespace std;
char cfunc(int i) {
if(i == 0)
return 'a';
if(i == 1)
return 'g';
if(i == 5)
return 'z';
return 'c';
}
int main() {
cout << "type an integer: ";
int val;
cin >> val;
cout << cfunc(val) << endl;
} // main
type an integer: 10
c
• In cfunc( ), the first if that evaluates to true exits the
function via the return statement.
• Notice that a function declaration is not necessary
because the function definition appears before it is used
in main( ), so the compiler knows about it from that
function definition.
Image Credit: gallerygogopix.net
Value-Returning Functions:
Some Weird Stuff
• What’s wrong with this code?
Image Credit: www.enginebasics.com
Value-Returning Functions:
Some Weird Stuff
• This is a legal return statement – what will get
returned?
• This is legal, but don’t do it!
return x,y;
Image Credit: www.youth-competition.org
2 Types Of Function Parameters
• Value Parameters: When a function is called, the value of a
value parameter is copied into the corresponding formal
parameter. After the copying, there is no relationship
between the value parameter and the formal parameter.
Value parameters only provide a one-way link from the value
parameter to the formal parameter.
• Reference Parameter: The reference parameter received the
address (memory location) of the actual parameter. Reference
parameters can pass one or more values from a function and
can change the value of the actual parameter.
Image Credit: mathinsight.org
When Are Reference Parameters
Useful?
1. When the value of the actual parameter needs
to be changed
2. When you want to return more than one value
from a function (note that the return statement
can only return one value)
3. When passing the address would save memory
space and time relative to copying a large
amount of data.
Image Credit: www.amazon.com
How Do You Define A Reference
Parameter?
• When you attach a & after a data type for a variable name in
the formal parameter list of a function, the variable becomes
a reference parameter.
Void Functions
• Void functions and value returning functions have a
similar structure. Both have a heading and a body.
• A void function does not have a data type.
• A void function may or may not have formal
parameters.
• You can use a return statement to exit a void function.
• A call to a void statement has to
be a stand-alone statement.
Image Credit: www.cnn.com
What Do Void Functions Look Like?
Image Credit: futurism.com
Scope Of An Identifier
• Are you allowed to access an identifier
(variable) anywhere in a program?
• Answer: no
• The scope of an identifier refers to where in
the program the variable is accessible.
Image Credit: www.linkedin.com
A Couple Of Definitions
• Local Identifier: Identifiers declared within a
function (or block). Note: local identifiers are
not accessible outside of the function.
• Global Identifier: Identifiers declared outside
of every function.
Image Credit: www.fcpablog.com
Important Note
• C++ does not allow the nesting of functions.
• You cannot include the definition of a function
within the body of another function.
Image Credit: www.amazon.com
Rules For Accessing A Variable
• Global variables are accessible if:
– The variable is declared before the function
definition.
– The function name is different than the variable.
– All parameters of the function are different than
the name of the variable.
– All local variables have names that are different
than the name of the global variable.
Image Credit: www.theprivacyanddatasecurityblog.com
Rules For Accessing A Variable
• Nested block: An identifier declared within a
block is accessible if:
– Only within the block from the point that it was
declared until the end of the block.
– By those blocks that are nested within that block if
the nested blocks do not have a variable with the
same name as that of the outside block.
Image Credit: www.123rf.com
Rules For Accessing A Variable
• The scope of a function name is similar to the
scope of a variable declared outside of any
block. The scope of a function name is the
same as the scope of a global variable.
Image Credit: www.secure.me
Constants In C++
• Constants are variables that have values that cannot be
changed during execution.
• Named constants placed before the main function are called
global named constants.
• Placing them here can improve the readability of your
program.
Static & Automatic Variables
• The rules so far:
– Memory for global variables remains allocated as
long as the program executes. (these are called
static variables)
– Memory for variables allocated within a block is
allocated at block entry and deallocated at block
exit. (these are called automatic variables)
Image Credit: southbostontoday.com
Static & Automatic Variables
• It turns out that you can declare a static
variable within a function by using the
reserved word static.
Image Credit: www.iconshut.com
Static & Automatic Variables
• Static variables declared within a function are local to
that function. Their scope is the same as any other
local variable in that function.
• Because the memory for static variables remains
allocated between function calls, static variables allow
you to use the value of a variable from one function
call to another.
• The difference between this an using global variables is
that other functions cannot manipulate the variable’s
value.
2 Image Credit: www.iconfinder.com
In Class Programming Challenge:
Drywall Calculations
• You are to create a program that will calculate the materials that will be needed in order to drywall
a new house that is being built.
• Your program will read in data on the dimensions of the room and calculate the total number of
sheets of drywall needed and the boxes of screws that will be required.
• All rooms will be rectangular, 10’ tall, and will be measured in meters
(1m = 3.28 ft).
• Drywall comes in sheets that are 4 x 8, 10, 12, 14, or 16
• Screws come in boxes of 5 and 25 pounds. One pound of screws contains 185 screws. A ratio of one
screw per square foot of drywall is assumed.
• Your program should read in the room data, report the size of each room, the drywall sheets
needed for each room and the number of screws needed.
• Three rules: (1) rooms have 4 walls, not 2, (2) Ignore doors and windows, (3) drywall can be shared
within a room but not between rooms.
• Required functions: convert_to_feet, calculate_drywall,
calculate_screws, print_output. You may create other
functions if you wish.
Image Credit: www.1drywall.com
You must make your main function be the first function in your file.
What’s In Your C++ Toolbox?
cout / cin #include if/else/
Switch
Math Class String getline While
For do…While Break /
Continue
Arrays Functions

More Related Content

PPTX
Intro To C++ - Class #20: Functions, Recursion
PPTX
CPP06 - Functions
PPTX
Functions in c
PDF
9 subprograms
PPT
Basics of cpp
PDF
08 subprograms
PDF
Python recursion
Intro To C++ - Class #20: Functions, Recursion
CPP06 - Functions
Functions in c
9 subprograms
Basics of cpp
08 subprograms
Python recursion

What's hot (20)

PDF
Python algorithm
PPT
Unit 2 Principles of Programming Languages
PDF
VIT351 Software Development VI Unit1
PPT
Inroduction to r
PDF
Functions
PPTX
PPTX
PPTX
Function different types of funtion
DOCX
C programming language working with functions 1
PDF
Aaa ped-2- Python: Basics
PPTX
Functions in python slide share
PPT
Language Integrated Query - LINQ
PDF
Intro to JavaScript - Week 2: Function
PPTX
Ch9 Functions
PPTX
Scope - Static and Dynamic
PPT
Savitch Ch 04
PPTX
3 cs xii_python_functions _ parameter passing
PDF
Client sidescripting javascript
PPTX
Operator Overloading and Scope of Variable
PDF
C- language Lecture 4
Python algorithm
Unit 2 Principles of Programming Languages
VIT351 Software Development VI Unit1
Inroduction to r
Functions
Function different types of funtion
C programming language working with functions 1
Aaa ped-2- Python: Basics
Functions in python slide share
Language Integrated Query - LINQ
Intro to JavaScript - Week 2: Function
Ch9 Functions
Scope - Static and Dynamic
Savitch Ch 04
3 cs xii_python_functions _ parameter passing
Client sidescripting javascript
Operator Overloading and Scope of Variable
C- language Lecture 4
Ad

Similar to Intro To C++ - Class #19: Functions (20)

PPT
Chapter 1.ppt
PPT
Chapter Introduction to Modular Programming.ppt
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
PPT
PDF
PPT
Functions
PDF
Chapter 11 Function
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
PPTX
Chapter One Function.pptx
PPTX
Chapter 4
PPT
Lecture 4
PPT
C++ Functions.ppt
PPTX
Programming Fundamentals lecture-10.pptx
PPTX
Functions in C++
PPTX
Silde of the cse fundamentals a deep analysis
PPTX
Amit user defined functions xi (2)
PPTX
C++ Functions.pptx
PPT
User Defined Functions
DOCX
Functions assignment
Chapter 1.ppt
Chapter Introduction to Modular Programming.ppt
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
Functions
Chapter 11 Function
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
Chapter One Function.pptx
Chapter 4
Lecture 4
C++ Functions.ppt
Programming Fundamentals lecture-10.pptx
Functions in C++
Silde of the cse fundamentals a deep analysis
Amit user defined functions xi (2)
C++ Functions.pptx
User Defined Functions
Functions assignment
Ad

Recently uploaded (20)

PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Cell Types and Its function , kingdom of life
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Business Ethics Teaching Materials for college
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Cell Structure & Organelles in detailed.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Anesthesia in Laparoscopic Surgery in India
Renaissance Architecture: A Journey from Faith to Humanism
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Cell Types and Its function , kingdom of life
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPH.pptx obstetrics and gynecology in nursing
Business Ethics Teaching Materials for college
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Microbial diseases, their pathogenesis and prophylaxis
Pharmacology of Heart Failure /Pharmacotherapy of CHF
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Cell Structure & Organelles in detailed.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
VCE English Exam - Section C Student Revision Booklet
O5-L3 Freight Transport Ops (International) V1.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Anesthesia in Laparoscopic Surgery in India

Intro To C++ - Class #19: Functions

  • 1. An Introduction To Software Development Using C++ Class #19: Functions
  • 2. Let Us Imagine An Automobile Factory… • When an auto is manufactured, it is not made from raw parts. • It is put together from previously manufactured parts. • Some parts are made by the auto company. • Some parts are made by other firms. Image Credit: www.porschepedia.org
  • 3. What Are C++ Functions? • You can think of functions as being like building blocks. • They let you divide complicated programs into manageable pieces. • Advantages of functions: – Working on one function allows you to just focus on it and construct and debug it. – Different people can work on different functions simultaneously. – If a function is needed in more than one place in a program or in different programs, you can write it once and use it multiple times. – Using functions greatly enhances a program’s readability. Image Credit: commons.wikimedia.org
  • 4. Predefined C++ Functions • Examples: pow(x,y), sqrt(x), floor(x) • The power function calculates xy. pow(2.0,3) = 2.03 = 8. Is of type double and has two parameters. • Square Root calculates the square root: sqrt(2.25) = 1.5. Is of type double, only has one parameter. • floor(x) calculates the largest whole number that is less than or equal to x. floor(48.79) = 48.0. Is of type double with one parameter. Image Credit: www.drcruzan.com
  • 5. How To Use Predefined Functions • You must include the header file that contains the function’s specifications via the include statement. • To use the pow function, a program must include: #include <cmath> Image Credit: nihima.deviantart.com
  • 6. User-Define Functions • C++ does not provide every function that you will ever need. • Designer’s can’t know a user’s specific needs. • This means that you need to learn to write your own functions. Image Credit: lisatilmon.blogspot.com
  • 7. Two Types Of User-Defined Functions • Value-Returning Functions: These are functions that have a return type. They return a value of a specific type using the return statement. • Void Functions: Functions that do not have a return type. These functions DO NOT use a return statement to return a value. Image Credit: www.hiveworkshop.com
  • 8. Value-Returning Functions • pow, sqrt, and floor are examples of value-returning functions • You need to include the header file in your program using the header statement. • You need to know the following items: – The name of the function – The parameters, if any – The data type of each parameter – The data type of the value returned by the function. – The code required to accomplish the task. Image Credit: www.creativebradford.co.uk
  • 9. What Do You Do With The Value Returned By A Value-Returning Function? • Save the value for further calculation: Ex: x = pow(3.0, 2.5); • Use the value in some calculation: Ex. Area = PI * pow(radius, 2.0); • Print the value: Ex. cout << abs(-5) << endl; Image Credit: www.fenwayfrank.com
  • 10. Defining A Value-Returning Function • You need to know the following items: 1. The name of the function 2. The parameters, if any 3. The data type of each parameter 4. The data type of the value returned by the function. 5. The code required to accomplish the task. int abs (int number) 1 234 “formal parameter” Image Credit: www.platform21.nl
  • 11. Suntax Of A Value-Returning Function Type of the value that the function returns Image Credit: secure.bettermaths.org.uk
  • 12. Function Call • A function’s formal parameter list can be empty. • The empty parentheses are still needed () • In a function call, the number of actual parameters (and their data types) must match with the formal parameters in the order given. This is called a one-to-one correspondence. A variable or an expression listed in the call to a function. Image Credit: www.behance.net
  • 13. Let’s Create A Function! • Create a function that will determine which of two numbers is bigger. • Function has two parameters. Both parameters are numbers. • Assume that the data type of the numbers is floating point – double. • Because the larger number will be double, the type of the function will need to be double also. • Let’s call our function larger Image Credit: www.pinterest.com
  • 14. Our Function! Image Credit: obdtooles.blogger.ba
  • 15. Other Ways To Code Our Function Note: no need for a local variable! Image Credit: www.youtube.com
  • 16. Using Our Function • Assume num, num1, and num2 are double variables. • Assume num1 = 45.75, num2 = 35.50 • Various ways that we can call larger: Image Credit: obdtooles.blogger.ba
  • 17. What Order Should User-Define Functions Appear In A Program? • Should we place our function larger before or after the main function? • You must declare an identifier before you use it. We use larger in main. We must declare larger before main. • C++ programmers often place the main function before all other user-defined functions. • This will create a compilation error. Image Credit: www.autopartslib.com
  • 18. Function Prototype • To prevent compilation errors when we place main first, we place function prototypes before any function definition (including the definition of main). • The function prototype is NOT a definition. • It gives the compiler the name of the function, the number and data types of the parameters, and the data type of the returned value – just enough to let C++ use the function. • The prototype is a promise that a full definition of the function will occur later on in the program. Bad things may happen if this does not occur. Image Credit: manuals.deere.com
  • 19. Example Of A Function Prototype 1 Image Credit: thecoolimages.net
  • 20. Example Of Multiple Return Statements// File: Return.cpp // Description: Use of "return" #include <iostream> using namespace std; char cfunc(int i) { if(i == 0) return 'a'; if(i == 1) return 'g'; if(i == 5) return 'z'; return 'c'; } int main() { cout << "type an integer: "; int val; cin >> val; cout << cfunc(val) << endl; } // main type an integer: 10 c • In cfunc( ), the first if that evaluates to true exits the function via the return statement. • Notice that a function declaration is not necessary because the function definition appears before it is used in main( ), so the compiler knows about it from that function definition. Image Credit: gallerygogopix.net
  • 21. Value-Returning Functions: Some Weird Stuff • What’s wrong with this code? Image Credit: www.enginebasics.com
  • 22. Value-Returning Functions: Some Weird Stuff • This is a legal return statement – what will get returned? • This is legal, but don’t do it! return x,y; Image Credit: www.youth-competition.org
  • 23. 2 Types Of Function Parameters • Value Parameters: When a function is called, the value of a value parameter is copied into the corresponding formal parameter. After the copying, there is no relationship between the value parameter and the formal parameter. Value parameters only provide a one-way link from the value parameter to the formal parameter. • Reference Parameter: The reference parameter received the address (memory location) of the actual parameter. Reference parameters can pass one or more values from a function and can change the value of the actual parameter. Image Credit: mathinsight.org
  • 24. When Are Reference Parameters Useful? 1. When the value of the actual parameter needs to be changed 2. When you want to return more than one value from a function (note that the return statement can only return one value) 3. When passing the address would save memory space and time relative to copying a large amount of data. Image Credit: www.amazon.com
  • 25. How Do You Define A Reference Parameter? • When you attach a & after a data type for a variable name in the formal parameter list of a function, the variable becomes a reference parameter.
  • 26. Void Functions • Void functions and value returning functions have a similar structure. Both have a heading and a body. • A void function does not have a data type. • A void function may or may not have formal parameters. • You can use a return statement to exit a void function. • A call to a void statement has to be a stand-alone statement. Image Credit: www.cnn.com
  • 27. What Do Void Functions Look Like? Image Credit: futurism.com
  • 28. Scope Of An Identifier • Are you allowed to access an identifier (variable) anywhere in a program? • Answer: no • The scope of an identifier refers to where in the program the variable is accessible. Image Credit: www.linkedin.com
  • 29. A Couple Of Definitions • Local Identifier: Identifiers declared within a function (or block). Note: local identifiers are not accessible outside of the function. • Global Identifier: Identifiers declared outside of every function. Image Credit: www.fcpablog.com
  • 30. Important Note • C++ does not allow the nesting of functions. • You cannot include the definition of a function within the body of another function. Image Credit: www.amazon.com
  • 31. Rules For Accessing A Variable • Global variables are accessible if: – The variable is declared before the function definition. – The function name is different than the variable. – All parameters of the function are different than the name of the variable. – All local variables have names that are different than the name of the global variable. Image Credit: www.theprivacyanddatasecurityblog.com
  • 32. Rules For Accessing A Variable • Nested block: An identifier declared within a block is accessible if: – Only within the block from the point that it was declared until the end of the block. – By those blocks that are nested within that block if the nested blocks do not have a variable with the same name as that of the outside block. Image Credit: www.123rf.com
  • 33. Rules For Accessing A Variable • The scope of a function name is similar to the scope of a variable declared outside of any block. The scope of a function name is the same as the scope of a global variable. Image Credit: www.secure.me
  • 34. Constants In C++ • Constants are variables that have values that cannot be changed during execution. • Named constants placed before the main function are called global named constants. • Placing them here can improve the readability of your program.
  • 35. Static & Automatic Variables • The rules so far: – Memory for global variables remains allocated as long as the program executes. (these are called static variables) – Memory for variables allocated within a block is allocated at block entry and deallocated at block exit. (these are called automatic variables) Image Credit: southbostontoday.com
  • 36. Static & Automatic Variables • It turns out that you can declare a static variable within a function by using the reserved word static. Image Credit: www.iconshut.com
  • 37. Static & Automatic Variables • Static variables declared within a function are local to that function. Their scope is the same as any other local variable in that function. • Because the memory for static variables remains allocated between function calls, static variables allow you to use the value of a variable from one function call to another. • The difference between this an using global variables is that other functions cannot manipulate the variable’s value. 2 Image Credit: www.iconfinder.com
  • 38. In Class Programming Challenge: Drywall Calculations • You are to create a program that will calculate the materials that will be needed in order to drywall a new house that is being built. • Your program will read in data on the dimensions of the room and calculate the total number of sheets of drywall needed and the boxes of screws that will be required. • All rooms will be rectangular, 10’ tall, and will be measured in meters (1m = 3.28 ft). • Drywall comes in sheets that are 4 x 8, 10, 12, 14, or 16 • Screws come in boxes of 5 and 25 pounds. One pound of screws contains 185 screws. A ratio of one screw per square foot of drywall is assumed. • Your program should read in the room data, report the size of each room, the drywall sheets needed for each room and the number of screws needed. • Three rules: (1) rooms have 4 walls, not 2, (2) Ignore doors and windows, (3) drywall can be shared within a room but not between rooms. • Required functions: convert_to_feet, calculate_drywall, calculate_screws, print_output. You may create other functions if you wish. Image Credit: www.1drywall.com You must make your main function be the first function in your file.
  • 39. What’s In Your C++ Toolbox? cout / cin #include if/else/ Switch Math Class String getline While For do…While Break / Continue Arrays Functions

Editor's Notes

  • #2: New name for the class I know what this means Technical professionals are who get hired This means much more than just having a narrow vertical knowledge of some subject area. It means that you know how to produce an outcome that I value. I’m willing to pay you to do that.