Recursion in software development

freeCodeCamp.org · Beginner ·⚡ Algorithms & Data Structures ·9y ago

Key Takeaways

This video teaches recursion in software development using flowcharts and Python code, covering recursive functions, base cases, and recursive cases.

Full Transcript

suppose you're digging through your grandma's attic and come across a mysterious locked suitcase grandma tells you that the key for the suitcase is probably in this other box this box contains more boxes with more boxes inside those boxes the key is in a box somewhere what's your algorithm to search for the key in this unit you'll learn about recursion recursion is used in many algorithms and is a building block for understanding later units in this course recursion can be used to solve the previous problem of searching for a key in nested boxes you will also learn to break down a problem into a base case and a recursive case i will be showing you a lot of code examples i highly recommend trying to run the code for yourself also at least once you should try to step through a recursive function with pen and paper to get a better sense of how recursive functions work this unit also includes a lot of pseudo code pseudocode is a high level description of the problem you're trying to solve in code it's written like code but it's meant to be closer to human speech let's get back to searching for a key in a box that contains boxes which contain other boxes first i'll show you a method to solve this problem that uses while loops then i'll show you a method that uses recursion here's the first method one make a pile of boxes to look through two while the pile isn't empty grab a box and look through it three if you find a key you're done four if you find a box add it to the pile of boxes to look through later five repeat here's the second method one look through the box two if you find a key you're done three if you find a box go to step one which approach seems easier to you the first way uses a while loop here's the first method using pseudocode first we make a pile of boxes to look through from the boxes in the main box while the pile is not empty choose a box to look through for each item in the box check if the item is another box if it is another box append or add it to the pile boxes we still have to look through if the item we found is a key then yell to everyone excitedly found the key keep checking the boxes until the pile is empty the second way uses recursion recursion is where a function calls itself here's the second way in pseudocode first we check the box passed into the function and go through each item in the box if the item is a box then we pass the box into the function again if the item is a key then yell to everyone excitedly found the key both approaches accomplish the same thing but the second approach seems cleaner a recursion is used when it makes the solution clearer there's actually no performance benefit to using recursion in fact loops are sometimes better for performance however people still use recursion since it often makes code easier to understand and look cleaner many important algorithms use recursion so it's important to understand because a recursive function calls itself it's easy to write a function incorrectly that ends up in an infinite loop for example suppose you want to write a function that prints a countdown you could write it recursively like this however if you run this code there's a problem the function will run forever if your code is in an infinite loop press ctrl c to kill the script when you write a recursive function you have to tell it when to stop recursing that's why every recursive function has two parts the base case and the recursive case the recursive case is when the function calls itself the base case is when the function doesn't call itself again so it doesn't go into an infinite loop here is the function from before with the addition of a base case the if i is less than or equal to 1 is the base case that is when the function will not call itself anymore in the else section is the recursive case that is what will keep happening if the base case has not yet been reached so unlike the last example this function will actually end

Original Description

To understand recursion you must first understand recursion. This video will help you do just that! Learn how recursion works with flowcharts and code. The code is in Python but the concepts apply to any programming language. Recursive functions have a base case and recursive case. Watch out for infinite loops! This video is part of a course called 'Algorithms in Motion' from Manning Publications. You can get it here: https://www.manning.com/livevideo/algorithms-in-motion?a_aid=algmotion&a_bid=9022d293  -- Learn to code for free and get a developer job: https://www.freecodecamp.com Read hundreds of articles on technology: https://medium.freecodecamp.com And subscribe for new programming videos every day: https://youtube.com/subscription_center?add_user=freecodecamp ❤️ Support for this channel comes from our friends at Scrimba – the coding platform that's reinvented interactive learning: https://scrimba.com/freecodecamp
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from freeCodeCamp.org · freeCodeCamp.org · 0 of 60

← Previous Next →
1 React: Production Server Setup Part 2 - Live Coding with Jesse
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
2 cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
3 Browser history tutorial - Beau teaches JavaScript
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
4 Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
5 React: Parameterized Routing with Next.js - Live Coding with Jesse
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
6 React: Dealing with jQuery Issues - Live Coding with Jesse
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
7 setInterval and setTimeout: timing events - Beau teaches JavaScript
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
8 Browser and Device Testing - Live Coding with Jesse
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
9 Last Minute Updates - Live Coding with Jesse
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
10 Post Launch Updates - Live Coding with Jesse
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
11 React: Setting Up Google Analytics - Live Coding with Jesse
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
12 React: Masonry Layout - Live Coding with Jesse
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
13 Load Balancing Digital Ocean Droplets - Live Coding with Jesse
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
14 try, catch, finally, throw - error handling in JavaScript
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
15 Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
16 Graphs: breadth-first search - Beau teaches JavaScript
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
17 React: Masonry Layout Part 2 - Live Coding with Jesse
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
18 React: WordPress API Live Search - Live Coding with Jesse
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
19 Creating WordPress Custom Post Types - Live Coding With Jesse
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
20 Dates - Beau teaches JavaScript
Dates - Beau teaches JavaScript
freeCodeCamp.org
21 Miscellaneous Front End Updates - Live Coding with Jesse
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
22 Merging a Pull Request from GitHub - Live Coding with Jesse
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
23 React + Prettier + Standard JS - Live Coding with Jesse
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
24 React: Sortable Responsive Table - Live Coding with Jesse
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
25 Geolocation Sorting by Distance - Live Coding with Jesse
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
26 Tradeoff Matrix - Agile Software Development
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
27 The Definition of Ready - Agile Software Development
The Definition of Ready - Agile Software Development
freeCodeCamp.org
28 Getting first React job without experience - Ask Preethi
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
29 React: Google Analytics Click Tracking - Live Coding with Jesse
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
30 Submitting a PR to an Open Source Project - Live Coding with Jesse
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
31 Should I go back to school to get CS degree? - Ask Preethi
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
32 Hero Section CSS Changes - Live Coding with Jesse
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
33 Working Agreement - Agile Software Development
Working Agreement - Agile Software Development
freeCodeCamp.org
34 A day at Pennybox with Co-Founder Reji Eapen
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
35 React: Sorting and Filtering Data - Live Coding with Jesse
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
36 React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
37 React: Building a New UI - Live Coding with Jesse
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
38 Definition of Done - Agile Software Development
Definition of Done - Agile Software Development
freeCodeCamp.org
39 Getting started with jQuery (tutorial) - Beau teaches JavaScript
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
40 Making a React Blog with WordPress Content - Live Coding with Jesse
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
41 React, NextJS, CSS - Live Coding with Jesse
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
42 jQuery events - Beau teaches JavaScript
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
43 React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
44 React: Working with API Data - Live Coding with Jesse
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
45 React: Refactoring Components - Live Streaming with Jesse
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
46 jQuery effects - Beau teaches JavaScript
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
47 More React Refactoring - Live Coding with Jesse
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
48 animate in jQuery - Beau teaches JavaScript
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
49 "Finishing" My React Site - Live Coding with Jesse
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
50 Starting a New React Project (P2D1) - Live Coding with Jesse
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
51 React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
52 The Agile Manifesto - Agile Software Development
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
53 jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
54 React Project 2 Day 3 - Live Coding with Jesse
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
55 The INVEST approach to product backlog items
The INVEST approach to product backlog items
freeCodeCamp.org
56 React Project 2 Day 4 - Live Coding with Jesse
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
57 Chickens and Pigs - Agile Software Development
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
58 React Project 2 Day 5 - Live Coding with Jesse
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
59 jQuery: add and remove DOM elements - Beau teaches JavaScript
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
60 React Project 2 Day 6 - Live Coding with Jesse
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org

This video teaches recursion in software development, covering the basics of recursive functions, base cases, and recursive cases, with examples in Python.

Key Takeaways
  1. Understand the problem of searching for a key in nested boxes
  2. Learn the first method using while loops
  3. Learn the second method using recursion
  4. Compare the two approaches
  5. Understand the importance of base and recursive cases
  6. Practice writing recursive functions with pen and paper
💡 Recursion is used when it makes the solution clearer, but it may not always be the most performant approach

Related Reads

📰
Trapping Rain Water: Understanding Data Structure Choices from a Beginner’s Perspective
Learn to solve the Trapping Rain Water problem by understanding optimal data structure choices and array traversal techniques
Medium · Programming
📰
The Grid Problem That Looks Easy Until You Need the Lexicographically Smallest Path
Learn to find the lexicographically smallest path in a grid, a problem that seems easy but requires careful consideration of path construction and comparison
Medium · Programming
📰
The Algorithm That’s Practically O(1) — But Provably Isn’t
Learn about an algorithm that behaves like O(1) but isn't, and how to analyze its complexity
Medium · Programming
📰
Knight Attack Made BFS Feel Like a Recipe, Not a Template
Learn how to apply BFS to solve the Knight Attack problem with a Python solution
Medium · Python
Up next
Quant Interview Question #quant
quantprof
Watch →