CC++C++11C++14C++17Generic ProgrammingIntroduction to C++Learn C++

What Is Recursion In C Programming

What Is Recursion In C Programming

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?

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.

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:

Is there an example of recursion in C++?

Here is a C++ example of a recursive function where the depth limit is 5,

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:

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?


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
CC++Introduction to C++Learn C++

How To Run A C Program in Command Prompt

CC++C++17Learn C++

How To Compile And Run A C Program

CC++Introduction to C++Learn C++

What Kind Of Programming Language Is C?

CC++Introduction to C++Learn C++

What Does C Mean In Programming?