SlideShare ist ein Scribd-Unternehmen logo
1 von 22
An Introduction To Software
Development Using C++
Class #20:
Functions, Recursion
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.
In Class Programming Challenge:
Drywall Calculations, Part II
• Your program will read in data on the dimensions of the rooms in the house being
built.
• All rooms will be rectangular, 10’ tall, and will be measured in meters
(1m = 3.28 ft).
• Your program will then convert the room measurements that are in meters to feet
using a single function with two reference parameters.
• This function will return two values, the room size in feet, using the two reference
parameters.
• Your program will print out: the room name, the room size in meters, and the
room size in feet.
Image Credit: www.1drywall.com
You must make your main function be the first function in your file.
1
Inline Functions
• Implementing a program as a set of functions is good from a software engineering
standpoint, but function calls involve execution-time overhead.
• C++ provides inline functions to help reduce function call overhead—especially for
small functions.
• Placing the qualifier inline before a function’s return type in the function definition
“advises” the compiler to generate a copy of the function’s body code in place
(when appropriate) to avoid a function call.
• The trade-off is that multiple copies of the function code are inserted in the
program (often making the program larger) rather than there being a single copy
of the function to which control is passed each time the function is called.
• The compiler can ignore the inline qualifier and typically does so for all but the
smallest functions.
5 Image Credit: www.iisa.org
Default Arguments
• It’s common for a program to invoke a function repeatedly with the same argument value
for a particular parameter.
• In such cases, you can specify that such a parameter has a default argument, i.e., a default value to be
passed to that parameter.
• When a program omits an argument for a parameter with a default argument in a function call, the
compiler rewrites the function call and inserts the default value of that argument.
• Default arguments must be the rightmost (trailing) arguments in a function’s parameter list.
• When calling a function with two or more default arguments, if an omitted argument is not the rightmost
argument in the argument list, then all arguments to the right of that argument also must be omitted.
• Default arguments must be specified with the first occurrence of the function name—typically, in the
function prototype.
• If the function prototype is omitted because the function definition also serves as the prototype, then the
default arguments should be specified in the function header.
• Default values can be any expression, including constants, global variables or function calls.
• Default arguments also can be used with inline functions
Image Credit: devblackops.io
Default Arguments
• The first call to boxVolume specifies no arguments, thus using all three default values of 1.
• The second call passes only a length argument, thus using default values of 1 for the width
and height arguments.
• The third call (line 20) passes arguments for only length and width, thus using a default value
of 1 for the height argument.
• The last call (line 24) passes arguments for length, width and height, thus using no default
values.
• Any arguments passed to the function explicitly are assigned to the function’s parameters
from left to right.
• Therefore, when boxVolume receives one argument, the function assigns the value of that
argument to its length parameter (i.e., the leftmost parameter in the parameter list).
• When boxVolume receives two arguments, the function assigns the values of those
arguments to its length and width parameters in that order.
• Finally, when boxVolume receives all three arguments, the function assigns the values of
those arguments to its length, width and height parameters, respectively.
6 Image Credit: www.mining.com
Unary Scope Resolution Operator
• It’s possible to declare local and global variables of the same name.
• C++ provides the unary scope resolution operator (::) to access a
global variable when a local variable of the same name is in scope.
• The unary scope resolution operator cannot be used to access a
local variable of the same name in an outer block.
• A global variable can be accessed directly without the unary scope
resolution operator if the name of the global variable is not the
same as that of a local variable in scope.
7 Image Credit: www.hitec-graduate-school.de
Recursion!
• Some problems are best solved by having a function call
itself.
• A recursive function is a function that calls itself, either
directly, or indirectly (through another function).
• Although many compilers allow function main to call itself,
the C++ standard document indicate that main should not
be called within a program or recursively. Its sole purpose is
to be the starting point for program execution.
Image Credit: mr-verb.blogspot.com
Recursive Problem-Solving Approaches
• A recursive function is called to solve a problem.
• The function knows how to solve only the simplest case(s),
or so-called base case(s).
• If the function is called with a base case, the function simply returns a result.
• If the function is called with a more complex problem, it typically divides the problem into
two conceptual pieces—a piece that the function knows how to do and a piece that it does
not know how to do.
• To make recursion feasible, the latter piece must resemble the original problem, but be a
slightly simpler or smaller version.
• This new problem looks like the original, so the function calls a copy of itself to work on the
smaller problem—this is referred to as a recursive call and is also
called the recursion step.
• The recursion step often includes the keyword return, because its result will be combined
with the portion of the problem the function knew how to solve to form the result passed
back to the original caller, possibly main
Image Credit: keenformatics.blogspot.com
An Example Of Recursion: 5!
n! = n · (n – 1)!
Recursive Problem-Solving Approaches
• The recursion step executes while the original
call to the function is still “open,” i.e., it has not
yet finished executing.
• The recursion step can result in many more such recursive
calls, as the function keeps dividing each new subproblem with which the
function is called into two conceptual pieces.
• In order for the recursion to eventually terminate, each time the function
calls itself with a slightly simpler version of the original problem, this
sequence of smaller and smaller problems must eventually converge on
the base case.
• At that point, the function recognizes the base case and returns a result to
the previous copy of the function, and a sequence of returns ensues up
the line until the original call eventually returns the final result to main.
2 Image Credit: www.joedog.org
Another Recursion Example:
Fibonacci Series
• The Fibonacci series:
0, 1, 1, 2, 3, 5, 8, 13, 21, …
• Begins with 0 and 1 and has the property that each
subsequent Fibonacci number is the sum of the previous
two Fibonacci numbers.
• The Fibonacci series can be defined recursively as follows:
fibonacci(0) = 0
fibonacci(1) = 1
fibonacci(n) = fibonacci(n – 1) + fibonacci(n – 2)
3 Image Credit: en.wikipedia.org
Fibonacci Series
3
A Note Of Caution About Recursion
• A word of caution is in order about recursive programs like the one we use here to
generate Fibonacci numbers.
• Each level of recursion in function fibonacci has a doubling effect on the number of
function calls; i.e., the number of recursive calls that are required to calculate the
nth Fibonacci number is on the order of 2n.
• This rapidly gets out of hand.
• Calculating only the 20th Fibonacci number would require on the order of 220 or
about a million calls, calculating the 30th Fibonacci number would require on the
order of 230 or about a billion calls, and so on.
• Computer scientists refer to this as exponential complexity.
• Problems of this nature humble even the world’s most
powerful computers!
Image Credit: en.wikipedia.org
Recursion vs. Iteration
• Both iteration and recursion are based on a control statement: Iteration uses a
repetition structure; recursion uses a selection structure.
• Both iteration and recursion involve repetition: Iteration explicitly uses a repetition
structure; recursion achieves repetition through repeated function calls.
• Iteration and recursion both involve a termination test: Iteration terminates when
the loop-continuation condition fails; recursion terminates when a base case is
recognized.
• Iteration with counter-controlled repetition and recursion both gradually approach
termination: Iteration modifies a counter until the counter assumes a value that
makes the loop-continuation condition fail; recursion produces simpler versions of
the original problem until the base case is reached.
• Both iteration and recursion can occur infinitely: An infinite loop occurs with
iteration if the loop-continuation test never becomes false; infinite recursion
occurs if the recursion step does not reduce the problem during each recursive
call in a manner that converges on the base case.
Image Credit: albello.com
Iterative solution
to the Factorial Problem
• A repetition statement is used rather than the selection
statement of the recursive solution.
• Both solutions use a termination test. In the recursive
solution, it tests for the base case. In the iterative solution,
it tests the loop-continuation condition—if
the test fails, the loop terminates.
• Finally, instead of producing simpler versions of the
original problem, the iterative solution uses a counter that
is modified until the loop-continuation condition becomes
false.
4 Image Credit: www.recommind.com
The Downside Of Recursion
• Recursion has negatives.
• It repeatedly invokes the mechanism, and consequently the
overhead, of function calls.
• This can be expensive in both processor time and memory space.
• Each recursive call causes another copy of the function (actually
only the function’s variables) to be created; this can consume
considerable memory.
• Iteration normally occurs within a function, so the overhead of
repeated function calls and extra memory assignment is omitted.
• So why choose recursion?
Image Credit: lawyernomics.avvo.com
Why Choose Recursion?
• Any problem that can be solved recursively can also be
solved iteratively (nonrecursively).
• A recursive approach is normally chosen when the
recursive approach more naturally mirrors the problem and
results in a program that’s easier to understand and debug.
• Another reason to choose a recursive solution is that an
iterative solution is not apparent.
• Avoid using recursion in performance
situations. Recursive calls take time and
consume additional memory.
Image Credit: everythingcomputerscience.com
Today’s In-Class
Programming Challenge: Phonewords
• Given a phone number, 800-356-9377, write a
recursive C++ program to generate as many words
out of these numbers as possible.
• 2 (a,b,c), 3 (d,e,f), 4(g,h,i), 5 (j,k,l), 6 (m,n,o),
7 (p,q,r,s), 8 (t,u,v), 9 (w,x,y,z)
• Ignore trying to convert
the “800” into a word.
Image Credit: www.acma.gov.au
What’s In Your C++ Toolbox?
cout / cin #include if/else/
Switch
Math Class String getline While
For do…While Break /
Continue
Arrays Functions

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Python recursion
Python recursionPython recursion
Python recursion
 
Functions
FunctionsFunctions
Functions
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtion
 
Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
Language Integrated Query - LINQ
Language Integrated Query - LINQLanguage Integrated Query - LINQ
Language Integrated Query - LINQ
 
Functions in python
Functions in python Functions in python
Functions in python
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
Python functions
Python functionsPython functions
Python functions
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
10. sub program
10. sub program10. sub program
10. sub program
 
DataWeave 2.0 - MuleSoft CONNECT 2019
DataWeave 2.0 - MuleSoft CONNECT 2019DataWeave 2.0 - MuleSoft CONNECT 2019
DataWeave 2.0 - MuleSoft CONNECT 2019
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 
Linq
LinqLinq
Linq
 
Functions
FunctionsFunctions
Functions
 
Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: Basics
 

Andere mochten auch

Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1Blue Elephant Consulting
 
Intro To C++ - Class #23: Inheritance, Part 2
Intro To C++ - Class #23: Inheritance, Part 2Intro To C++ - Class #23: Inheritance, Part 2
Intro To C++ - Class #23: Inheritance, Part 2Blue Elephant Consulting
 
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIATypes Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIADheeraj Kataria
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGAbhishek Dwivedi
 
ATAM 2014 - Estimations et planification Agiles
ATAM 2014 - Estimations et planification AgilesATAM 2014 - Estimations et planification Agiles
ATAM 2014 - Estimations et planification AgilesYassine Zakaria
 

Andere mochten auch (8)

Functions12
Functions12Functions12
Functions12
 
Recursion
RecursionRecursion
Recursion
 
Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1
 
Intro To C++ - Class #23: Inheritance, Part 2
Intro To C++ - Class #23: Inheritance, Part 2Intro To C++ - Class #23: Inheritance, Part 2
Intro To C++ - Class #23: Inheritance, Part 2
 
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIATypes Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
ATAM 2014 - Estimations et planification Agiles
ATAM 2014 - Estimations et planification AgilesATAM 2014 - Estimations et planification Agiles
ATAM 2014 - Estimations et planification Agiles
 

Ähnlich wie C++ Functions, Recursion, and Parameters

Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C ProgrammingAnil Pokhrel
 
C++ question and answers
C++ question and answersC++ question and answers
C++ question and answersAdenKheire
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxGebruGetachew2
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptxYagna15
 
Functions
FunctionsFunctions
FunctionsOnline
 
Functions in c language1
Functions in c language1Functions in c language1
Functions in c language1sirikeshava
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptxzueZ3
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06Terry Yoast
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdfManiMala75
 

Ähnlich wie C++ Functions, Recursion, and Parameters (20)

Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
CPP07 - Scope
CPP07 - ScopeCPP07 - Scope
CPP07 - Scope
 
C++ question and answers
C++ question and answersC++ question and answers
C++ question and answers
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
 
Functions
FunctionsFunctions
Functions
 
Functions in c language1
Functions in c language1Functions in c language1
Functions in c language1
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
Functions
FunctionsFunctions
Functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functions
Functions Functions
Functions
 
user defined function
user defined functionuser defined function
user defined function
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 

Kürzlich hochgeladen

Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 

Kürzlich hochgeladen (20)

Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 

C++ Functions, Recursion, and Parameters

  • 1. An Introduction To Software Development Using C++ Class #20: Functions, Recursion
  • 2. 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
  • 3. 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
  • 4. 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.
  • 5. In Class Programming Challenge: Drywall Calculations, Part II • Your program will read in data on the dimensions of the rooms in the house being built. • All rooms will be rectangular, 10’ tall, and will be measured in meters (1m = 3.28 ft). • Your program will then convert the room measurements that are in meters to feet using a single function with two reference parameters. • This function will return two values, the room size in feet, using the two reference parameters. • Your program will print out: the room name, the room size in meters, and the room size in feet. Image Credit: www.1drywall.com You must make your main function be the first function in your file. 1
  • 6. Inline Functions • Implementing a program as a set of functions is good from a software engineering standpoint, but function calls involve execution-time overhead. • C++ provides inline functions to help reduce function call overhead—especially for small functions. • Placing the qualifier inline before a function’s return type in the function definition “advises” the compiler to generate a copy of the function’s body code in place (when appropriate) to avoid a function call. • The trade-off is that multiple copies of the function code are inserted in the program (often making the program larger) rather than there being a single copy of the function to which control is passed each time the function is called. • The compiler can ignore the inline qualifier and typically does so for all but the smallest functions. 5 Image Credit: www.iisa.org
  • 7. Default Arguments • It’s common for a program to invoke a function repeatedly with the same argument value for a particular parameter. • In such cases, you can specify that such a parameter has a default argument, i.e., a default value to be passed to that parameter. • When a program omits an argument for a parameter with a default argument in a function call, the compiler rewrites the function call and inserts the default value of that argument. • Default arguments must be the rightmost (trailing) arguments in a function’s parameter list. • When calling a function with two or more default arguments, if an omitted argument is not the rightmost argument in the argument list, then all arguments to the right of that argument also must be omitted. • Default arguments must be specified with the first occurrence of the function name—typically, in the function prototype. • If the function prototype is omitted because the function definition also serves as the prototype, then the default arguments should be specified in the function header. • Default values can be any expression, including constants, global variables or function calls. • Default arguments also can be used with inline functions Image Credit: devblackops.io
  • 8. Default Arguments • The first call to boxVolume specifies no arguments, thus using all three default values of 1. • The second call passes only a length argument, thus using default values of 1 for the width and height arguments. • The third call (line 20) passes arguments for only length and width, thus using a default value of 1 for the height argument. • The last call (line 24) passes arguments for length, width and height, thus using no default values. • Any arguments passed to the function explicitly are assigned to the function’s parameters from left to right. • Therefore, when boxVolume receives one argument, the function assigns the value of that argument to its length parameter (i.e., the leftmost parameter in the parameter list). • When boxVolume receives two arguments, the function assigns the values of those arguments to its length and width parameters in that order. • Finally, when boxVolume receives all three arguments, the function assigns the values of those arguments to its length, width and height parameters, respectively. 6 Image Credit: www.mining.com
  • 9. Unary Scope Resolution Operator • It’s possible to declare local and global variables of the same name. • C++ provides the unary scope resolution operator (::) to access a global variable when a local variable of the same name is in scope. • The unary scope resolution operator cannot be used to access a local variable of the same name in an outer block. • A global variable can be accessed directly without the unary scope resolution operator if the name of the global variable is not the same as that of a local variable in scope. 7 Image Credit: www.hitec-graduate-school.de
  • 10. Recursion! • Some problems are best solved by having a function call itself. • A recursive function is a function that calls itself, either directly, or indirectly (through another function). • Although many compilers allow function main to call itself, the C++ standard document indicate that main should not be called within a program or recursively. Its sole purpose is to be the starting point for program execution. Image Credit: mr-verb.blogspot.com
  • 11. Recursive Problem-Solving Approaches • A recursive function is called to solve a problem. • The function knows how to solve only the simplest case(s), or so-called base case(s). • If the function is called with a base case, the function simply returns a result. • If the function is called with a more complex problem, it typically divides the problem into two conceptual pieces—a piece that the function knows how to do and a piece that it does not know how to do. • To make recursion feasible, the latter piece must resemble the original problem, but be a slightly simpler or smaller version. • This new problem looks like the original, so the function calls a copy of itself to work on the smaller problem—this is referred to as a recursive call and is also called the recursion step. • The recursion step often includes the keyword return, because its result will be combined with the portion of the problem the function knew how to solve to form the result passed back to the original caller, possibly main Image Credit: keenformatics.blogspot.com
  • 12. An Example Of Recursion: 5! n! = n · (n – 1)!
  • 13. Recursive Problem-Solving Approaches • The recursion step executes while the original call to the function is still “open,” i.e., it has not yet finished executing. • The recursion step can result in many more such recursive calls, as the function keeps dividing each new subproblem with which the function is called into two conceptual pieces. • In order for the recursion to eventually terminate, each time the function calls itself with a slightly simpler version of the original problem, this sequence of smaller and smaller problems must eventually converge on the base case. • At that point, the function recognizes the base case and returns a result to the previous copy of the function, and a sequence of returns ensues up the line until the original call eventually returns the final result to main. 2 Image Credit: www.joedog.org
  • 14. Another Recursion Example: Fibonacci Series • The Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, … • Begins with 0 and 1 and has the property that each subsequent Fibonacci number is the sum of the previous two Fibonacci numbers. • The Fibonacci series can be defined recursively as follows: fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n) = fibonacci(n – 1) + fibonacci(n – 2) 3 Image Credit: en.wikipedia.org
  • 16. A Note Of Caution About Recursion • A word of caution is in order about recursive programs like the one we use here to generate Fibonacci numbers. • Each level of recursion in function fibonacci has a doubling effect on the number of function calls; i.e., the number of recursive calls that are required to calculate the nth Fibonacci number is on the order of 2n. • This rapidly gets out of hand. • Calculating only the 20th Fibonacci number would require on the order of 220 or about a million calls, calculating the 30th Fibonacci number would require on the order of 230 or about a billion calls, and so on. • Computer scientists refer to this as exponential complexity. • Problems of this nature humble even the world’s most powerful computers! Image Credit: en.wikipedia.org
  • 17. Recursion vs. Iteration • Both iteration and recursion are based on a control statement: Iteration uses a repetition structure; recursion uses a selection structure. • Both iteration and recursion involve repetition: Iteration explicitly uses a repetition structure; recursion achieves repetition through repeated function calls. • Iteration and recursion both involve a termination test: Iteration terminates when the loop-continuation condition fails; recursion terminates when a base case is recognized. • Iteration with counter-controlled repetition and recursion both gradually approach termination: Iteration modifies a counter until the counter assumes a value that makes the loop-continuation condition fail; recursion produces simpler versions of the original problem until the base case is reached. • Both iteration and recursion can occur infinitely: An infinite loop occurs with iteration if the loop-continuation test never becomes false; infinite recursion occurs if the recursion step does not reduce the problem during each recursive call in a manner that converges on the base case. Image Credit: albello.com
  • 18. Iterative solution to the Factorial Problem • A repetition statement is used rather than the selection statement of the recursive solution. • Both solutions use a termination test. In the recursive solution, it tests for the base case. In the iterative solution, it tests the loop-continuation condition—if the test fails, the loop terminates. • Finally, instead of producing simpler versions of the original problem, the iterative solution uses a counter that is modified until the loop-continuation condition becomes false. 4 Image Credit: www.recommind.com
  • 19. The Downside Of Recursion • Recursion has negatives. • It repeatedly invokes the mechanism, and consequently the overhead, of function calls. • This can be expensive in both processor time and memory space. • Each recursive call causes another copy of the function (actually only the function’s variables) to be created; this can consume considerable memory. • Iteration normally occurs within a function, so the overhead of repeated function calls and extra memory assignment is omitted. • So why choose recursion? Image Credit: lawyernomics.avvo.com
  • 20. Why Choose Recursion? • Any problem that can be solved recursively can also be solved iteratively (nonrecursively). • A recursive approach is normally chosen when the recursive approach more naturally mirrors the problem and results in a program that’s easier to understand and debug. • Another reason to choose a recursive solution is that an iterative solution is not apparent. • Avoid using recursion in performance situations. Recursive calls take time and consume additional memory. Image Credit: everythingcomputerscience.com
  • 21. Today’s In-Class Programming Challenge: Phonewords • Given a phone number, 800-356-9377, write a recursive C++ program to generate as many words out of these numbers as possible. • 2 (a,b,c), 3 (d,e,f), 4(g,h,i), 5 (j,k,l), 6 (m,n,o), 7 (p,q,r,s), 8 (t,u,v), 9 (w,x,y,z) • Ignore trying to convert the “800” into a word. Image Credit: www.acma.gov.au
  • 22. What’s In Your C++ Toolbox? cout / cin #include if/else/ Switch Math Class String getline While For do…While Break / Continue Arrays Functions

Hinweis der Redaktion

  1. 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.