recursion
i don't know how recursion works therefore I need someone to explain what it is, I may understand it that way
9 Replies
⌛
This post has been reserved for your question.
Hey @Omidd! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
sum(10)=10+sum(9)=10+9+sum(8)+...
sum(9)=9+sum(8)
....
sum(1)=1+sum(0)
sum(0)=0
It likes a loop that ends when it reaches 0
10+(9+8+7+6+5+4+3+2+1)=10+sum(9)
So you can write it like this
T(n)=n+T(n-1) & T(0)=0
Recursion in Java (or any programming language) is a technique where a method calls itself
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
if you don't understand recursion, I would try to visualize the call stack of a java program. When you call methods inside methods, the context of the original method is saved in a stack data structure. When a recently called method returns, it's popped off the stack. There is nothing special about recursion, it's just calling a method and that method call happens to be in the same method called.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
In a recursive method there are two parts : the recursive part where the method calls itself, and the stopping condition, where the recursion stops and the current call returns the value. In your example you have the recursive part where if the sum is bigger than zero you call the method with a value that tends to the stopping condition which is being equal to zero. When that condition is reached in one recursive call, the method returns and so on until the original caller is reached and you have the result. hope it helps
I recommend you study the call stack and data frame to help you understand recursion. The Stack data structure. There's a popular Python video on Youtube on this topic presented at Python presentation.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.