O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Recursion

Recursion is the act of a function calling itself. The concept is very theoretical, so it's best understood by working with concrete examples. Say you want to create an application that determines the next highest prime number after a submitted value. The process would work like this:

1.
Increase the submitted number by 1 (because you want to find the next highest number).
2.
Check if this new number is prime (by checking the remainder when dividing that number by every prime that comes before it).
3.
If the number is not prime, go back to Step 1.
4.
If the number is prime, return it.

As a recursive function, this process could be written like so:

 int find_prime(int num) { short int is_prime = 0; // Flag ++num; // Calculations ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required