Unit-III
Unit-III
1. Declaration
• The return type, the function name, and
the parameters (if any)
2. Definition
• Body of the function (code to be
executed)
Syntax:
// Declaration
Return Statement
Parameters • Also possible to return a value from a
function. For this, we need to specify the
• Act as the variable inside the function returnType of the function
• Variables listed as part of a function's • Can be used to return a value from a
declaration and definition function
• May have 1 or more parameters to
perform its task
Arguments
o Here we have data type int instead of void.
• Actual values passed to the function o This means that the function returns an int
when it is called. value.
• These values replace the parameters in o return a + b; = returns sum of two parameters
the function definition during execution o The return statement denotes that the
function has ended.
o Any code after return inside the function is
not executed.
CC-103
Unit III: Functions and Recursive Functions
Function Prototype
• a declaration of a function that informs
the compiler about the function's name, Benefits of Using User-Defined Functions
return type, and parameters before the • Functions make the code reusable. We
function is actually defined can declare them once and use them
multiple times.
• Functions make the program easier as
each small task is divided into a function
Standard Functions
• predefined functions provided by the
C++ Standard Library
• These functions perform common tasks
and are readily available for use in your
• if we want to define a function after the programs
function call, we need to use the • Groups in different libraries which can
function prototype be included in the C++ program
CC-103
Unit III: Functions and Recursive Functions
CC-103
Unit III: Functions and Recursive Functions
• method in C++ which calls itself directly • A potent programming approach that
or indirectly until a suitable condition is involves breaking a problem down into
met smaller, easier subproblems in order to
• the process of solving a problem by solve it.
reducing it to smaller versions of itself
• the process of calling the function by
itself as its subroutine to solve the • Code Simplified. Programmers can
complex program create code that is elegant and concise
by using recursion. This is due to the fact
that recursion functions can frequently
Two parts of recursion: be written in less code than their
iterative equivalents
1. recursive condition = helps in the
• Solves difficult Issues. Complex
condition of code again and again
problems that are challenging or
2. base case = helps in the termination of
impossible to solve using iterative
the condition
methods benefit greatly from recursion
• Improving Readability. Compared to
iterative functions, recursive functions
are frequently simpler to read and
comprehend. Recursive functions are
more understandable since they can
more nearly resemble the issue they are
attempting to solve.
if there is no base case in the recursive function,
the recursive function will continue to repeat
continuously
CC-103
Unit III: Functions and Recursive Functions
Finite Recursion
Infinite Recursion
1. Base Case
• Needs to have a base case or the
circumstance in which the recursion
ends
CC-103