Browse Source

Started section on Recursive functions, needs more work

pull/1/merge
Brett Langdon 14 years ago
parent
commit
da879e6fb0
3 changed files with 43 additions and 8 deletions
  1. +31
    -0
      3 - Functional Programming/1.3 - Functions.tex
  2. BIN
      Programming In General.pdf
  3. +12
    -8
      Programming In General.tex

+ 31
- 0
3 - Functional Programming/1.3 - Functions.tex View File

@ -204,4 +204,35 @@ print SayHello( 3 )
This example will output the exact same as above but it only uses a single Return statement.
\subsection{Recursion}
Recursion, now this section is going to be FUN!
Please take your time reading through this chapter as it is very easy to get confused by Recursive Functions and can be difficult to wrap
your head around the first time through (I know I had issues when I first learned it).
Basically what it means for a Function to be recursive is when it makes a call to itself from within it's own code block.
The example we are going to be using is a recursive Function used calculate the factorial of a number.
The factorial of the number 6, denoted 6!, is 6 x 5 x 4 x 3 x 2 x 1 = 720.
Lets look at how this would look as a recursive Function.
\begin{lstlisting}[caption={Recursive Factorial}]
def Factorial( num ):
if num < 1:
return 1
else:
return num * Factorial( num - 1 )
print ``6! = `` + Factorial( 6 )
\end{lstlisting}
There are a few parts of a Recursive Function that we need to mention before diving deeping.
First off we have the actual Recursive call which is when the Function calls it's, in this Function it is
the code \pigVar{Factorial ( num - 1 )}.
Secondly, and probably the most important part, is the Base Case:\\
\pigVar{if num < 1:
return 1}\\
The base case tells the Function when to stop calling itself recursively, otherwise the Function will call itself
infinitely and cause an infinite loop and could crash the program.
\par
It might not be clear to some exactly how this Function is working, how is it getting the right value back out into
the program?
Well, lets do what we can to break down the Function call \pigVar{Factorial( 6 )} down to see exactly how the program
interprets this Function.

BIN
Programming In General.pdf View File


+ 12
- 8
Programming In General.tex View File

@ -203,36 +203,40 @@ To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/
\input{"./6 - Data Structures/Data Structures"}
\vfill
\pagebreak
\section{Big O Notation}
\input{"./6 - Data Structures/1.1 - Big O Notation"}
\vfill
\pagebreak
\section{Linked Lists}
\input{"./6 - Data Structures/1.1 - Linked Lists"}
\input{"./6 - Data Structures/1.2 - Linked Lists"}
\vfill
\pagebreak
\section{Doubly Linked Lists}
\input{"./6 - Data Structures/1.2 - Doubly Linked Lists"}
\input{"./6 - Data Structures/1.3 - Doubly Linked Lists"}
\vfill
\pagebreak
\section{Stacks}
\input{"./6 - Data Structures/1.3 - Stacks"}
\input{"./6 - Data Structures/1.4 - Stacks"}
\vfill
\pagebreak
\section{Queues}
\input{"./6 - Data Structures/1.4 - Queues"}
\input{"./6 - Data Structures/1.5 - Queues"}
\vfill
\pagebreak
\section{Hash Maps}
\input{"./6 - Data Structures/1.5 - Hash Maps"}
\input{"./6 - Data Structures/1.6 - Hash Maps"}
\vfill
\pagebreak
\section{Binary Trees}
\input{"./6 - Data Structures/1.6 - Binary Trees"}
\input{"./6 - Data Structures/1.7 - Binary Trees"}
\vfill
\pagebreak
\section{B-Trees}
\input{"./6 - Data Structures/1.7 - B-Trees"}
\input{"./6 - Data Structures/1.8 - B-Trees"}
\vfill
\pagebreak
\section{B+ Trees}
\input{"./6 - Data Structures/1.8 - B+ Trees"}
\input{"./6 - Data Structures/1.9 - B+ Trees"}
\vfill
\pagebreak
\chapter{Algorithms}


Loading…
Cancel
Save