# Multiple Return Values: Unlocking the Power of Recursive Functions | Recursion Series

## Метаданные

- **Канал:** WilliamFiset
- **YouTube:** https://www.youtube.com/watch?v=_RaIpPQ5aP8
- **Дата:** 31.05.2023
- **Длительность:** 12:49
- **Просмотры:** 7,875

## Описание

In this video, we dive into the fascinating world of recursive functions and learn how to return multiple values from them. Recursive functions are powerful tools that call themselves repeatedly to solve complex problems by breaking them down into smaller, more manageable subproblems. Returning multiple values from a recursive function can be particularly useful when you need to gather and analyze various pieces of information during the recursive process.

Source code repository:
https://github.com/williamfiset/algorithms

Video slides:
https://github.com/williamfiset/algorithms/tree/master/slides

Personal website:
williamfiset.com

## Содержание

### [0:00](https://www.youtube.com/watch?v=_RaIpPQ5aP8) Introduction

foreign hey guys welcome back today we're going to probe further into the world of recursion all the functions we've been looking at so far have always dealt with a single return value today we are going to be taking a look at how we can return multiple values from a recursive function this is something we want to know how to do as it comes in handy time and time again the whole idea behind

### [0:33](https://www.youtube.com/watch?v=_RaIpPQ5aP8&t=33s) Why return multiple values

returning more than one return value from a function is that we want to be able to capture or compute more than one thing inside a function and return it as an output for example the min max function below sorts the two numbers A and B by returning both the minimum and the maximum there are multiple methods for how you can actually return multiple values in a programming language the sudo code below returns a comma separated list of return values but not all languages offer this sometimes you need to return a list of values or create a wrapper object however returning multiple values is largely about being able to juggle around multiple variables within a function so let's have a look at an example

### [1:26](https://www.youtube.com/watch?v=_RaIpPQ5aP8&t=86s) Example

example in this simple problem we're going to be finding the maximum element in a list but instead of just finding the maximum element we are also going to be returning the index of that maximum element for example the maximum element in this list should be three with an index of two all right now let's walk through how the maximum element and its index are actually found the main idea is to run through all the elements and continuously update the best Max index pair that we find we're going to start on the left side of the list and work our way to the right we'll Begin by making a call to the find Max function at index position 0 and move towards the end of the list the find Max function should take two arguments the index of the current position as well as a reference to the list we are going to keep on recursively calling the find Max function until we are out of bounds once we're out of bounds we're going to return a value index pair containing the values null common null to indicate that there is no best maximum or index yet on the Callback we find that the value of 2 is better than the maximum value of null so update the best value index pair to 2 comma 4. since 2 is greater than negative 4 keep the best value index pair as two comma four next three is a greater max value then 2 so update the best value index pair to 3 comma two after that 3 is still a better max value than one so keep the best value index pair as three comma two and similarly for the next position in the end we find that the maximum element is 3 with an index position of two the way we did that was by propagating the value index pair or cross recursive calls and comparing the current value to the best known value and updating accordingly so the process of returning multiple values is really the same as returning a single value except that there's more variables and juggling of variables going on

### [3:58](https://www.youtube.com/watch?v=_RaIpPQ5aP8&t=238s) Pseudocode

let's quickly take a look at some pseudocode on how to find the maximum element and its index so the first thing I do is declare a function called find maximum element which delegates the work of finding the maximum value and its index to the find Max function all this function does is called the find Max function with a starting index of 0 as well as a reference to the list just like in the previous example find Max takes I the current index position as the input and LST a reference to the list our base case is when we've reached the end of the list in this case return null comma null since there are multiple return values we need to return null for each one this is a good time to mention that if your programming language doesn't support returning a comma separated list of values there are workarounds one popular method is to return the list containing the index and the maximum value another strategy is to create a wrapper data class encapsulating the max value and its index personally I tend to prefer creating a new data class if there's going to be a lot of information being returned since that tends to keep the data more organized after that we get the best known maximum value and its index by calling the find Max function if the best known maximum value is null or the current value is better than the best known maximum update the best Max and best index when the best known Max is null this means that there is no best maximum value yet which is why we want to set the best maximum to the current value after that simply return best Max and best index as the return values all right so like I've been doing in the other videos I have a mini coding challenge for you guys so we just looked at how to find the maximum element and its Associated index in a one-dimensional list now the challenge I have for you guys is to implement a recursive function called find matrix Max which finds the row column index position of the largest element in the Matrix you can assume that the input Matrix is a 2d array of rectangular proportions I'm going to be walking through the solution and the slides to come so feel free to pause the video here and give it a try

### [6:42](https://www.youtube.com/watch?v=_RaIpPQ5aP8&t=402s) Solution

awesome I hope you all had a chance to give the challenge a try let's look at one way to solve this problem so here on the screen I have a 2d Matrix with six cells the way we're going to approach solving this problem is by keeping track of the maximum value by traversing The Matrix left to right row by row starting at index position zero on the right I will be keeping track of the recursive function calls to The Matrix Max function which takes three arguments the row index position the column position and M a reference to The Matrix the first function call to The Matrix Max function is the row column position 0 to kick start the recursion then we begin traversing to the right incrementing the column position as we go eventually the column position goes out of bounds this indicates that we should go down to the next row so we and reset the column position to zero and then we keep incrementing the column index one position at a time then we're outside the bounds of the Matrix again which means we should go down to the next row now the recursion continues down to the next row at which point we realize that the next row is also outside the bounce of the Matrix this is our base case that indicates that we should stop the recursion when the row position is outside the balance of the Matrix return null common null to indicate that there is no best maximum element yet returning up the stack to the last position where we were since this is also an empty cell propagate the best known answer which is currently still null common null since 7 is a better max value than null update the best row column position to 1 comma 2. next 3 is less than 7 so we propagate the row column position for cell 7. now 8 is a better maximum value than 7 so update the best row column position to cell one comma zero if the cell is empty propagate the best known answer since 6 is less than eight also propagate the best known answer now 9 is greater than eight so we update the best row column position to cell 9 which is zero comma one is less than nine so propagate the best known row column position in the end we get the maximum element is at the row column position zero comma one corresponding to cell nine okay let's have a look at some pseudo code on how one would implement the find matrix Max function you'll notice that it resembles the one-dimensional array example in many ways except that we are now dealing with changing rows first off we declare the find matrix Max function this calls the internal function Matrix Max which kick-starts our recursion by setting the row column position to 0 0. inside the Matrix Max function we have two cases we handle the first one checks if the row position is equal to the number of rows in The Matrix and returns null if we were to visualize what this means it essentially tests for the case when we are outside the bounds of the rectangle From Below returning null common null represents that there is no best known row column index yet the second if statement checks if we're outside the bounds of the Matrix but on the right when this happens we want to move down to the next row if we were to visualize this anytime we were to land in a Cell highlighted in yellow we would move to the cell on the road down to the left resetting the column position to zero for example if we were at position 0 comma three this would move us to the cell one comma zero and if we were at cell one comma 3 we would go to cell 2 comma zero this is essentially what allows us to wrap around and go down to the next row after those base cases are handled we call the Matrix Max function to get the max row and Max column indices by default we want to be moving cells from left to right which is why when we recursively called The Matrix Max function the row position stays the same but we increment the column by one to move to the right on the Callback after the Matrix Max function returns this is where we want to actually find the index of the maximum element in the Matrix if the returned Max row Max column values are null this means there is no best cell yet so we want to set the best Row in the best column position to the current row column position after that here's where the more important update happens if the value in the current cell is greater than the best known value update Max row and Max column to the current cell and the final step is just to return the max row and the max column values all right guys and that's all I've got for you today thank you so much for sticking around till the end please like this video And subscribe if you learned something and I will catch you in the next one cheers foreign

---
*Источник: https://ekstraktznaniy.ru/video/49239*