C++ Inline Functions and Recursion
C++ inline function is very useful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.
Basically an inline function code is re-written wherever the function call is encountered.
When function is called within the same function, it is known as recursion in C++. Using recursive algorithm, certain problems can be solved quite easily.
Consider a situation where we have to determine the factorial of first n natural numbers. Lets try to write a program for the same.
Factorial of number is basically the product of the number and product of all numbers preceding the number. So factorial of 6 will be 1*2*3*4*5*6=720
To perform this we can use a do while loop.
We can perform the same operation using a recursive function.
Looking at the above flow chart we can write a recursive function using the given syntax
Now lets write a code for factorial of a number using recursive function.
Caution! Recursion is very useful in certain situations but the use of recursion must be avoided unless you are very confident about what you are doing, recursion can wield undesired results if the code is not specific.