<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel McCoy on Medium]]></title>
        <description><![CDATA[Stories by Daniel McCoy on Medium]]></description>
        <link>https://medium.com/@danmmccoy?source=rss-1bd1ae5e0b61------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*IIxIKUA6TMI9UwcH.jpeg</url>
            <title>Stories by Daniel McCoy on Medium</title>
            <link>https://medium.com/@danmmccoy?source=rss-1bd1ae5e0b61------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 09:28:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danmmccoy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Recursion for Non-Computer Scientists]]></title>
            <link>https://medium.com/@danmmccoy/recursion-for-non-computer-scientists-5d22af195e9?source=rss-1bd1ae5e0b61------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d22af195e9</guid>
            <dc:creator><![CDATA[Daniel McCoy]]></dc:creator>
            <pubDate>Tue, 10 Jun 2014 23:29:19 GMT</pubDate>
            <atom:updated>2014-06-10T23:29:19.690Z</atom:updated>
            <content:encoded><![CDATA[<h4>An overview of Recursion</h4><p>Recursion is usually one of the first Computer Science concepts that programmers are introduced to. I remember the first time that the concept was introduced to me I didn’t really understand and haven’t understood it fully until recently. My CS 101 instructor lectured us about stacks and recursive algorithms without defining what a call stack is or what types of problems recursion is used to solve. In this post I’ll outline the basics of recursion, what types of problems recursion is used to solve, the components of a recursive method, and talk about stacks and stack frames within the context of recursion.</p><pre><strong>def</strong> <strong>fibonacci</strong>(n)</pre><pre>	<strong>if</strong> n <strong>&lt;=</strong> 1 </pre><pre>		<strong>return</strong> n</pre><pre>	<strong>else</strong> </pre><pre>		<strong>return</strong> fibonacci(n<strong>-</strong>1) <strong>+</strong> fibonacci(n<strong>-</strong>2)</pre><pre>	<strong>end</strong></pre><pre><strong>end</strong></pre><pre>puts fibonacci(4) <em>#3</em></pre><p>Above you’ll see one of the most common examples of recursion. The method takes a single parameter, n, a number, and returns its factorial. If you’re new to recursion you might be surprised to notice that the method is actually calling itself. This, in a nutshell, is all that recursion is.</p><h4>So, that’s it?</h4><p>Well, that’s the high level overview of recursion. Now it’s time to get into the finer points.</p><p>Recursion is used when the solution to a problem depends on the solutions to smaller instances of the problem. Take the above method which finds the nth number in the fibonacci sequence. In order to figure out what the 200th number in the fibonacci sequence is you must first know what the other 199 numbers are that precede it. Thus, the answer to our problem relies on the solutions to those smaller problems.</p><pre><strong>def</strong> <strong>fibonacci</strong>(n)</pre><pre>	<strong>if</strong> n <strong>&lt;=</strong> 1 </pre><pre>		<strong>return</strong> n</pre><pre>	<strong>else</strong> </pre><pre>		<strong>return</strong> fibonacci(n<strong>-</strong>1) <strong>+</strong> fibonacci(n<strong>-</strong>2)</pre><pre>	<strong>end</strong></pre><pre><strong>end</strong></pre><pre>puts fibonacci(4) <em>#3</em></pre><p>The above method is comprised of a simple if/else statement. Within the if statement we are checking to see if the value of n is greater or equal to 1. If so, we return n. This is referred to as a ‘base case’. Base cases are important because without one a method will continually call itself, won’t return anything, and crash with a ‘Stack Level Too Deep’ error.</p><p>In this scenario we know that n being less than 1 is our base case because by definition the first two values in the fibonacci sequence are 0 and 1. So, if n is less than 1 we’ll return n. Base cases are always based off of a known fact or value like this. Once the base case has been met the recursive method calls stop and the method returns.</p><h4>Cool, but what’s my computer doing throughout all of this?</h4><p>Earlier I mentioned a ‘Stack Level Too Deep’ error. This should give you a clue as to what your computer is doing when it executes a recursive method. As you might expect it has something to do with a stack.</p><h4>Stacks &amp; Stack Frames</h4><pre><strong>def</strong> <strong>do_math</strong> (a, b)</pre><pre>	return addition(a, b)</pre><pre><strong>end</strong></pre><pre><strong>def</strong> <strong>addition</strong>(a , b)</pre><pre>	<strong>return</strong> a <strong>+</strong> b</pre><pre><strong>end</strong></pre><pre>do_math(1, 3) <em># 4</em></pre><p>A call stack is data structure that is stored in memory that contains information about the methods being run in a program. Call stacks exist for the main purpose of keeping track of where to return control to once a method continues executing.</p><p>In the example above the do_math method calls the addition method. When the addition method is called within the do_math method control of the program is passed to the addition method. When the addition method finishes executing and returns the program needs to know where to return control to. It does this by looking in memory at the call stack. The do_math method regains control of the program and finishes executing.</p><p>Within a call stack there are stack frames. A stack frame stores information about a method (ex. variables and their values). A stack frame is created every time a method is called.</p><h4>Stack Frames In Recursion</h4><pre><strong>def</strong> <strong>factorial</strong>(n)</pre><pre>	<strong>if</strong> n <strong>==</strong> 0</pre><pre>		<strong>return</strong> 1</pre><pre>	<strong>else</strong></pre><pre>		<strong>return</strong> n <strong>*</strong> factorial(n<strong>-</strong>1)</pre><pre>	<strong>end</strong></pre><pre><strong>end</strong></pre><pre>puts factorial(5) <em>#120</em></pre><p>Let’s say we call the above fibonacci method passing it the value of 4. A stack frame is immediately added to the call stack. Within the stack frame the method variables and a pointer to which method called it are stored. The inital value does not fulfill our base case to Wthe else block is executed and the method calls itself again. At this point the initial method call hasn’t reached completion. So, another stack frame is added to the call stack storing the value of n as n-1 which in this case is 4 and a pointer to which method called it (the initial method call). This continues (creating four stack frames in total) until we hit our base case. Now that the base case has been fulfilled and recursive calls have stopped, control is returned to the 3rd stack frame where n is 2. The method executes 2 * factorial(1) which evaluates to 2. Then control is returned to the 2nd stack frame where n is equal to 3 and the method executes 3 * factorial(2) which evaluates to 6. Then control is returned to the first stack frame on the call stack. After this point there are no stack frames left on the call stack. The initial method call will finally finish executing where variable n is equal to 4,<br>4 * factorial(3) which evaluates to 24.</p><p>Since you now know that a call stack is simply a segment of allocated memory you can probably infer what a ‘Stack Level Too Deep’ error means. Without a base case a method calls itself over and over again; creating a stack frame each time. Since the memory allocated to a call stack is bound and data is constantly being added to the stack it’s only a matter of time before the call stack runs out of memory. Thus, the stack becomes to deep.</p><p>I hope that I’ve helped demystify recursion for you. Always remember the two most important things: a base case and to make the method call itself.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d22af195e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>