A function that calls itself is claimed to be recursive. This process can also be performed indirectly if the function first calls another function or multiple functions before it is called
once more.

But an opportunity criterion is usually necessary to avoid having the call itself infinitely. The concept of local objects makes it possible to define recursive functions in C++.

Recursion requires local objects to be created each time the function is called, and these
objects must not have access to any other local objects from other function calls.

What effectively happens is that the local objects are placed on the stack, and thus the object created last is destroyed first.

Simple Program a recursive function

which reads a line from the keyboard and outputs it in reverse order.

using namespace std;
void getput(void);
int main()
cout << "Please enter a line of text:n";
cout << "nBye bye!" << endl;
return 0;
void getput()
char c;
if( cin.get(c) && c != 'n')

output In Your Screen Like this:-


Let’s look at the principle of recursion by referring to the sample program opposite. The
program contains the recursive function getput() that reads a line of text from the
keyboard and outputs it in reverse order.

The function getput() is first called by main() and reads a personality from the keyboard, storing it within the local variable c.

If the character is not ‘n’, the function getput() calls itself again and thus reads a further character from the keyboard before storing it in the local variable c.

The chain of recursive function calls is terminated by the user pressing the Return
key. The last character to be read, ‘n’ (line feed), is output and the program flow
branches to the previous getput() instance.

This outputs the second to last character, and so on. When the first character to have been read has finally been output, the program flow is handed back to main().

Recursive functions allow you to formulate this kind of logic in an efficient and elegant manner. However, always make sure that sufficient memory is available for the stack.

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Do you like Zubeen's articles?  Follow on social!
Comments to: What is recursion in Cpp with example

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories


Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page


Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
    Strength indicator
    Log In | Lost Password