Recursion in software development
Skills:
Algorithm Basics80%
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
Playlist
Uploads from freeCodeCamp.org · freeCodeCamp.org · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
Dates - Beau teaches JavaScript
freeCodeCamp.org
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
The Definition of Ready - Agile Software Development
freeCodeCamp.org
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
Working Agreement - Agile Software Development
freeCodeCamp.org
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
Definition of Done - Agile Software Development
freeCodeCamp.org
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
The INVEST approach to product backlog items
freeCodeCamp.org
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
Trapping Rain Water: Understanding Data Structure Choices from a Beginner’s Perspective
Medium · Programming
The Grid Problem That Looks Easy Until You Need the Lexicographically Smallest Path
Medium · Programming
The Algorithm That’s Practically O(1) — But Provably Isn’t
Medium · Programming
Knight Attack Made BFS Feel Like a Recipe, Not a Template
Medium · Python
🎓
Tutor Explanation
DeepCamp AI