0% found this document useful (0 votes)
2 views

Unit-III

Unit III covers functions and recursive functions in C++. It explains the structure and syntax of functions, including user-defined and standard functions, as well as the concept of recursion, its types, and the importance of base cases. The document emphasizes the benefits of using functions for code organization and reusability, alongside the principles of recursion for solving complex problems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit-III

Unit III covers functions and recursive functions in C++. It explains the structure and syntax of functions, including user-defined and standard functions, as well as the concept of recursion, its types, and the importance of base cases. The document emphasizes the benefits of using functions for code organization and reusability, alongside the principles of recursion for solving complex problems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Unit III: Functions and Recursive Functions

function Calling a function

• is a reusable block of code designed to • To call the greet() function, we use:


perform a specific task
Example 1: Displaying a text
• functions developed by the programmer
to implement in the program void greet() {
• or a subprogram cout<<”Hello World!”;
• simply a chunk of C++ code that has: }
1. descriptive function name Int main() {
2. returning value (for value returning) //calling a function
3. sub-process (for non-value returning) greet();
}

Two parts of function: Example 2: With Parameters

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

returnType functionName (parameter1…) {


// function body (Definition)
}

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

Example 3: Add two numbers codes defined in the body of the


function
• These functions help organize code into
logical blocks, making it more readable
and reusable
• also called Returning value functions;
functions that perform a task and then
return a value to the calling code

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

Syntax: pow standard function in <cmath> library


returnType functionName(dataType1, dataType2);
cout<<”2 raised to 3 is “<<pow(2,3);

User-defined Function (Descriptive Functions) Output:


• C++ allows the programmer to define 2 raised to 3 is 8
their own function
• A user-defined function groups code to
perform a specific task and that group of
code is given a name (identifier)
• When the function is invoked from any
part of the program, it all executes the

CC-103
Unit III: Functions and Recursive Functions

Sub-processes (Non-Returning Value Functions)


• also known as void functions, perform
tasks but do not return a value.
• These functions are used when the task
does not require any return value

Incorrect Usage of Functions


1. Incompatible data type -> string sum =
add(4,5);
2. Insufficient arguments -> cout << “The sum
is “ << add(4);
3. Incorrect data type arguments -> cout <<
“The sum is “ << add(4,’a’);
4. Too much arguments supplied -> int sum =
add(4,5,6);

CC-103
Unit III: Functions and Recursive Functions

Recursion Why do we need recursion?

• 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

n==0 is the base case that will terminate the


iteration

return(n-1) is the recursive function that will help


in the repetition of code

Algorithmic steps for implementing recursion in


a function:

1. define a base case


2. define a recursive case
3. ensure the recursion terminates
4. combine the solutions

CC-103
Unit III: Functions and Recursive Functions

Types of Recursion • Simplest example that the function can


solve without returning
Direct Recursion
2. Forward Progress
• The function calls itself directly • Every recursive function should advance
which implies that with each recursive
call, it should get closer to the base case.
• In other words, the function must mover
closer to the answer.
3. Design Rule
• Every recursive function should solve a
Indirect Recursion problem by breaking it down into one or
more smaller instances of the same
• If a function calls itself indirectly from problem.
another function
• In other words, the function should
decompose the issue into more
manageable subproblems.

Finite Recursion

• Occurs when the recursion terminates


after a finite number of recursive calls
• A recursion terminates only when a base
condition is met

Infinite Recursion

• Occurs when the recursion does not


terminate after a finite number of
recursive calls
• As the base condition is never met, the
recursion carries on infinitely

Three Laws of recursion

1. Base Case
• Needs to have a base case or the
circumstance in which the recursion
ends

CC-103

You might also like