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