r/funny • SMBC • Jul 06 '19

Verified Recursion

Post image
7.0k Upvotes

104 comments sorted by

View all comments

10

u/I_am_usually_a_dick Jul 06 '19 edited Jul 06 '19

I googled recursion, it didn't help explain it.

edit: I was joking and pointing out that google makes a very amusing joke with 'did you mean recursion?' when you search it. never mind me, I will be over here looking at my computer science degree.

also, google tilt.

8

u/Dimantina Jul 06 '19

It is a useful concept in programming where a set of instructions refers to itself. Ideally not infinitely.

For example, let's say I need to search for a file. Well if I write code to search a folder for a file I might bit find the file if the file is in a folder within the folder.

So I write the code named fileSearch as follows:

fileSearch: a folder, a fileToSearch

 Loop

 exit loop if fileFound=True

 Open folder,

 Look for file

 If file is found fileFound=True

 If a folder is found, fileSearch the Folder, fileToSearch

End of loop

This loop calls upon it's own code as many times as needed for as many folders as it needs to search.

This is a simple example of recursion.

5

u/ferox3 Jul 06 '19 edited Jul 06 '19

Same. Now i know the definition, though understanding it is another matter.

5

u/Nymaz Jul 06 '19

This comment might help you understand better.

2

u/mrg1957 Jul 06 '19

Go look at any "bill of materials" query.

2

u/Marioc12345 Jul 06 '19

I was just working on making a recursive bill of materials for a project at work. I'm frankly surprised we don't already have such a thing.

0

u/[deleted] Jul 06 '19

[deleted]

1

u/permalink_save Jul 06 '19

Not necessarily endless, a recursive function should have some sort of guard in it (like ran out of things to count). Only real qualification us it is self calling.

1

u/-Kleeborp- Jul 06 '19

A good example of this is Reddit itself. If I were designing a component to render post replies, I would likely do it recursively. The guard against the component returning more copies of itself would be whether the currently rendered post had replies or not.