0% found this document useful (0 votes)
19 views55 pages

Functions

Uploaded by

korichigpt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views55 pages

Functions

Uploaded by

korichigpt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Functions &

Recursion

By : MEISSA Marwa
Functions
What is a function
A function in programming is a block of code designed
to perform a specific task. You can think of a function as
a "mini-program" within a program, which can be called
multiple times throughout the code.
( Subprogram, Subroutine ..etc)
Why function is important
•Reusability: Avoids repeating code.
•Organization: Breaks code into manageable parts.
•Simplicity: Reduces redundancy and complexity.
•Abstraction: Hides complex details.
•Maintainability: Easier to update and fix.
•Testability: Simplifies individual testing.
Types of function
•Library Functions: Functions provided by C standard
libraries that you can use directly (e.g., printf(), scanf()).
•User-Defined Functions: Functions written by the
programmer to perform specific tasks, defined with a name,
return type, and parameters (e.g., int add(int a, int b)).
Types of functions
•Library Functions: Functions provided by C standard
libraries that you can use directly (e.g., printf(), scanf()).
•User-Defined Functions: Functions written by the
programmer to perform specific tasks, defined with a name,
return type, and parameters (e.g., int add(int a, int b)).
Types of functions
In C programming language, functions can be called either with or
without arguments and might return values. They may or might not
return values to the calling functions.
• Function with no arguments and no return value
• Function with no arguments and with return value
• Function with argument and with no return value
• Function with arguments and with return value
The syntax of functions
The syntax of function can be divided
into 3 aspects:
1.Function Declaration
2.Function Definition
3.Function Calls
Function declaration
Function declaration

int add (int a, int b)


Function definition
Function definition
Function definition
Function calling
Function calling
Procedures
What is a procedure
procedure is a block of code that performs a task but
does not return a value (or result).
Procedures are used to execute commands or actions,
like printing something to the screen or modifying data,
without needing to return anything.
Variables
Variables
In C programming, understanding different types of variables and
their scope is essential for writing efficient code.
• Global / Local
• Actual / Formel
• Static / Automatic
Global Vs Local
Actual Vs Formel
Static Vs Automatic
Recursion
Divide and conquer
Technique involves breaking down a large problem into smaller
subproblems, solving each one recursively, and combining their
solutions to solve the original problem.
Advantages of Divide and conquer

1. Solves complex problems: by breaking them into


smaller subproblems.
2. Improves efficiency: in algorithms like quicksort.
3. Supports parallelism: making it ideal for multi-
processor systems.
4.Optimizes memory access: by using cache efficiently.
Recursion in C
Recursion in C refers to a programming technique
where a function calls itself directly or indirectly to
solve a problem. This technique is commonly used to
solve problems that can be broken down into smaller,
similar subproblems.
Syntax of Recursive Functions
Example
Example
The three rules in Recursion
 Base Case: The recursion stops when \( n = 1 \), returning 1.

 State Progression: The value of \( n \) decreases with each


recursive call, leading to the base case.
 Recursive Call: The function calls itself as ensuring recursion
continues until termination.
Types of C Recursion
Direct Recursion
Direct recursion is the most common type of recursion,
where a function calls itself directly within its own
body. The recursive call can occur once or multiple
times within the function due to which we can further
classify the direct recursion
Head Recursion
The head recursion is a linear recursion where the
position of its only recursive call is at the start of the
function. It is generally the first statement in the
function.
Head Recursion example
Head Recursion sum( 4)+ 5

sum( 3)+4

sum( 2)+3

sum( 1) +2

sum( 0)+ 1
Tail Recursion
The tail recursion is also a liner recursion like head
recursion but the position of the recursive call is at the
end of the function. Due to this, the tail recursion can
be optimized to minimize the stack memory usage. This
process is called Tail Call Optimization.
Tail Recursion example
5+ sum( 4)

4+ sum( 3)
Tail Recursion

3+ sum( 2)

2+ sum( 1)

1+ sum( 0)
Head Recursion vs Tail Recursion
Tree Recursion
In tree recursion, there are multiple recursive calls
present in the body of the function. Due to this, while
tracing the program flow, it makes a tree-like structure,
hence the name Tree Recursion.
Example of Tree Recursion
Example of Tree Recursion
Indirect Recursion (Mutual)
Is an interesting form of recursion where a function calls
another function, which eventually calls the first function
or any other function in the chain, leading to a cycle of
function calls. In other words, the functions are mutually
recursive. This type of recursion involves multiple functions
collaborating to solve a problem
Example of Indirect Recursion
Applications
 Tree and Graph Traversal: Recursive methods are useful for
thoroughly exploring and accessing nodes in trees and graphs.

 Divide-and-Conquer Algorithms: Algorithms like binary search use


recursion to break problems into smaller sub-problems, solving them
efficiently.

 Fractal Generation: Recursive algorithms are employed to create


fractals, such as the Mandelbrot set, by repeatedly applying complex
mathematical operations.

 Backtracking Algorithms: These algorithms use recursion to explore


possible solutions by making decisions step-by-step, backtracking
when a decision leads to an incorrect path.
Advantages
 Simplicity: Code gets cleaner and uses fewer needless
function calls.
 Elegant solution: Helpful for resolving difficult algorithms
and formula-based challenges.
 Tractable approach: They are useful for traversing trees and
graphs because they are naturally recursive.
 Code reusability: Recursive functions in C can be used
repeatedly within the program for different inputs, promoting
Disadvantages
 Complexity: It gets challenging to read and
decipher the code.
 Stack consumption: The copies of recursive
functions take up a significant amount of memory.
 Performance: Recursive calls can be slower and
less efficient than iterative solutions due to function
call overhead and repeated calculations.
 Limited applicability: Not all problems are well-
suited for C recursion, and converting iterative
solutions into recursive ones may not always be
practical or beneficial
Conclusion
Any problem that can be solved using iteration can
also be solved with recursion and vice versa.

So, C recursion is just one of the methods to solve problems of


different kinds. It provides a lot of benefits but also comes with certain
limitations. It should be used when its benefits outweigh its
limitations.
What is the difference
between Iteration and
recursion ?

You might also like