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
Ad

More Related Content

What's hot (20)

Python algorithm
Python algorithmPython algorithm
Python algorithm
Prof. Dr. K. Adisesha
 
Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming Languages
Vasavi College of Engg
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
YOGESH SINGH
 
Inroduction to r
Inroduction to rInroduction to r
Inroduction to r
manikanta361
 
Functions
FunctionsFunctions
Functions
Learn By Watch
 
10. sub program
10. sub program10. sub program
10. sub program
Zambales National High School
 
Functions
FunctionsFunctions
Functions
Lakshmi Sarvani Videla
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtion
svishalsingh01
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
Jeevan Raj
 
Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: Basics
AminaRepo
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Language Integrated Query - LINQ
Language Integrated Query - LINQLanguage Integrated Query - LINQ
Language Integrated Query - LINQ
Doncho Minkov
 
Intro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: FunctionIntro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: Function
Jeongbae Oh
 
Ch9 Functions
Ch9 FunctionsCh9 Functions
Ch9 Functions
SzeChingChen
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and Dynamic
Sneh Pahilwani
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
Terry Yoast
 
3 cs xii_python_functions _ parameter passing
3 cs xii_python_functions _ parameter passing3 cs xii_python_functions _ parameter passing
3 cs xii_python_functions _ parameter passing
SanjayKumarMahto1
 
Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
Selvin Josy Bai Somu
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
MOHIT DADU
 
C- language Lecture 4
C- language Lecture 4C- language Lecture 4
C- language Lecture 4
Hatem Abd El-Salam
 
Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming Languages
Vasavi College of Engg
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
YOGESH SINGH
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtion
svishalsingh01
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
Jeevan Raj
 
Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: Basics
AminaRepo
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Language Integrated Query - LINQ
Language Integrated Query - LINQLanguage Integrated Query - LINQ
Language Integrated Query - LINQ
Doncho Minkov
 
Intro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: FunctionIntro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: Function
Jeongbae Oh
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and Dynamic
Sneh Pahilwani
 
3 cs xii_python_functions _ parameter passing
3 cs xii_python_functions _ parameter passing3 cs xii_python_functions _ parameter passing
3 cs xii_python_functions _ parameter passing
SanjayKumarMahto1
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
MOHIT DADU
 

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

Functions
FunctionsFunctions
Functions
Online
 
An Introduction To Python - Final Exam Review
An Introduction To Python - Final Exam ReviewAn Introduction To Python - Final Exam Review
An Introduction To Python - Final Exam Review
Blue Elephant Consulting
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
Hashni T
 
ExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptxExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptx
nglory326
 
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by referenceFunction Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
 
Functions
FunctionsFunctions
Functions
Golda Margret Sheeba J
 
OOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptxOOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptx
sarthakgithub
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
AtharvPotdar2
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
LokeshK66
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
sangeeta borde
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
zueZ3
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
Terry Yoast
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
Bharath904863
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
Michael Heron
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
Ashim Lamichhane
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
ArshiniGubbala3
 
Functions
FunctionsFunctions
Functions
Online
 
An Introduction To Python - Final Exam Review
An Introduction To Python - Final Exam ReviewAn Introduction To Python - Final Exam Review
An Introduction To Python - Final Exam Review
Blue Elephant Consulting
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
Hashni T
 
ExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptxExamRevision_FinalSession_C++ notes.pptx
ExamRevision_FinalSession_C++ notes.pptx
nglory326
 
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by referenceFunction Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
 
OOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptxOOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptx
sarthakgithub
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
AtharvPotdar2
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
LokeshK66
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
sangeeta borde
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
zueZ3
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
Terry Yoast
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
Michael Heron
 
Ad

Recently uploaded (20)

All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Ad

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.
  翻译: