DEV Community

Cover image for C - Recursion
Haile Melaku
Haile Melaku

Posted on

1

C - Recursion


#C - Recursion

Recursion is a method of calling a function inside a function iteratively.

void recursion() {
   recursion(); /* function calls itself */
}

int main() {
   recursion();
}
Enter fullscreen mode Exit fullscreen mode

In this case the function recursion calls a copy of itself to work on a smaller problem until some conditional is satisfied.

Any function which calls itself is called recursive function, and such function calls are called recursive calls.

Recursion is a method of solving problems based on the divide and conquer mentality and we break the problem into much smaller sub part's of itself, one of the subpart consists the a conditional to break the recursion.

Example

Creating a recursive function to find the factorial of a number

Image description
Output

Image description

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay