{"id":77820,"date":"2020-04-01T13:56:33","date_gmt":"2020-04-01T08:26:33","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=77820"},"modified":"2020-04-01T13:56:33","modified_gmt":"2020-04-01T08:26:33","slug":"python-recursion","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/","title":{"rendered":"Python Recursion &#8211; A programmer&#8217;s most important weapon"},"content":{"rendered":"<p>Coders often joke,<strong> \u201cIn order to understand recursion, you must first understand recursion\u201d<\/strong>.<\/p>\n<p>Funny, right?<\/p>\n<p>Well if you didn\u2019t find it funny, scroll back up after reading this entire article on <strong>Python Recursion<\/strong> and we know you will like it for sure.<\/p>\n<p>And if you still don\u2019t like it, I have another one for you!<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/python-recursion-meme.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77877\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/python-recursion-meme.jpg\" alt=\"\" width=\"500\" height=\"633\" \/><\/a><\/p>\n<h3>What is Python Recursion?<\/h3>\n<p><strong>Recursion<\/strong> occurs when a <strong>function<\/strong> or <strong>algorithm<\/strong> <strong>calls itself<\/strong>. It is a <strong>problem-solving<\/strong> method that involves <strong>repetitive<\/strong> <strong>breaking down<\/strong> of a <strong>problem<\/strong> into a <strong>smaller instance<\/strong> of the <strong>same problem<\/strong>. We keep breaking down until we reach a problem that is <strong>small enough<\/strong> to be <strong>solved easily<\/strong>.<\/p>\n<p>We usually <strong>implement recursion<\/strong> through a <strong>recursive function<\/strong>.<\/p>\n<p>Let\u2019s learn the concept of <strong>recursion<\/strong> using the <strong>classic example<\/strong> of <strong>finding factorial<\/strong> of a <strong>number<\/strong>.<\/p>\n<p><strong> Factorial<\/strong> of a <strong>number<\/strong> is the <strong>product<\/strong> of <strong>all integers<\/strong> which are <strong>greater than 1<\/strong> and <strong>less than<\/strong> or <strong>equal to<\/strong> that <strong>number<\/strong>.<\/p>\n<p>For instance, the <strong>factorial<\/strong> of <strong>4 is 4 * 3 * 2 * 1<\/strong>.<\/p>\n<p>So basically, for any <strong>non-negative integer N<\/strong>, the factorial of <strong>N = N * factorial of (N &#8211; 1)<\/strong><\/p>\n<h3>Python Recursive Function<\/h3>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/python-recursive-function.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77871\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/python-recursive-function.jpg\" alt=\"\" width=\"802\" height=\"420\" \/><\/a><\/p>\n<p>A <strong>recursive function<\/strong> is one that <strong>invokes itself<\/strong> as a part of its <strong>execution<\/strong>.<\/p>\n<p>So if we have a function for <strong>calculating<\/strong> the <strong>factorial of a number<\/strong>, say <strong>factorial(n)<\/strong>, based on the above discussion we can say,<\/p>\n<p><strong>factorial(n) = n * factorial(n &#8211; 1)<\/strong><\/p>\n<h4>Cases in Python Recursive Function<\/h4>\n<ul>\n<li>The <strong>base case,<\/strong> which when satisfied, will <strong>terminate<\/strong> the <strong>recursive process<\/strong>. This is also known as the<strong> \u201cexit condition\u201d<\/strong>.<\/li>\n<li>The <strong>recursive case,<\/strong> which is where the <strong>recursion<\/strong> will <strong>actually occur<\/strong>. That means this is where the <strong>function<\/strong> will <strong>call itself<\/strong>.<\/li>\n<\/ul>\n<p><strong>Finally, let\u2019s put our recursive function for calculating the factorial of a number into code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def factorial(n):\n    # base case\n    if n == 1:\n        return 1\n\n    # recursive case\n    else:\n        return n * factorial(n-1)<\/pre>\n<p><strong>Run this code and in the Python shell call the function:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; factorial(10)\n3628800\n&gt;&gt;&gt; factorial(5)\n120\n<\/pre>\n<h3>How does Recursion work in <strong>Python<\/strong>?<\/h3>\n<p>To get a full understanding of the <strong>working process<\/strong> of <strong>recursion<\/strong>, we first need to learn about <strong>call stack<\/strong>.<\/p>\n<ul>\n<li>When you <strong>call a function<\/strong>, the system sets aside <strong>space<\/strong> in the <strong>memory<\/strong> for that <strong>function<\/strong> to do its <strong>work<\/strong>.<\/li>\n<li>Such chunks of <strong>memory<\/strong> are called <strong>stack frames<\/strong> or <strong>function frames<\/strong>.<\/li>\n<li>More than one <strong>function\u2019s stack frame<\/strong> may exist in the <strong>memory<\/strong> at a given time.<\/li>\n<li>We know that a function has the ability to <strong>call another function<\/strong>.<\/li>\n<li>So suppose<strong> start()<\/strong> <strong>calls move()<\/strong>, which then <strong>calls direction()<\/strong>, all <strong>3 functions<\/strong> have <strong>open frames<\/strong> in the <strong>memory<\/strong>.<\/li>\n<li>But only the <strong>function<\/strong> which is <strong>called recently<\/strong> has an <strong>active frame<\/strong>.<\/li>\n<\/ul>\n<p>These frames are <strong>arranged<\/strong> in a <strong>stack<\/strong>, called the<strong> \u2018call stack\u2019<\/strong>. The frame for the most recently <strong>called function<\/strong> is always on the <strong>top of the stack<\/strong>.<\/p>\n<p>When a <strong>new function<\/strong> is <strong>called<\/strong>, a <strong>new frame<\/strong> is <strong>pushed<\/strong> onto the <strong>top of the stack<\/strong> and becomes the <strong>active frame<\/strong>. When a function finishes its work, its <strong>frame<\/strong> is <strong>popped<\/strong> <strong>off<\/strong> from the <strong>stack<\/strong> and the <strong>frame<\/strong> immediately below <strong>becomes<\/strong> the <strong>active frame<\/strong>. This <strong>active function<\/strong> picks up immediately where it left off.<\/p>\n<p>When a <strong>function calls another function<\/strong>, the <strong>caller function<\/strong> is set on <strong>hold<\/strong> and the called <strong>function becomes active<\/strong>. Now the <strong>frame below<\/strong> this <strong>active frame<\/strong> has to <strong>wait until<\/strong> it again becomes <strong>active<\/strong> and <strong>resumes its work<\/strong>.<\/p>\n<p>So recalling our <strong>factorial code<\/strong>, let\u2019s understand how <strong>factorial of 5<\/strong> is calculated.<\/p>\n<p>Our <strong>factorial(n) function states<\/strong> that, if <strong>n<\/strong> is <strong>1<\/strong>, the function <strong>returns 1<\/strong>. Otherwise, the function returns <strong>n * factorial(n &#8211; 1)<\/strong>.<\/p>\n<p>So if we <strong>call factorial(5)<\/strong> it will in turn <strong>call factorial(4)<\/strong>, which will <strong>call factorial(3)<\/strong>, which <strong>calls factorial(2)<\/strong> and at last we reach <strong>factorial(1)<\/strong>.<\/p>\n<p>The functions then <strong>pop off<\/strong> from the <strong>stack one<\/strong> by one as follows:<\/p>\n<p><strong>1. factorial(1)<\/strong> returns 1 to the frame of <strong>factorial(2)<\/strong> and pops off.<\/p>\n<p><strong>2. factorial(2)<\/strong> then pops off after returning<strong> 2*1<\/strong> to the frame of <strong>factorial(3)<\/strong>.<\/p>\n<p><strong>3. factorial(3)<\/strong> then returns <strong>3*2*1<\/strong> to the <strong>factorial(4)<\/strong> frame.<\/p>\n<p><strong>4. factorial(4)<\/strong> returns<strong> 4*3*2*1<\/strong> to the frame of<strong> factorial(5)<\/strong><\/p>\n<p>5. And lastly,<strong> factorial(5)<\/strong> returns<strong> 5*4*3*2*1<\/strong> which equates to <strong>120<\/strong>.<\/p>\n<h3>Summary<\/h3>\n<p>That was all for <strong>Python Recursion<\/strong>.<\/p>\n<p>In this article, our aim was to make you understand how to <strong>implement recursion<\/strong> in <strong>python<\/strong>. And now that you have also learned what actually happens behind the <strong>scenes of recursion<\/strong>, I hope you will get that joke we mentioned earlier in the introduction of this article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Coders often joke, \u201cIn order to understand recursion, you must first understand recursion\u201d. Funny, right? Well if you didn\u2019t find it funny, scroll back up after reading this entire article on Python Recursion and&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":77871,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[2236,2237,2238,2239,2240,1328,2241],"class_list":["post-77820","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-cases-in-python-recursive-function","tag-how-python-recursive-work","tag-java-recursion-example","tag-python-recursion","tag-python-recursive-function","tag-recursion-in-python","tag-recursive-function-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Recursion - A programmer&#039;s most important weapon - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python Recursion - With this article learn the concept of Recursion in Python and also explore its functions, cases and know how to implement them in Python\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-recursion\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Recursion - A programmer&#039;s most important weapon - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python Recursion - With this article learn the concept of Recursion in Python and also explore its functions, cases and know how to implement them in Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-recursion\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-01T08:26:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-recursive-function.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Recursion - A programmer's most important weapon - TechVidvan","description":"Python Recursion - With this article learn the concept of Recursion in Python and also explore its functions, cases and know how to implement them in Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/","og_locale":"en_US","og_type":"article","og_title":"Python Recursion - A programmer's most important weapon - TechVidvan","og_description":"Python Recursion - With this article learn the concept of Recursion in Python and also explore its functions, cases and know how to implement them in Python","og_url":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-04-01T08:26:33+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-recursive-function.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Recursion &#8211; A programmer&#8217;s most important weapon","datePublished":"2020-04-01T08:26:33+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/"},"wordCount":704,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-recursive-function.jpg","keywords":["cases in python recursive function","how python recursive work","java recursion example","python recursion","python recursive function","recursion in python","recursive function in java"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-recursion\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/","url":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/","name":"Python Recursion - A programmer's most important weapon - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-recursive-function.jpg","datePublished":"2020-04-01T08:26:33+00:00","description":"Python Recursion - With this article learn the concept of Recursion in Python and also explore its functions, cases and know how to implement them in Python","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-recursion\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-recursive-function.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-recursive-function.jpg","width":802,"height":420,"caption":"python recursion"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-recursion\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Recursion &#8211; A programmer&#8217;s most important weapon"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=77820"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77820\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/77871"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=77820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=77820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=77820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}