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. You can learn C++ and the C language easily from our website. In this article we will teach you how you can make a function repeat itself. We call these kind of functions recursive functions. So how we can repeat our app again and again?
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
How to make a program repeat itself in C++?
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 allow you to call the same function from within the function itself to reprocess data with different steps of calculations.
A good way to understand recursion is to imagine that you have the blueprint for a house. You follow the blueprint’s instructions and build your house. Now your daughter or son asks you to build them a doll’s house “that looks exactly like our house”. You use the same blueprint for the house, but you adjust all the measurements (the parameters) of the house’s blueprint, so the doll’s house is 1/10th the size of the real house. You build the doll’s house inside the real house, same instructions, with a small variation on the parameters. You can then repeat those instructions again, but this time you make a little house, while you are inside your bigger real house, for your backyard for the children to play in. It’s an imperfect example of recursion, but you get the general idea.
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.
Is there a program example of how to make a program repeat itself in C++?
If you really want your app completely to repeat itself, you should call your main application again and again. In C++ it is easy to call your main()
function. You just need to use main()
function, that’s all! Here is an example that is calling its main function itself in C++.
1 2 3 4 5 6 7 |
#include <iostream> int main() { std::cout << "This is recursive app! "; main(); } |
As you see we simply call our main application to make our application recursive. This will result the run of infinitive main functions as below,
The output will be infinitive repeat of the main function. This is faster than the next example below, because it doesn’t require you to call the system or have the overhead of loading and running executable.
Note that you must let users break, or stop these kind of applications. Otherwise it will continue to run and it will create new task apps which may result out of memory at the end. In this example, we can say that somehow our main function is recursive application itself.
Is there a Windows example of how to make a program repeat itself in C++?
In another example, If you want your app completely repeat itself, you can run your executable application again and again. This will be slower than the example above because of using system()
command that allows app to use system and loading and executing application. In C++, it is easy to call your executable C++ application. You just need to use system command to run your application. Let’s assume your C++ app name is “myrecursive
” and its C++ code should be as below,
1 2 3 4 5 6 7 |
#include <iostream> int main() { std::cout << "This is recursive app! "; system("myrecursive.exe"); } |
As you see we simply call our main application via the system
command in C++. This will result in running apps forever (until the system runs out of resources) as above but slower than the example above.
What is an example which shows how to make a program repeat itself in C++ or C compilers?
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 kinds 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 how to make a program repeat itself 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 a program repeat itself in C++?
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 the Golden Ratio. Here is another recursive function example that generates Fibonacci numbers.
How to make a program repeat itself in C++ that is useful in AI, machine learning and deep learning?
These recursive examples like the Brute Force Method and 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 is 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 this kind of games and problems, nowadays other Deep Learning algorithms are preferred such as Reinforcement Neural Networks and Concurrent Neural Networks. Note that, if you consider speed of CPUs or GPUs and small variation problems, you can still use them easily, for example the 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.
How to make a program repeat itself in C++ for other purposes?
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 IDE and compiler to make a program repeat itself in C++?
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?