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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs