What is recursion? Are while and for loops examples of recursions?
Recursion is when a system calls itself as part of the natural process. The definition is: logic, maths the application of a function to its own values to generate an infinite sequence of values. The recursion formula or clause of a definition specifies the progression from one term to the next, as given the base clause f (0) = 0, f ( n + 1) = f ( n ) + 3 specifies the successive terms of the sequence f ( n ) = 3 n An example would be calculating a factorial: def factorial(n): total = 0 if n == 1 or n == 0: return 1 else: total = n * factorial(n-1) return total print(factorial(0)) Notice the line, total = n * factorial(n-1), this is where the function calls itself to continue the natural process of calculating the factorial of a number.
Another way to think about is as a circular argument or circular thinking. While a recursive programming statement is not the same as circular reasoning, the concepts do share some similarities. Let's take a classical example. You want to explain why something can float, so you say "Whatever is less dense than water will float, because whatever is less dense than water will float" according to Cederblom and Paulsen, this is a circular argument. You define something one way then use that definition to explain itself. This makes a circle where the "whatever is less dense than water will float" just gets repeated and repeated as to why "whatever is less dense than water will float" is true. Eventually, someone comes up, slaps you and says, "Silly! You mean it is because such objects don't sink in water!" Then, the argument has a conclusion. So how is a recursive function similar to this? You call the function to do a job, but it only does a little of it and as part of the process it calls itself! On and on, it does a bit and calls itself. Eventually it reaches what is called the simplest case and the if statement finally gets slapped by else and bang, there is a real solution that suddenly gets returned, which helps solve the call before it, which returns and resolves the call before that, and so on and so forth until it hands back a real answer that is made up of all the partial answers.
And a while or for loop may be used inside a recursion, but they are not one.
recursion is a function within a function... not like while and for loops they can be as is ... they can be nested or not
@LemuelPogi "recursion is a function within a function..." not quite. It is a function within itself. So not just any function within a function, but a function that calls itself.
yah yah XD thanks for correcting me i was doing my machine project regarding x86 assembly language hahha
in another language recursion is a programming method of solving large and complex problems by breaking the problem down into a base case or solving a small part of the problem one step at a time till the whole problem is solved. a good example of recursion is the palindrome. it is worthwhile to notice that in recursion we mostly use conditionals and function rather that loops.
Join our real-time social learning platform and learn together with your friends!