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

Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
David Livingston J
 
Interface and abstraction
Interface and abstractionInterface and abstraction
Interface and abstraction
Raghav 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 (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

Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
 

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 (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

Recently uploaded (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

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.