{"id":40,"date":"2022-11-26T10:23:48","date_gmt":"2022-11-26T10:23:48","guid":{"rendered":"https:\/\/blog.metu.edu.tr\/e243521\/?p=40"},"modified":"2022-11-26T11:31:42","modified_gmt":"2022-11-26T11:31:42","slug":"python-recursion","status":"publish","type":"post","link":"https:\/\/blog.metu.edu.tr\/e243521\/2022\/11\/26\/python-recursion\/","title":{"rendered":"Python Recursion"},"content":{"rendered":"<h2 id=\"what\">What is recursion?<\/h2>\n<p>Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.<\/p>\n<h2 id=\"function\">Python Recursive Function<\/h2>\n<p>In Python, we know that a function can call other functions. It is even possible for the function to call itself. These types of construct are termed as recursive functions.<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-41\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r1-300x127.jpg\" alt=\"\" width=\"300\" height=\"127\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r1-300x127.jpg 300w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r1.jpg 618w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 3 (denoted as 3!) is 3*2*1. Quesition is that how to implement this function in python?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-42\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r2-300x179.jpg\" alt=\"\" width=\"300\" height=\"179\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r2-300x179.jpg 300w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r2.jpg 687w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>In the above example, factorial() is a recursive function as it calls itself. When we call this function with a positive integer, it will recursively call itself by decreasing the number. Each function multiplies the number with the factorial of the number below it until it is equal to one.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-43\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r3-262x300.jpg\" alt=\"\" width=\"262\" height=\"300\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r3-262x300.jpg 262w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r3.jpg 538w\" sizes=\"auto, (max-width: 262px) 100vw, 262px\" \/><\/p>\n<p>Our recursion ends when the number reduces to 1. This is called the base condition. Every recursive function must have a base condition that stops the recursion or else the function calls itself infinitely.<\/p>\n<div id=\"block-inject-1\" class=\"block-inject block-inject-1\">\n<div class=\"pgAdWrapper\">\n<div id=\"div-gpt-ad-Programizcom37046\" data-google-query-id=\"CLHcj_jPy_sCFfCFgwcd55cOTQ\"><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>What is recursion? Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. Python Recursive Function In Python, we know that a function can call other functions. It [&hellip;]<\/p>\n","protected":false},"author":8584,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[1],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-all-tutorial"],"_links":{"self":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/users\/8584"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":0,"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}