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:

Increase the submitted number by 1 (because you want to find the next highest number).
Check if this new number is prime (by checking the remainder when dividing that number by every prime that comes before it).
If the number is not prime, go back to Step 1.
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 ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.