Recursion : What is Function Recursion
Printable View
Recursion : What is Function Recursion
A recursive function is a function that calls itself. It uses a base step and a recursive step.
Can you explain it by giving a simple example of addition or multiplication plezz
Consider a factorial of a number. Let's go with this as a definition: n! = n * n-1 * n-2 .... * 1
Now let's talk about 5 factorial, written as "5!".
5! can be written two ways.
- 5! = 5 * 4 * 3 * 2 * 1
- 5! = 5 * 4!
The first view is iterative. The second is recursive.
There's your example.
A recursive multiplication (very stupid implementation, but it does the required task):
The above code will only work for relatively small numbers, as there is a limit to how much stack you can use [Pedant prevention: Yes, a good compiler will probably make the tail-recursion into a loop].Code:int mul_rec(int x, int y)
{
if (x == 0) return 0;
if (x == 1) return y;
return mul_rec(x - 1, y);
}
--
Mats
need and explanation about addition and subtraction of numbers entered through an 1 D array
Do your own homework