Last active
December 20, 2015 00:59
-
-
Save clay-whitley/6046160 to your computer and use it in GitHub Desktop.
Explaining the differences between typical methods and recursive methods for my blog.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Typical method definition | |
| def multiply_number(integer) | |
| puts integer * 2 | |
| end | |
| # Typical method calls | |
| multiply_number(5) # this would print to the console the result of 5 * 2 | |
| multiply_number(23) # this would print to the console the result of 23 * 2 | |
| # Recursive method definition | |
| def count_down_from(integer) | |
| if integer < 1 | |
| puts "This method is all done!" | |
| return | |
| end | |
| puts integer | |
| count_down_from(integer - 1) # here is the recursive call, | |
| # the method calls itself INSIDE it's own definition. | |
| end | |
| # Recursive method call | |
| count_down_from(5) | |
| # The above method call would print to the console the numbers 5,4,3,2, and 1, | |
| # followed by the string "This method is done!" | |
| count_down_from(100) # This call would print to the console numbers from 100 to 1. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment