explanation on recursion function in python











up vote
0
down vote

favorite












I am a beginner in python and am not getting the concept of recursion.can i get an explanation of the code below:



def rec(k):
if(k>0):
print('the value of k is:',k)
result = k+rec(k-1)
print(result)
else:
result = 0
return result
rec(8)


the output is:



the value of k is: 8
the value of k is: 7
the value of k is: 6
the value of k is: 5
the value of k is: 4
the value of k is: 3
the value of k is: 2
the value of k is: 1
1
3
6
10
15
21
28
36


How is it that we are not getting print(result) after each print('the value of k is',k)










share|improve this question




















  • 1




    You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
    – jonrsharpe
    Nov 21 at 15:20






  • 4




    Why would print(result) be the next thing that is being executed? Python is busy executing rec(k-1) first, and that function call also prints, etc.
    – Martijn Pieters
    Nov 21 at 15:26










  • Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
    – Martijn Pieters
    Nov 21 at 15:28










  • Put differently, why would Python already know what to print as result when the rec(k - 1) call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?
    – Martijn Pieters
    Nov 21 at 15:29















up vote
0
down vote

favorite












I am a beginner in python and am not getting the concept of recursion.can i get an explanation of the code below:



def rec(k):
if(k>0):
print('the value of k is:',k)
result = k+rec(k-1)
print(result)
else:
result = 0
return result
rec(8)


the output is:



the value of k is: 8
the value of k is: 7
the value of k is: 6
the value of k is: 5
the value of k is: 4
the value of k is: 3
the value of k is: 2
the value of k is: 1
1
3
6
10
15
21
28
36


How is it that we are not getting print(result) after each print('the value of k is',k)










share|improve this question




















  • 1




    You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
    – jonrsharpe
    Nov 21 at 15:20






  • 4




    Why would print(result) be the next thing that is being executed? Python is busy executing rec(k-1) first, and that function call also prints, etc.
    – Martijn Pieters
    Nov 21 at 15:26










  • Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
    – Martijn Pieters
    Nov 21 at 15:28










  • Put differently, why would Python already know what to print as result when the rec(k - 1) call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?
    – Martijn Pieters
    Nov 21 at 15:29













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am a beginner in python and am not getting the concept of recursion.can i get an explanation of the code below:



def rec(k):
if(k>0):
print('the value of k is:',k)
result = k+rec(k-1)
print(result)
else:
result = 0
return result
rec(8)


the output is:



the value of k is: 8
the value of k is: 7
the value of k is: 6
the value of k is: 5
the value of k is: 4
the value of k is: 3
the value of k is: 2
the value of k is: 1
1
3
6
10
15
21
28
36


How is it that we are not getting print(result) after each print('the value of k is',k)










share|improve this question















I am a beginner in python and am not getting the concept of recursion.can i get an explanation of the code below:



def rec(k):
if(k>0):
print('the value of k is:',k)
result = k+rec(k-1)
print(result)
else:
result = 0
return result
rec(8)


the output is:



the value of k is: 8
the value of k is: 7
the value of k is: 6
the value of k is: 5
the value of k is: 4
the value of k is: 3
the value of k is: 2
the value of k is: 1
1
3
6
10
15
21
28
36


How is it that we are not getting print(result) after each print('the value of k is',k)







python recursion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 15:25









Martijn Pieters

693k12923992236




693k12923992236










asked Nov 21 at 15:17









hellriser

124




124








  • 1




    You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
    – jonrsharpe
    Nov 21 at 15:20






  • 4




    Why would print(result) be the next thing that is being executed? Python is busy executing rec(k-1) first, and that function call also prints, etc.
    – Martijn Pieters
    Nov 21 at 15:26










  • Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
    – Martijn Pieters
    Nov 21 at 15:28










  • Put differently, why would Python already know what to print as result when the rec(k - 1) call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?
    – Martijn Pieters
    Nov 21 at 15:29














  • 1




    You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
    – jonrsharpe
    Nov 21 at 15:20






  • 4




    Why would print(result) be the next thing that is being executed? Python is busy executing rec(k-1) first, and that function call also prints, etc.
    – Martijn Pieters
    Nov 21 at 15:26










  • Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
    – Martijn Pieters
    Nov 21 at 15:28










  • Put differently, why would Python already know what to print as result when the rec(k - 1) call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?
    – Martijn Pieters
    Nov 21 at 15:29








1




1




You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
– jonrsharpe
Nov 21 at 15:20




You are getting the result printed afterwards, on the way back out of the stack of recursive calls.
– jonrsharpe
Nov 21 at 15:20




4




4




Why would print(result) be the next thing that is being executed? Python is busy executing rec(k-1) first, and that function call also prints, etc.
– Martijn Pieters
Nov 21 at 15:26




Why would print(result) be the next thing that is being executed? Python is busy executing rec(k-1) first, and that function call also prints, etc.
– Martijn Pieters
Nov 21 at 15:26












Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
– Martijn Pieters
Nov 21 at 15:28




Imagine a line of people that you give instructions: Accept a number, shout that number, then give the number minus 1 to the next person. When that next person hands you back their result, add that result to your number, shout the result of the addition, and then hand back the result to whomever had given you the first number. Do you think everyone will shout twice directly, or will they wait?
– Martijn Pieters
Nov 21 at 15:28












Put differently, why would Python already know what to print as result when the rec(k - 1) call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?
– Martijn Pieters
Nov 21 at 15:29




Put differently, why would Python already know what to print as result when the rec(k - 1) call hasn't completed yet? How would a person in your line of people know what to shout for the result without waiting for the next person to be done first, and why would they wait with shouting the first number when given?
– Martijn Pieters
Nov 21 at 15:29

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415161%2fexplanation-on-recursion-function-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415161%2fexplanation-on-recursion-function-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Futebolista

Jornalista