thought for the day: to understand the concept of recursion, you must first understand what is recursion ;-) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

thought for the day: to understand the concept of recursion, you must first understand what is recursion ;-)

6th Nov 2016, 6:26 PM
gowtham
gowtham - avatar
2 Answers
0
It's just when a function or method calls itself. For example: factorial function Say you were to enter a 3 as the parameter, it would return 1 * 2 * 3. Or if you enter 1 or 0, returns 1. And if the parameter doesn't have a factorial it returns -1. long long factorial(long long num) { switch (num) { case 0: case 1: return 1; default: if (num > 0) return factorial(num - 1) * num; return -1; } }
6th Nov 2016, 6:43 PM
Geoffrey Casper
Geoffrey Casper - avatar
0
Some simple example of reccursive function calcutating N fibonacci's number: int fib(int n) { if(n==1 || n==2) return n; return fib(n-1)+fib(n-2) }
7th Dec 2016, 5:02 PM
Maciej Falbogowski
Maciej Falbogowski - avatar