SlideShare a Scribd company logo
1 of 7
Download to read offline
CS111 Lab
Functions
Instructor: Michael Gordon
Why use functions?
 Functions

allow you to perform a specific
task without copying the same code over
and over.
 Make a change once, instead of multiple
times.
 Keeps your main method less cluttered
looking.
Functions we’ve seen
 sqrt()





– returns the calculated square root

#include <cmath>
Parameter type: double
Return type: double

 rand()





– returns a random number

#include <cstdlib>
Parameter type: no parameter
Return type: int
Creating a function
A

function has two parts: a prototype (or
signature) and a definition.
 One common approach is to include the
prototype before the main method and
the definition after.


The compiler will not be able to execute
your functions if they are not declared
before the main method.
Prototype
 The





prototype consists of these parts:

The return type (int, double, void, etc.)
The function name (same rules as variables)
The parameters (separated by commas):
 Each



gets a type and a local variable name

If the prototype is declared separate from
the definition, it ends with a semicolon. If
the definition follows immediately, the
prototype ends with an open bracket.
Return type
 The

return type is what the function returns
to the main program. It can be a number
(int, double), a string, etc.
 If nothing is returned, the return type is
void.
 A void function can (and should) still
perform some action, such as printing to
the console or changing values.
Examples
 See






Dr. Ryba’s site for function examples.

venus.cs.qc.edu/~ryba/cs111/Ch4/square.cpp
venus.cs.qc.edu/~ryba/cs111/Ch4/tri3.cpp
venus.cs.qc.edu/~ryba/cs111/Ch4/tri4.cpp
Note: These examples feature the entire
function (prototype and definition) declared
before the main method. Either way is fine.

More Related Content

What's hot

Whats to come with swift generics
Whats to come with swift genericsWhats to come with swift generics
Whats to come with swift genericsDenis Poifol
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and OverridingMichael Heron
 
Differences between method overloading and method overriding
Differences between method overloading and method overridingDifferences between method overloading and method overriding
Differences between method overloading and method overridingPinky Anaya
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in PythonPranavSB
 
Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143sanjeet kumar
 
Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享LearningTech
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7ArraLafuente
 
What's new in c# 6.0
What's new in c# 6.0What's new in c# 6.0
What's new in c# 6.0LearningTech
 
Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in cDavid Livingston J
 
Type conversion in c
Type conversion in cType conversion in c
Type conversion in cSHIKHA GAUTAM
 
Double pointer (pointer to pointer)
Double pointer (pointer to pointer)Double pointer (pointer to pointer)
Double pointer (pointer to pointer)sangrampatil81
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method OverloadingMichael Heron
 
Interface and abstraction
Interface and abstractionInterface and abstraction
Interface and abstractionRaghav Chhabra
 

What's hot (20)

Whats to come with swift generics
Whats to come with swift genericsWhats to come with swift generics
Whats to come with swift generics
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 
Thin Template Explained
Thin Template ExplainedThin Template Explained
Thin Template Explained
 
Differences between method overloading and method overriding
Differences between method overloading and method overridingDifferences between method overloading and method overriding
Differences between method overloading and method overriding
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in Python
 
Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143
 
Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享
 
Method overloading and constructor overloading in java
Method overloading and constructor overloading in javaMethod overloading and constructor overloading in java
Method overloading and constructor overloading in java
 
Typecasting in c
Typecasting in cTypecasting in c
Typecasting in c
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7
 
What's new in c# 6.0
What's new in c# 6.0What's new in c# 6.0
What's new in c# 6.0
 
Pointers
PointersPointers
Pointers
 
Template classes and ROS messages
Template classes and ROS messagesTemplate classes and ROS messages
Template classes and ROS messages
 
Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
 
Type conversion in c
Type conversion in cType conversion in c
Type conversion in c
 
Double pointer (pointer to pointer)
Double pointer (pointer to pointer)Double pointer (pointer to pointer)
Double pointer (pointer to pointer)
 
Type conversion
Type  conversionType  conversion
Type conversion
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
 
Interface and abstraction
Interface and abstractionInterface and abstraction
Interface and abstraction
 
Mock object
Mock objectMock object
Mock object
 

Viewers also liked

Viewers also liked (9)

Shape logic 1
Shape logic 1Shape logic 1
Shape logic 1
 
Writing edit free billing entries2 (3) (2)
Writing edit free billing entries2 (3) (2)Writing edit free billing entries2 (3) (2)
Writing edit free billing entries2 (3) (2)
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Sage crm introduction by Triad Software
Sage crm introduction by Triad SoftwareSage crm introduction by Triad Software
Sage crm introduction by Triad Software
 
If statements
If statementsIf statements
If statements
 
Introduction to Linux, Pico, G++
Introduction to Linux, Pico, G++Introduction to Linux, Pico, G++
Introduction to Linux, Pico, G++
 
While loops
While loopsWhile loops
While loops
 
貧窮
貧窮貧窮
貧窮
 
Output
OutputOutput
Output
 

Similar to Functions

Similar to Functions (20)

Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
ACM init()- Day 4
ACM init()- Day 4ACM init()- Day 4
ACM init()- Day 4
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
Lecture 08.pptx
Lecture 08.pptxLecture 08.pptx
Lecture 08.pptx
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Function
FunctionFunction
Function
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
Functions
FunctionsFunctions
Functions
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11
 

More from Michael Gordon

More from Michael Gordon (12)

Raspberry Pi presentation for Computer Architecture class
Raspberry Pi presentation for Computer Architecture classRaspberry Pi presentation for Computer Architecture class
Raspberry Pi presentation for Computer Architecture class
 
Strings2
Strings2Strings2
Strings2
 
Strings1
Strings1Strings1
Strings1
 
Introduction to Computer Science 111 Lab
Introduction to Computer Science 111 LabIntroduction to Computer Science 111 Lab
Introduction to Computer Science 111 Lab
 
Strings
StringsStrings
Strings
 
Arrays, continued
Arrays, continuedArrays, continued
Arrays, continued
 
Arrays
ArraysArrays
Arrays
 
For loops
For loopsFor loops
For loops
 
Arithmetic
ArithmeticArithmetic
Arithmetic
 
Variables
VariablesVariables
Variables
 
Word cloud
Word cloudWord cloud
Word cloud
 
Millennial white paper
Millennial white paperMillennial white paper
Millennial white paper
 

Recently uploaded

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 

Recently uploaded (20)

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 

Functions

  • 2. Why use functions?  Functions allow you to perform a specific task without copying the same code over and over.  Make a change once, instead of multiple times.  Keeps your main method less cluttered looking.
  • 3. Functions we’ve seen  sqrt()    – returns the calculated square root #include <cmath> Parameter type: double Return type: double  rand()    – returns a random number #include <cstdlib> Parameter type: no parameter Return type: int
  • 4. Creating a function A function has two parts: a prototype (or signature) and a definition.  One common approach is to include the prototype before the main method and the definition after.  The compiler will not be able to execute your functions if they are not declared before the main method.
  • 5. Prototype  The    prototype consists of these parts: The return type (int, double, void, etc.) The function name (same rules as variables) The parameters (separated by commas):  Each  gets a type and a local variable name If the prototype is declared separate from the definition, it ends with a semicolon. If the definition follows immediately, the prototype ends with an open bracket.
  • 6. Return type  The return type is what the function returns to the main program. It can be a number (int, double), a string, etc.  If nothing is returned, the return type is void.  A void function can (and should) still perform some action, such as printing to the console or changing values.
  • 7. Examples  See     Dr. Ryba’s site for function examples. venus.cs.qc.edu/~ryba/cs111/Ch4/square.cpp venus.cs.qc.edu/~ryba/cs111/Ch4/tri3.cpp venus.cs.qc.edu/~ryba/cs111/Ch4/tri4.cpp Note: These examples feature the entire function (prototype and definition) declared before the main method. Either way is fine.