From the course: Programming Foundations: Algorithms

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Find max value recursively

Find max value recursively

- [Instructor] For this example, we're going to use recursion one more time and we're going to write a recursive function to find the maximum value in a list of values. And yes, I do realize that this is a bit of a contrived example, but it's a useful way of seeing recursion work with an example that's not too complex. So here's how the function's going to work. It will accept a list of numeric values. When the function is called, it will check to see if the list has only one item in it. If so, it will just return that value since that's obviously the largest value in the list because it's the only value in the list. Otherwise, it will get the first value in the list and then assign the second comparison value to a recursive call to itself with the remaining values. After all the values are retrieved, each value will be compared with its neighbor to determine which is largest. So if we look at how this is going to work, it's…

Contents