{"id":95,"date":"2026-05-20T11:34:27","date_gmt":"2026-05-20T11:34:27","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=95"},"modified":"2026-05-22T08:14:45","modified_gmt":"2026-05-22T08:14:45","slug":"recursion","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=recursion","title":{"rendered":"Recursion"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Recursion in C++ is a programming technique where a function calls itself repeatedly to solve a problem. It is commonly used for tasks that can be divided into smaller subproblems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Recursion?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Recursion is a process in which a function calls itself until a specific condition is met.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A recursive function has two important parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Base Case \u2192 Stops the recursion<\/li>\n\n\n\n<li>Recursive Call \u2192 Function calls itself<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Recursion?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Recursion is useful because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simplifies complex problems<\/li>\n\n\n\n<li>Reduces code length<\/li>\n\n\n\n<li>Works well with hierarchical data<\/li>\n\n\n\n<li>Is commonly used in algorithms and data structures<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Recursive Function<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>return_type function_name(parameters) {<br><br>    if (base_condition) {<br>        return value;<br>    }<br><br>    return function_name(smaller_problem);<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Recursion<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>void countDown(int n) {<br><br>    if (n == 0) {<br>        return;<br>    }<br><br>    cout &lt;&lt; n &lt;&lt; endl;<br><br>    countDown(n - 1);<br>}<br><br>int main() {<br>    countDown(5);<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>5<br>4<br>3<br>2<br>1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Recursion Works<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Function is called<\/li>\n\n\n\n<li>Function performs a task<\/li>\n\n\n\n<li>Function calls itself with smaller input<\/li>\n\n\n\n<li>Process repeats<\/li>\n\n\n\n<li>Base case stops recursion<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Example: Factorial Using Recursion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Factorial formula:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><semantics><mrow><mi>n<\/mi><mo stretchy=\"false\">!<\/mo><mo>=<\/mo><mi>n<\/mi><mo>\u00d7<\/mo><mo stretchy=\"false\">(<\/mo><mi>n<\/mi><mo>\u2212<\/mo><mn>1<\/mn><mo stretchy=\"false\">)<\/mo><mo stretchy=\"false\">!<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">n! = n \\times (n-1)!<\/annotation><\/semantics><\/math>n!=n\u00d7(n\u22121)!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int factorial(int n) {<br><br>    if (n == 0 || n == 1) {<br>        return 1;<br>    }<br><br>    return n * factorial(n - 1);<br>}<br><br>int main() {<br>    cout &lt;&lt; factorial(5);<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>120<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example: Fibonacci Series Using Recursion<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int fibonacci(int n) {<br><br>    if (n == 0) {<br>        return 0;<br>    }<br><br>    if (n == 1) {<br>        return 1;<br>    }<br><br>    return fibonacci(n - 1) + fibonacci(n - 2);<br>}<br><br>int main() {<br>    cout &lt;&lt; fibonacci(6);<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Recursion<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Makes code shorter<\/li>\n\n\n\n<li>Solves complex problems easily<\/li>\n\n\n\n<li>Useful for tree and graph structures<\/li>\n\n\n\n<li>Improves code readability in some cases<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Disadvantages of Recursion<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Can use more memory<\/li>\n\n\n\n<li>May be slower than loops<\/li>\n\n\n\n<li>Incorrect base case can cause infinite recursion<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Recursion is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Recursion is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Helps solve divide-and-conquer problems<\/li>\n\n\n\n<li>Is widely used in algorithms<\/li>\n\n\n\n<li>Supports advanced data structures<\/li>\n\n\n\n<li>Simplifies repetitive problem-solving<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of standing between two mirrors:<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779437695725\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>One reflection creates another reflection repeatedly<\/li>\n\n\n\n<li>The process continues until it fades away<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is similar to recursion where a function keeps calling itself.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Recursion in C++ is a powerful programming technique where a function calls itself to solve smaller versions of a problem. It is widely used in algorithms, mathematical calculations, and advanced programming concepts.<audio autoplay=\"\"><\/audio><\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate C++ > Functions > Recursion<\/span><\/span><\/div>","protected":false},"menu_order":24,"template":"","class_list":["post-95","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Recursion - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ recursion with examples like factorial and Fibonacci to understand base case, recursive calls, and problem-solving techniques.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Recursion - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ recursion with examples like factorial and Fibonacci to understand base case, recursive calls, and problem-solving techniques.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T08:14:45+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=recursion\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Recursion - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T11:34:27+00:00\",\"dateModified\":\"2026-05-22T08:14:45+00:00\",\"description\":\"Learn C++ recursion with examples like factorial and Fibonacci to understand base case, recursive calls, and problem-solving techniques.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate C++ > Functions > Recursion\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Recursion - Learn C++Language with GiGz.PK","description":"Learn C++ recursion with examples like factorial and Fibonacci to understand base case, recursive calls, and problem-solving techniques.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Recursion - Learn C++Language with GiGz.PK","og_description":"Learn C++ recursion with examples like factorial and Fibonacci to understand base case, recursive calls, and problem-solving techniques.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T08:14:45+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=recursion","url":"https:\/\/gigz.pk\/","name":"Recursion - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T11:34:27+00:00","dateModified":"2026-05-22T08:14:45+00:00","description":"Learn C++ recursion with examples like factorial and Fibonacci to understand base case, recursive calls, and problem-solving techniques.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Intermediate C++ > Functions > Recursion"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}