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 ...
Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.