 Hi everyone, today I'm going to go over the extra practice questions from the Functions 1 workshop, and the solutions for those are already available on iLone. They've got our regular solutions using loops and whatever we need to do, but what I'm going to do today is walk you through some recursive solutions for these questions. So a recursive function is a function that calls itself, so off of the return value isn't 1, 2, or 3, it'll be another function call. So I'll walk you through some of these to explain how they work, but of course if you did it the regular solution way, that's totally fine, just thought I'd introduce another way of doing things. Okay, so I've got my structure for each question, whenever we are creating a function it's going to have this structure. So what I find is helpful is just have that structure ready, and then when I have a look at a question I say what the return type is, what inputs I need, whether it's a producer or a changer, and once I know what my function needs to do I'll have a structure ready, and then I can complete the method. So question seven, we need to define a function that when passed an integer, say we name it num in the function header, okay, so we already know we're passing an integer, so that's our parameter, cool, and it returns the sum of the first num positive integers, we want to call that inside set up to test that out, so if we make it equal to five then it's one plus two plus three plus four plus five, cool, so what should we call this? I'm not sure, that's a good word, sum of positives, actually let's see what do the answers call it, sum, that's a bit better, let's just call it sum, and we'll be returning an integer because we're getting the sum of the first one to num numbers, so that's going to return an int, so if we have a look at the solution here what they've done is they've just created a variable called total which they've made zero and then they've looped through the numbers from one to n, in our case num, or perhaps we'll do n as well just to keep it consistent, and then for each iteration of the loop it adds that value back to the total, so that works fine, but we want to do this recursively, and when we are creating a recursive function what we need to consider are our base cases and our recursive cases, so our base case is the case where we should stop calling another function and just return a value, so our base case in this case would be if we get, if we're passed in, if we call our function with the number one then we should just return the number one because the sum from one to one is just one, so that would be considered our base case, we also need to consider what if someone passes in a negative five we don't want that, so maybe we want to say if the number we get is negative, so if it's less than zero we should return zero, and we've said as well if n is equal to one then that should just be one, and in every other case say if it's sum of two then we need to do two plus one, so if we get my whiteboard up, so we know that sum one is equal to one, we know that sum two is equal to one plus two which will be three, so what we can say as well is that sum two is equal to sum one plus sum two, so what we need to do in this case is, oh sorry that's incorrect, two plus sum one, if we have sum three we can rewrite that as three plus sum two, and we know that sum two is equal to sum one plus two, so we can replace that there, so we can say sum three is equal to three plus, and then we'll replace sum two with sum one plus two because this is equal to this, and then we know what sum one is because we've got sum one up here it's just one, so again we can rewrite this as three plus one plus two, so we can use those function calls because we know it will give us that result to work out our answer, so what we want to do here is return the current number plus sum n minus one, so that might be a little bit confusing here, so if I do six that should be six plus five plus four plus three plus two plus one, we're getting 21 which should be right, oops let's just double check, very good, so let's trace this because that might not be immediately clear and that's fine, so let's have a look at tracing this here, okay so what we are calling a function call is sum six, we need to work out what that returns, so sum six what will that return, we go into our function n is equal to six, we've got if n is less than zero it's not true, if n is equal to one not true, so we're here and we're returning n plus sum n minus one, so n is currently six and then we've got plus sum n minus one, so that would be sum five, so at this point here sum six has returned something but what we need is an int, we need an actual int whole number to return back and we can only work out what that number is if we work out what sum five is, so that means that we have a recursive function call of sum five, so to work out what sum six returns now we need to call this sum five function and work out what it returns, so now we're going back into this function, we've got n is equal to five and we go again, if n is less than zero let's false, if n is equal to one false again, so sum five is returning n plus sum n minus one, so it's returning five plus sum n minus one which is four, okay so again sum five is returning five plus something, we don't know what sum is, so we have to go into this function call and work out what it returns, so again we'll trace again, we've got if n is less than zero if n is equal to one return n plus sum n minus one, so we've got four plus sum three, again we're recursing again, so continue down here and I'm just going to speed up this process because I think you guys hopefully you're getting the idea here, it's very repetitive, okay so we've made a few more recursive calls and now we're up to here where we've got sum one, so let's call this function sum one, so n is one and we've got if n is less than zero false, if n is equal to one return one, so we've finally returned an actual int number, so that means that we can stop recursing and we can work out what sum six actually returns, so to do that we know that what we've worked out down here is that sum one, if you have the function called sum one that will return one, so we can say this one is being returned back to this function call so this is being put back up here so I can replace this with the number one and now I know that sum two returns two plus one which we know is three, so sum two will return three so that value goes back to our function call and sum two is equal to three so we can replace this with three and we can continue on with that until we get back to our original function call which is sum six, so we know sum three is returning three plus three which is six, so sum three will be six, so now we've got sum four returns four plus six which is ten, we've got sum five returns five plus ten which is fifteen and finally by tracing that all the way back up we finally have a return value for sum six and we know that it is 21 which is what we got for our question which is great so a loop solution is a fine solution so if you did a loop solution that's good but there is a recursive way of doing questions like these and sometimes especially with a lot of practice the recursive solution is often an easier solution so let's try the other questions by using a recursive method, so question eight we've got a function that when passed two integers so we've got two integers being passed and they say to name them x and n and x and n which returns x to the power of n so we're doing powers here and the return type will be an integer returns x to the power of n great so if we did two to the power of five we get 32 zero to the power of one it's one easy okay so if we want to do this recursively we need to think about base case again so we know that if we had five to the power of two that's equal to five times five if we've got three to the power of zero then that's one we've got five to the power of three that's five times five times five so I would say that our base case is that if we give it a power of zero then we should just return one if we give it any other power then we need to multiply our base one more time so I don't think this question is asking us to deal with negatives you may assume that n is more than or equal to zero so we can forget about our powers being negatives that would make our question a little more complex and we can just focus on positive powers so if n is equal to zero then we know that we've got something to the power of one so I can say if n is equal to zero I want to return one and in any other case what we need to do is we need to multiply our base which we've said is x and we want to multiply that by a recursive function called that same base number so say if it's five we want five times five a few more times but we want it to be n minus one times so let's just double check this works before we move on great okay so that's working so if we have a look here in this case I'm saying that let's just say power two five we've got x and n so this is our function call we need to see what that returns so what does this return if n is equal to zero well it's not it's five return one otherwise return x times and then we've got our recursive function call so return x which is two multiplied by a recursive function call where x remains the same and n is n minus one so now I've got five minus one which is four so we need to work out what this returns so if we call this function here if n is zero no return x times and then another recursive function call so x is still two we're timesing by the power x remains the same and then our n is n minus one so now that's equal to three and then what does this return we'll find that this returns this and you can keep tracing I'll just speed it up a bit speed us up to this point here so now we've gotten we've recursed a number of times and now we're at the point where n is equal to zero and we know our condition says if n is equal to zero return one so we've hit our base case we're stopping that recursion there and we want to bring our values back up the chain so we can get back to our initial function call so what you'll find when you're tracing all of these back the answer will be two times the return value of this which is two times the return value of this which is two times this two times this two times this one so you'll find if you'll bring all of that back up we have a look at all of those twos we've got two times two times two times two times two times two times one which is two to the power of five it's two times itself five times we've got that times one at the end there but that doesn't matter because um anything times one is one okay so when designing your recursive functions you want to consider your base case when should I stop going through um this pattern of numbers if we're dealing with numbers um and then what's my recursive case when should I what should I do next what's next in the pattern um great okay question nine question nine define a function that when past two integers we'll name them a and b okay so we've got two integers a and b return the greatest common divisor so we'll call it gcd and that will be an int the greatest common divisor of two integers you may assume that both a and b are more than zero um great okay so they've got an example there so the greatest common divisor of 40 and 24 is 8 so it's the highest number that goes into both of these um so worst case scenario with these questions is that the greatest common divisor is 1 because then that means that there's no other number that goes into any of them like in the case of 12 and 17 so that should be our base case is that we return 1 if there's nothing in common between those numbers but first we want to check if there's a higher number and for people who are currently doing discrete maths 1 um you've probably had a look at these questions or maybe you're looking at them in future weeks um but this is how you would solve this one recursively they've got the answer here as well if you want to compare it um but i'll show you the recursive way um okay so it'll be if a is equal to zero or b is equal to zero return 1 if a so that's our base case if the numbers are equal then that means that a and b they are the same so that would be the greatest common divisor because they're the same number the greatest common divisor of 8 and 8 is 8 because it goes into both so it doesn't matter which one return it can be either so that's another um like final case as well um and so if they aren't equal then what we want to do is find which number's the highest take that higher number and minus away the smaller number with another recursive function call so if a is greater than b i want to return gcd a minus b b and then else we don't need an else because um we're at the end anyway all right return twice okay let's double check that this works by calling it up here so we already have an example here the greatest common divisor of 40 and 24 is 8 so let's see if that works and let's check another one get to do some testing okay awesome so this one works so i'll show you how this one works so say let's do it with the example of 28 and 40 i think it was no 24 and 24 and 40 is that the one yeah 24 and 40 got gcd 24 and 40 cool okay a and b are either of them equal to zero no um are a and b equal no is a greater than b no so this is what this returns returns a is the same so a is still 24 but b is b minus a so 40 minus 24 we get 16 okay what does this return are either of them equal to zero no are they equal no is a greater than b yes so i want to return gcd a is now a minus b so 24 minus 16 and we get 8 and then b remains the same okay so what does that return we call the function again is a equal to zero or b no other numbers equal no is a greater than b no so we're returning this gcd where a is the same 8 and b is equal to b minus a which is 16 minus 8 which is 8 and now when we call this function we've got if a is equal to b return a so we finally hit a base case there um and because there's no other like adding on each of these um function calls this value 8 just gets taken right back up to the top because it goes here which goes here which goes here there's no other manipulation um going on in this case okay so that is the recursive one for gcd let's do question 10 define oh i should do this okay define a function that went past an integer um okay so pass an integer returns the number of digits in that number so um digits and that return type will be an int because we're returning the number of digits in a number um cool so this is a good one um and we already have a base case we have the number of digits in zero is zero cool um the last digit oh so that's a little hit there so we have our base case we're saying if we're given zero then we return zero because there's zero digits in zero um and what doesn't say not to assume that it's positive so i guess the first thing to do is to take we need to check if it's a um negative number and if it's a negative number we want to turn it into a positive number because we don't care if it's positive or negative um we're more focused on how many digits there are so that doesn't affect it so if it's less than zero we should make it equal to or multiply it by negative one to make it a positive number um and then what we need to do is we'll use this hint here which is n divided by 10 and i'll show you what that is so with integer arithmetic we know that if we have two integers involved in an operation uh that the result will also be an integer so if you do five divided by two um usually you would say that that's 2.5 but with integer arithmetic that would be two because we always round down um and because of that we had a look at this question i think it was question three in this week's workshop um if you divide a number by 10 it is the same as just chopping off that last digit and removing it because we know that when we divide by 10 we move that invisible decimal place here we move it one spot to the left we get 4.7 um but we'll round down so it just removes that remainder um what we can also do is we can use modulus to grab that last digit so that will give us seven because what we're saying is when i divide by 10 what's left over and what's left over is this digit when we make that jump so using these two operations divide by 10 and mod 10 we can grab that last digit um and we can also remove the last digit um so in this case we don't care about what that last digit is we don't need mod um but it's a good thing to keep in mind all we need for this one is divide by 10 so um what we're doing is we're working at the number of digits in a number um so if we don't fit this case if n is an equal to zero then we assume that it's any number from one to the biggest possible integer you can have um so it definitely has at least one digit so what we need to do is we need to plus one one plus and then do a recursive function call with that last digit removed okay n divided by 10 so the next time we call this function if our number is 256 when we call this function we've said okay i found one digit at the end it's six so one plus the number of digits with 256 divided by 10 it's 25 and that will loop around again what did i call it i called it num digits one two three four so that should give us four in our console cool so let's do a quick trace of that one i'm just going to shorten it to num just to save time so we've got num one two three four where we said what did we call it we called it n let's move this back over again so we've got this here what does this return is n equal to zero no is it less than zero no return one plus num with n divided by 10 so one two three four divided by 10 we get one two three and now we need to work out what that returns so now our n is one two three in this function call so we're saying if n is equal to zero no um is it less than zero no so we're returning one plus num 12 what does num 12 return that will return one plus num one what does that return one plus num one divided by 10 you get zero and we know in our first condition that if we've got zero then we're returning zero so if we do that process of taking this all the way back to our first function call you'll find that what we get is one plus one plus one plus one plus zero so we've got four ones there so we'll get four that's our answer cool and then 11 is similar to question 10 except instead of getting the number of digits we're getting the sum of the digits so i'm going to be a bit lazy and copy that over change it to some digits and instead of returning one plus that i want to return that last digit which we said before you could get with n mod 10 so if i want some digits of um let's do one two seven because that will give us 10 oh i've made a mistake here ah one two seven what mistake am i making if n is equal to zero return zero n mod 10 so digits seven plus two plus one that's 10 so what have i done wrong perhaps this is a good time to use our debugger in practice with that okay so we've got one two seven when i step is it lesson zero no we're returning n mod 10 that's not looking good if n is less than zero so that should jump to 61 and that's returning n mod 10 which is seven plus this recursive function call which is n divided by 10 so now it will be 12 so if we step oh it's stepping right back up here how odd what have i done oh so silly this is why you don't get lazy and copy and paste i'm not doing a recursive function or i am doing recursive function call but i'm calling this one i deserve that i deserve that for being lazy 10 there we go oh goodness it's always the silliest things isn't it okay that's okay all right cool um so hopefully that makes sense there we're doing the exact same thing but we're returning the last digit plus the next recursive function call okay great question 12 uh to find a function that when past the floating point value returns the ceiling of that number um cool so we don't need to do this one recursively we can just do it regularly we know that we're being passed in a float because we want to get the ceiling of a float but what we want to return is an int so instead of rounding down we want to round up call it ceiling so what we want to do well the easiest way to do this would be to just round down and then plus the number one um so you can round down by casting that number as an int so you can say n is a float but please change it to an int value um so that will do that and then we want to plus one so we've round down but then we've added one so that it rounds back up so if i get the ceiling of 12.3 we should get 13 there um but we're not considering negative numbers oh no that's okay it works with negative numbers cool okay um getting there question 13 define a function that when past the floating point value returns the round off value of that number ah so we actually did this in the practical this week and again this is not something that we need to do recursively um so we're passing in a float and we're returning an int so um if that last digit is divisive if that last sorry if that last digit is less than five then we round down if that digit is greater than five then we round up so first we want to get that last digit which is n mod 10 and then we want to do a conditional we say if that last digit is less than five return all we want to round down in that case we can just convert the number to an int else so if that last digit is five or above um then we still want to do that we want to cast it down are we assuming that we just have one decimal place define a function of the past floating point value returns the round off as the integer closest to the floating point value i just assume that it would be one decimal place but maybe it's not good to um okay yeah so we shouldn't be assuming that that's a better way of doing it um cool so instead of using modulus um you just get the non decimal part which is int n we don't want to change n um and then so say if i had let's get this back up if i've got n is equal to 15.23 then non decimal will be equal to 15 and if we want the decimal part then we would do n minus non decimal and that will give us the decimal portion so that's definitely a better way of doing it um and what we can say instead is if that decimal portion is less than 0.5 that's better then we return int n otherwise we to round it up so let's oh i didn't give it a name so if i did let's do five one so that should round up to 155 cool and let's check one that won't let's do 49 that should go to 154 bottom so i've got that one marking and now the advanced question so we've got define a function that when pass two integers returns true if there is any digit that is present in both the numbers and false otherwise ah this is cool call the function inside set up so this should return false because you've got no common digits this should return true because nine is common very cool so we can do this one recursively as well we can do it recursively by slowly removing um a digit from one of them so um our base case here but how do we deal with zeros is what i'm wondering zero considered a digit how do they do it in the solution and while a is not equal to zero as long as this digits left okay so we're ignoring zero okay cool all right so um let's get started with this one is that up the screen um so what we are returning is true or false so you know our return type is boolean um they've called it common digit present so let's call it the same thing and our parameters are two integer parameters which we can call a and b so um our base case would be that one of them is zero so there's no point checking it if one of them is zero so if a or b is zero then return false because i mean zero and zero are common because they're the same but we want to forget about zeros in this case um okay so what do we want to do what we want to do is take a digit in a so let's say nine check if that digit matches any of the digits in b and if it does return true otherwise we want to check um the next digit in a which is two so we can combine loops and recursion with this one so um let's create a variable last digit a and that will be a mod 10 because that's gets our last digit and then we want a loop and we need to keep a copy of b somewhere because we don't want to destroy b so i'll call temp b equal to b and i'll say while temp b is not equal to zero i check if the already have a variable for that if the last digit in a is equal to the last digit in b which we'll say is b mod 10 well if the last digit in a is equal to the last digit in b that means we found a digit so we can just return true otherwise we want to check the next digit in b um and our condition sorry our loop says while temp b is not equal to zero so what we can do with temp b so actually we need to call this temp b um is we can slowly remove digits from this temporary variable until it's equal to zero um and then we can continue on again so we say temp b is divided by 10 um and we need to create a temporary variable for b because we need to use b um later on for our recursive function call if it comes to that so that's really important that we use a temporary one there um so we can assume if we get to the end of this loop that temp b is now zero and if that's the case and what i want to do is do a recursive function call and i want to do the same thing over and over again compare the last digit in a with every single digit in b but i've already checked nine so i can get rid of nine so i can do a divided by 10 so now i've got 172 and i'm checking it with b again and we've kept b because we've only mutated it on a temporary variable so we can keep looping through this until we've chopped off every digit in a which we'll do if we keep doing this recursive function call um and eventually it will be zero and this will return false so let's do some testing we can test it with theirs because we know what it's going to return so this should be false and this should be true so this isn't like exhaustive testing but it does the job here so it should be false false should be true true awesome so that looks like it's working um i guess we can do a quick trace here um maybe with a smaller number might be easier um um which case should i do i'm just going to change the name to foo because common digit present will take a while to write many times and perhaps i want to do it with um 28 and 123 this is a this is b is a or b equal to zero no um last digit a is equal to a mod 10 so perhaps we're not returning anything yet so let's remove this arrow and let's keep track of our variables so we've got last digit a let's do ld is equal to 8 we've got temp b is equal to b while temp b is not equal to zero if the last digit in a is equal to temp b mod 10 so is 8 equal to 3 no so um temp b is equal to temp b divided by 10 so now temp b is equal to 12 we loop around again if last digit is equal to temp b mod 10 so is 8 equal to 2 no so temp b is equal to temp b divided by 10 so now it's equal to 1 we loop around again is the last digit equal to the last digit here no so again we divide by 10 and at this case we've got zero and so our loop is terminated we're finished with our loop so that means that we're making our recursive function call where a is equal to a divided by 10 so now we've got two one two three and we start again so is a or b equal to zero no um so we've got the last digit which is a mod 10 which is just two and then we've got a temporary b variable and we do that loop again so as long as temp b is not equal to zero is the last digit in a equal to the last digit in b no so we chop that digit off we loop around again is the last digit in a equal to the last digit in b yes so there's a common digit um so that means that this condition is true so we're returning true cool so those are some examples of how to create some recursive functions again if you did them using similar solutions to these using loops and conditions and things like that that's totally fine um lots of different ways to answer the same question um but if you're interested in recursion um then it's definitely a good way of answering questions um yeah so maybe give it a try in some of your functions workshop questions this week just one last quick note about the advanced question um another solution if you uh don't want to do a loop is that you can create a second helper method a second helper recursive method that just takes in a single digit from a so that single last digit um and then another one b and or perhaps a better way of doing it would be x and y just to have variables of different names it's probably a better way to go um so instead of this loop i can say um if contains digit last digit a and b return true that way you don't need that temporary b and that is actually a little bit neater um just complaining about not having a bracket so it's doing the exact same thing except instead of that while loop i call a function that um just takes in that single digit which we had as last digit so eight um and then goes through b on its own recursively um and checks whether that digit's present and if it's present then it will um return true for this method here um so if you want to have a look at that screenshot that maybe you run it in debugger trace it like this um to understand that one further you actually don't need that variable make it even cleaner yeah that's a lot better um i i would go with that solution over the loop one for the advanced one anyway that's all from me