We may earn an affiliate commission when you visit our partners.
Huw Collingbourne

Recursion is a powerful programming technique. A function that calls itself recursively not only saves programming effort and avoids repetition but it can also be used to navigate complex structures such as Trees and Class Hierarchies.

This is an intermediate-to-advanced level course. It is aimed at programmers who can already program confidently in one or more programming languages. It is not appropriate for beginners.

Read more

Recursion is a powerful programming technique. A function that calls itself recursively not only saves programming effort and avoids repetition but it can also be used to navigate complex structures such as Trees and Class Hierarchies.

This is an intermediate-to-advanced level course. It is aimed at programmers who can already program confidently in one or more programming languages. It is not appropriate for beginners.

The courses includes numerous short sample programs to show how recursion works. There are samples written in C, Ruby and C#. However, you don’t need to program in those languages to follow this course. Recursion works the same way in all mainstream programming languages. This course explains the theory and the practice of recursion. You can use the techniques that are taught to write recursive functions in whichever language you prefer: C, C#, Java, JavaScript, Python, Basic, Pascal and others. The course is not about any specific language.

This is what you will learn…

  • What is recursion?

  • How variables are scoped in recursive functions

  • How recursive functions return values

  • The Stack and Stack Frames

  • Debugging recursive functions

  • Recursion v Iteration

  • Stack Corruption, and how to avoid it

  • Infinite Recursion, and how to avoid it

  • Recursing Fibonacci Numbers

  • Recursing a Class Hierarchy

  • Trees and recursion

  • Navigating subdirectories recursively

  • Code samples provided in C, Java, Ruby...

Enroll now

What's inside

Learning objectives

  • Call functions recursively
  • Understand how recursion works
  • Understand the stack and stack frames
  • Avoid stack corruption
  • Use recursion in any mainstream programming language
  • Know the pros and cons of recursion and iteration
  • Navigate tree structures
  • Traverse disk directories recursively
  • Code samples in c, java, ruby...

Syllabus

What is recursion – and how does it work?

Recursion is a powerful technique but it can be quite difficult to understand. This course explains how recursion works in all mainstream programming languages.

Read more

How should you use this course to understand recursion?

Download and unzip this archive of sample code

Java programmers may download this archive which contains most of the C sample programs translated into the Java language.

Download this file for information on more books and web sites and any other notes or FAQs on this course.

Some books treat recursion as a puzzle-solving technique. It’s not. It has numerous important, real-world applications.

Let’s begin by taking a look at a very short recursive function.

Here I’ll show how to debug recursive code using Visual Studio

How are local variables affected by recursion?

So how does recursion actually work? This may be the most important lesson in the course!

What is a stack frame and why does it matter?

Let’s use a debugger again to see how the call stack works

Here we look at more examples of recursive functions

Recursive functions work the same way in most languages. To prove that, I’ve translated the last C project into Ruby.

How does a recursively-called function return a value?

Why isn’t the value of a local variable discarded when it is returned by a function?

When you should use iteration or arithmetic rather than recursion?

How does recursion work?

Let’s calculate a series of Fibonacci numbers!

Let’s compare iteration and recursion when calculating Fibonacci numbers.

Time to look at a common bug.

When the order arguments has a weird side-effect

One of the most serious bugs you may encounter

Stack corruption can happen even without recursion

A closer look at the stack

Summary of stack frames

How to backtrack from and end-node to a root in a tree

How to recurse from a root to all the nodes or ‘branches’ in a tree

Navigating subdirectories on a disk in the C programming language

Navigating subdirectories on a disk in the Ruby programming language

Now try writing your own disk browser

Understanding recursion and avoiding errors

We are at the end of the course. How can you continue studying recursion?

Traffic lights

Read about what's good
what should give you pause
and possible dealbreakers
Explores recursion, which is a fundamental concept in computer science and software development, enabling programmers to solve problems elegantly and efficiently
Teaches debugging recursive functions, which is a crucial skill for identifying and resolving issues in complex code, leading to more robust and reliable software
Compares recursion and iteration, which allows learners to understand the trade-offs between these two approaches and choose the most appropriate one for a given task
Includes code samples in C, Java, and Ruby, which demonstrates the universality of recursion across different programming languages and paradigms
Requires familiarity with programming concepts, which may exclude absolute beginners who have not yet grasped the fundamentals of coding and software development
Discusses stack corruption, which is a critical topic for understanding memory management and preventing crashes in recursive programs, ensuring program stability

Save this course

Create your own learning path. Save this course to your list so you can find it easily later.
Save

Reviews summary

Deep dive into recursion mechanics

According to learners, this course offers a strong theoretical foundation and clear explanations of how recursion works under the hood, emphasizing the stack. The debugger demonstrations are particularly invaluable for clarity. While praised for its depth in mechanics, some note a lack of extensive practical application examples. The code samples in C/Ruby/C# can be challenging if unfamiliar with the language, and some examples feel somewhat dated, though concepts are timeless. One found the presentation dry. Overall, it's well-received for its clear, deep dive into recursion's inner workings.
Concentrates on mechanics over practical patterns
"If you're looking for practical ways to apply recursion to common problems..., this isn't really it. It's more about the execution model."
"Was hoping for more practical application examples."
"The examples are mostly theoretical."
"Wish there were more complex, real-world problem-solving examples using recursion."
Visual demos clarify execution and stack
"The use of debugger demos to show the stack was invaluable."
"The debugger walkthroughs were particularly insightful."
"The debugger section is highly valuable."
"Here I’ll show how to debug recursive code using Visual Studio"
Provides a strong theoretical understanding
"Really solid theoretical foundation."
"The course explains the theory well..."
"This course focuses on the 'why' and 'how' of recursion, which is exactly what I needed."
"Provides a solid understanding of recursion's mechanics."
Explains how recursion works internally
"It really helped solidify my understanding of how recursion works under the hood."
"Good explanations of the theory and mechanics of recursion (stack, variables)."
"The instructor's explanations on the stack and variable scoping are top-notch. It finally clicked for me..."
"I learned more about the stack here than in any other resource."
Some find the delivery method dry
"Found it difficult to engage with the material. The presentation style is a bit dry."
"While the information might be technically correct, it didn't capture my interest."
Some examples or tools feel slightly old
"Some parts felt a little dated, but the core concepts are timeless."
"My only minor gripe is that some of the tools/demos shown seem a bit old, but the underlying principles are sound."
Code examples may challenge if language is new
"I found the examples a bit hard to follow if you aren't familiar with C. While it says you don't need to know the languages, it helps a lot."
"...even as an intermediate programmer, I struggled a bit with the code examples."
"Could perhaps use more modern language examples like Python or JavaScript, but C/Ruby were fine for illustrating the principles."

Activities

Be better prepared before your course. Deepen your understanding during and after it. Supplement your coursework and achieve mastery of the topics covered in Recursion For Programmers with these activities:
Review Stack Data Structure
Review the concept of stacks, as understanding the stack and stack frames is crucial for grasping how recursion works and avoiding stack overflow errors.
Browse courses on Stack Data Structure
Show steps
  • Read about stack data structures.
  • Implement a stack in your preferred language.
  • Practice stack-related problems on LeetCode.
Read 'Cracking the Coding Interview'
Review recursion-related problems in 'Cracking the Coding Interview' to prepare for technical interviews.
Show steps
  • Study the recursion chapter.
  • Solve the practice problems.
  • Analyze the solutions and time complexities.
Implement Fibonacci Sequence Recursively
Practice implementing the Fibonacci sequence using recursion to solidify understanding of recursive function calls and return values.
Show steps
  • Write a recursive function for Fibonacci.
  • Test with different input values.
  • Analyze the time complexity.
Three other activities
Expand to see all activities and additional details
Show all six activities
Create a Recursion Visualization
Develop a visual representation of how a recursive function works, including stack frames and variable scoping, to enhance understanding and debugging skills.
Show steps
  • Choose a recursive algorithm to visualize.
  • Design the visual representation.
  • Implement the visualization using a tool.
  • Test and refine the visualization.
Recursive Directory Traversal Tool
Build a tool that recursively traverses a directory structure and prints the file names, reinforcing the concept of recursion in a practical application.
Show steps
  • Design the directory traversal logic.
  • Implement the recursive function.
  • Handle edge cases like symbolic links.
  • Test with various directory structures.
Read 'Structure and Interpretation of Computer Programs'
Study SICP to gain a deeper understanding of recursion and its applications in computer science.
Show steps
  • Read the chapters on recursion and higher-order functions.
  • Work through the examples and exercises.
  • Experiment with the concepts in your preferred language.

Career center

Learners who complete Recursion For Programmers will develop knowledge and skills that may be useful to these careers:
Algorithm Developer
Algorithm Developers create and optimize algorithms for various applications, ranging from search engines to financial modeling. This course is highly relevant, as it provides a deep understanding of recursion, a fundamental concept in algorithm design. You can learn to implement recursive algorithms for tasks such as sorting, searching, and graph traversal. Studying the course material helps build a solid foundation in recursive problem-solving, and you will learn to analyze the efficiency of algorithms and identify potential pitfalls like stack overflow and infinite loops. The course's emphasis on practical examples and debugging techniques is particularly useful for aspiring algorithm developers.
Software Engineer
A software engineer designs, develops, tests, and maintains software applications. This course helps software engineers deepen their understanding of recursion, which is crucial for solving complex problems and optimizing code. By mastering recursive techniques, you can write more efficient and elegant algorithms for tasks such as traversing data structures, implementing search algorithms, and solving mathematical problems. The course emphasizes the practical application of recursion with code samples in C, Ruby, and Java, further enhancing a software engineer's ability to implement these concepts in real-world projects. Understanding concepts, such as stack frames, taught in this course helps build a solid foundation.
Game Developer
A game developer designs and builds video games, often involving complex algorithms and data structures. This course helps game developers write recursive functions for tasks such as generating game levels, implementing artificial intelligence, and creating special effects. The ability to navigate tree structures and class hierarchies recursively is particularly useful for managing game entities and their relationships. The course covers stack management and debugging, helping game developers avoid common errors and optimize performance in resource-intensive game environments. Knowing the differences between recursion and iteration described in this course can help you more effectively choose how to implement features.
Web Developer
Web developers build and maintain websites and web applications, often dealing with complex data structures and user interfaces. This course helps web developers write recursive functions for tasks such as rendering nested components, managing complex state, and implementing search algorithms. The ability to navigate tree structures efficiently is particularly useful for managing website navigation menus and rendering hierarchical data. The course covers stack management and debugging, helping web developers avoid common errors and optimize performance in dynamic web environments. This will boost your ability to build performant software.
Compiler Designer
Compiler designers develop the software that translates high-level programming languages into machine code. This course helps compiler designers understand how recursion is implemented at the machine level, including the use of the call stack and stack frames. A compiler designer can learn to optimize recursive function calls and detect potential stack overflow errors. The course’s coverage of different programming languages (C, Ruby, Java) provides valuable insights into how recursion is handled in different compiler implementations. Understanding how to backtrack from an end-node to a root in a tree may be particularly useful when working with abstract syntax trees.
Robotics Engineer
Robotics engineers design, build, and program robots for various applications. This course may be useful for writing recursive algorithms for tasks such as pathfinding, motion planning, and sensor data processing. A robotics engineer often works with hierarchical data structures, such as robot kinematics and sensor fusion trees. The course helps build a foundational knowledge of tree navigation and recursive function design, which can improve the efficiency and reliability of robot control systems. The robotics engineer may find that the aspects of the course related to stack corruption are particularly critical.
Data Scientist
A data scientist analyzes large datasets to extract meaningful insights and develop predictive models. This course may be useful for writing recursive functions to process hierarchical data, such as trees or nested data structures, which are common in data analysis. Learning to navigate these structures efficiently is essential for tasks like building decision trees, exploring network data, and implementing graph algorithms. The course covers concepts like stack corruption and debugging recursive functions, which helps data scientists write robust and reliable code for complex analytical tasks.
Embedded Systems Engineer
An embedded systems engineer designs software for devices with limited resources, such as microcontrollers and IoT devices. This course may be useful for writing efficient recursive functions to manage data structures, such as trees or linked lists. Learning to avoid stack overflow and optimize memory usage is crucial in embedded systems development. The course teaches how to debug recursive functions, which helps embedded systems engineers identify and fix issues in their code. Because many embedded systems use C or a related language, the code samples provided may be particularly useful.
Database Administrator
Database administrators manage and maintain databases, ensuring data integrity and performance. This course may be useful for writing recursive queries to process hierarchical data, such as organizational charts or bill-of-materials structures. Learning to navigate tree structures and optimize recursive function calls is essential for tasks like querying nested data and performing complex data transformations. The course helps the database administrator avoid common errors such as infinite loops and stack overflow, which can impact database performance. The section on recursion versus iteration may be particularly helpful.
Bioinformatician
Bioinformaticians analyze biological data, such as DNA sequences and protein structures, to gain insights into biological processes and develop new therapies. This course may be useful for writing recursive algorithms to process hierarchical data, such as phylogenetic trees and protein folding pathways. This allows bioinformaticians to efficiently analyze complex biological data and develop new algorithms for drug discovery and personalized medicine. The course can help you to navigate tree structures and class hierarchies recursively.
Network Engineer
Network engineers design, implement, and maintain computer networks. This course may be useful for writing recursive algorithms to manage network configurations, such as routing tables and network topologies. The course helps network engineers to understand how to traverse network graphs and implement protocols that rely on recursive techniques. This helps build a foundational knowledge of network management and optimization, and you will learn to troubleshoot network issues and improve network performance. You may find that traversing disk directories recursively may have application in your work.
Financial Engineer
Financial engineers develop and implement mathematical models for pricing derivatives, managing risk, and optimizing investment strategies. This course may be useful for writing recursive algorithms to simulate financial markets, model complex financial instruments, and analyze market data. Learning to navigate tree structures and optimize recursive function calls is essential for tasks such as building Monte Carlo simulations and pricing exotic options. The course helps financial engineers avoid common errors such as infinite loops and stack overflow, which can impact the accuracy and reliability of financial models.
CAD Designer
CAD designers use computer-aided design software to create detailed technical drawings and models for engineering and manufacturing. This course may be useful for writing recursive algorithms to generate complex geometric shapes and manage hierarchical design structures. Learning to navigate tree structures and optimize recursive function calls helps CAD designers automate design tasks and improve design efficiency. By understanding how recursion works, CAD designers can create more sophisticated designs and reduce the time to market for new products. The course's section on backtracking from end-node to root node may have applications in the design of mechanical parts.
DevOps Engineer
A DevOps engineer automates and streamlines the software development and deployment process. This course helps DevOps engineers write recursive scripts to manage complex system configurations, automate infrastructure provisioning, and orchestrate deployments. By understanding how recursion works, you can write more efficient and reliable automation scripts, reducing the risk of errors and improving the speed of software delivery. The course's coverage of stack corruption and debugging helps DevOps engineers troubleshoot issues in recursive scripts and ensure the stability of their systems. This improves the reliability of deployments.
Data Analyst
Data analysts examine data to identify trends, develop charts, and create insights for business stakeholders. This course may be useful for writing functions in data processing languages like Python or R that need to recursively process data. For example, you may need to examine a decision tree and take action depending on the final result. The course mentions languages popular for data analytics, and the debugging discussion may be particularly useful. Overall, this course may improve the quality of your work.

Reading list

We've selected two books that we think will supplement your learning. Use these to develop background knowledge, enrich your coursework, and gain a deeper understanding of the topics covered in Recursion For Programmers.
Provides a deep dive into fundamental programming concepts, including recursion. It uses Scheme, a dialect of Lisp, to illustrate these concepts. While not directly using C, Java, or Ruby, the principles discussed are universally applicable. This book is more valuable as additional reading to deepen understanding of programming paradigms.
Comprehensive guide to preparing for technical interviews, and it includes a section on recursion. It provides numerous examples and practice problems. This book useful reference tool for understanding common recursion patterns and problem-solving techniques. It is commonly used by students and professionals alike.

Share

Help others find this course page by sharing it with your friends and followers:

Similar courses

Similar courses are unavailable at this time. Please try again later.
Our mission

OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.

Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.

Find this site helpful? Tell a friend about us.

Affiliate disclosure

We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.

Your purchases help us maintain our catalog and keep our servers humming without ads.

Thank you for supporting OpenCourser.

© 2016 - 2025 OpenCourser