The C++ and C programming languages are the two most popular programming languages in the software development world. Their raw speed and versatility have made them consistently at the top of ‘league tables’ of software development languages.
The C Programming Language was developed in the 1970s and since 1970, there have been many programming languages and have been many changes in C language, like C+, C++, CLANG standards C++99, C++11, C++14, and C++17. C++ programming language includes C language and is powered by Object Oriented Programming features like Classes, Objects and Methods. Still, this venerable language is popular, frequently appearing in the top 3 programming language choices – but if we consider its other variations and its usage on microchips and IoTs it is the most used programming language.
The C++ and C programming languages support recursion. Recursion can be a very useful technique in software development. But what is recursion and how do we use recursion in C++ and C programming?
Table of Contents
What is recursion in C++ and C programming?
Recursion means the process of repeating things in itself. In C and C++, if you create a function to call itself, it is called a recursive function, recursive procedure, recursive call, or recursive method. Recursive functions allows you to call the same function from within the function itself to reprocess data with different steps of calculations.
Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.
You can try many variations in all problems in few seconds by using the recursive functions. Let’s see some recursion examples in the C++ and C programming language.
What is an example of recursion in C programming?
This is not a good example to run but a good example for the infinitive recursion. If you run this in debug mode, your application will continuously call itself and will never stop. To stop the program once it is running you will need to manually hit the “stop”, “break” or “pause” button and terminate the program in the C++ IDE.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> void my_recursive() { my_recursive(); } int main() { my_recursive(); return 0; } |
When you use recursion, you need to be very careful to define an exit condition from the function or the relevant code may go into an ‘infinite loop’ until it either crashes the app or locks up the computer’s operating system. To avoid this, generally we use depth limit to exit from these kind of recursions. You can limit your depth, or you can limit number of recursions by counting them, otherwise it will go into an infinite loop as given above. Generally they are limited in the number of depth levels or search stops in the last depth level completion after an action ( a move of player, end of time period, etc.) .
Here is a C example of a recursive function where the depth limit is 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> void my_recursive(int depth) { printf("%d\n", depth); if(depth<5) my_recursive( depth+1 ); printf("\n"); } int main() { my_recursive(0); getchar(); return 0; } |
Is there an example of recursion in C++?
Here is a C++ example of a recursive function where the depth limit is 5,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> void my_recursive(int depth) { std::cout << depth << std::endl; if(depth<5) my_recursive( depth+1 ); std::cout << std::endl; } int main() { my_recursive(0); getchar(); return 0; } |
Is there a longer and more complete example of recursion in C programming?
Most known example to recursive functions is a factorial function. You can create a recursive function that calculates the factorial of a given number using itself.
Another example is, the Brute Force Method is using recursive method to solve problems. It is a method of mathematical proof in which the statement to be proved is split into a finite number of cases or sets of equivalent cases, these each unique cases are checked to see if the proposition in question holds in all cases, if all case variations satisfy then we can say all is provided and this statement is true or false. These
If you are looking for more examples, Brute force method and examples can be also found here;
Fibonacci Numbers are sequence numbers called the Fibonacci sequence, and in mathematics, it starts with 0 and 1, and then each number is the sum of the two preceding ones. Fibonacci numbers are strongly related to Golden Ratio. Here is another recursive function example that generates Fibonacci numbers.
How is recursion useful in AI, machine learning and deep learning?
These recursive examples like Brute Force Method, are generally used in some machine learning (ML) and deep learning (DL) applications to search for the best tree branch, for example in chess and go games with depth limits. Recursive functions like the Brute Force method are able to solve full variations of a tree-search if the number of variations are less. In the wider professional community, they are heavily used, especially in AI applications, where they are used to calculate all variations including unnecessary variations. They cost a lot of calculations and because of this other ML, DL methods are mostly preferred in AI applications. Thus, for these kind of games and problems, nowadays other Deep Learning algorithms are preferred such as Reinforcement Neural Networks, Concurrent Neural Networks etc. Note that, if you consider speed of CPUs or GPUs and small variation problems, you can still use them easily, for example tic-tac-toe game is very easy today with recursive functions in addition to more complex algorithms such as Reinforcement Learning Algorithms like Q Learning, Genetic Algorithms other new and old AI methods can be applied too. Generally, any problem with a branch tree, needs recursive functions, including most of these methods of AI.
What other uses are there for recursion in C++ and C programming?
Recursive functions are also good in parallel processing or multi-core / multi-task applications. You can easily spread each recursion to a thread of CPU cores easily. Thus, recursive functions allows you to use maximum computational speed in small finite depths.
As you can see it’s very easy to create recursive functions and methods in C and C++.
What is the best article to read about recursion in C++ and C programming?
If you’re still not sure what recursion means and how it works, then try this article: https://learncplusplus.org/what-is-recursion-in-c-programming/
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. Why not download a free trial today and try out some of the examples in this article?
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition