‫Hello and welcome.
‫In this lecture, we're going to be looking at how we can return values out of our functions using different
‫return types and a return statement.
‫Let's dive in and see how this works.
‫So last lecture, we said that if our function doesn't return a value, then the return type should
‫be void, which is what we've done here.
‫But what if we wanted to take a function such as get basically checking whether we should turn around
‫this function here in the if statement, if we could say should turn around instead, it would be a
‫much cleaner function at the end of the day.
‫So how do we do that?
‫How do we achieve a function that can return a Boolean expression?
‫Well, the way we do it is simply by using a type instead of the void.
‫So if we were to want to return a boolean expression because type bool like we would for a variable.
‫But the thing that distinguishes the function from the variable is the fact that we put parentheses
‫after it that makes it a function rather than a variable.
‫And what we could do is call it should platform return and then we put our parentheses.
‫And in this case, I don't have anything that I want to pass in as an argument to this function, so
‫I can just leave the parameter list empty, put semicolon at the end of the line and then copy this
‫entire line and go over to the moving platform.
‫CP And right at the end of the file you could put it anywhere you like really between any of the functions.
‫Just make sure it's not inside a function.
‫I'm going to paste it in here.
‫I'm going to indent it and I'm going to add the class and.
‫The scope resolution operates.
‫So a moving platform colon colon should platform return, then remove the semicolon, add some braces
‫instead.
‫And what we want to do here is if we want it to actually return a result, we need to use a special
‫keyword return, followed by an expression of the correct type.
‫So in this case it has to be a boolean expression so I could return true and then put semicolon on the
‫end, or I could return false and then put a semicolon at the end.
‫But it doesn't have to be that simple.
‫It could actually be any expression.
‫It could be another call to a function, it could be a variable, it could be a use of some operators
‫as we have up here inside our statement.
‫So, in fact, let's go ahead and try doing that.
‫I'm going to take this expression.
‫The distance moved and is is greater than mov distance.
‫I'm going to cut it out and I'm going to put it as the return expression for this should platform return
‫function.
‫You notice it's going to complain here because distance move isn't a local variable within our should
‫platform return function.
‫So we're going to have to move some more things over.
‫But before we do that, I don't want to forget to put a call to this should platform return in an if
‫statement so should platform return and then remember to put the parentheses so the function actually
‫gets called inside our F statement.
‫So now it's starting to read a bit better to me.
‫If platform should return, then we do all of the following.
‫So now what we need to do is bring across the distance moved variable that we've got in MOVE platform.
‫We don't actually need it inside MOVE platform.
‫I'm going to cut it out and bring it and put it on the line before I return statement.
‫Usually return statement is the last thing in a function because once you've returned out of a function,
‫you actually don't do anything else.
‫If I were to put something like a log message, so I'll put a you log after my return statement with
‫some sort of message.
‫Usually what's going to happen is you'll get a warning from your compiler saying that this line will
‫never be reached and that you probably didn't mean to do that.
‫So the return statement usually is the last thing.
‫So having moved across the line that defines the distance moved in should platform return function,
‫you can see it's now saying, hey, I don't have a current location and well the current location we
‫can just very easily grab, we've set the current location just before we're calling should platform
‫return.
‫So actually get active location is exactly what we want.
‫So I'm just going to put that instead of a variable, I'm just going to put a call to get active location
‫in the place and should platform return.
‫So now what we've got is the distance moved is equal to the of distance between the start location and
‫the get active current location.
‫So if that's greater than configured move distance, then yes, the platform should return.
‫That's what this function is saying and it allows us to use it at a higher level of abstraction from
‫within our MOVE platform function.
‫Now you'll notice that it's now complaining that we can't get the distance move to print out overshoot.
‫Well, that's no big deal.
‫I don't actually want to print that anymore.
‫It's kind of messing up my function anyway.
‫So I'm going to delete the overshoot variable line.
‫I'm going to delete the get name line, I'm going to delete the log message because it was only there
‫temporarily, hence the log temp name.
‫So now our move platform function is starting to look a lot cleaner.
‫We can see that we are doing some updating of the location here.
‫We can see whether the platform should return and if it should.
‫Then we are updating the location and flipping it around.
‫Now one thing that I think would look a little bit better here is if we had an alt statement instead
‫of having this bunch of code at the top.
‫So if the platform should return, we will execute this else we will execute the normal code in terms
‫of moving it forward.
‫So remember we cast your mind back to when we talked about the alt statement.
‫It just comes after an if statement and we just do it like this.
‫After the braces, we have a new line and type else, then another new line and open some new parentheses
‫and we can just move this code that's up here at the top of our tick.
‫We can take it and move it down into the alt statement.
‫So now it seems pretty straightforward.
‫Should the platform return then do the code that does platform returning else?
‫We're moving the platform forward with our platform velocity.
‫Now I'd like to challenge you to create your own function with a return value.
‫I'd like you to add a get distance moved function.
‫So we've currently calculate this into a variable called distance moved.
‫I'd like you to replace that with a call to a function instead.
‫So it should return a float because that's how the type of distance moved, we should call it in should
‫platform return.
‫So instead of using the vector dist, we should be using our own implementation.
‫So what code should we should it contain?
‫What code should we put into it with?
‫The hint is that we've already typed this code, so you are just copying and pasting it into the new
‫function.
‫But have a little think about what the return expression should be, such that what we're returning
‫is the distance moved value.
‫Pause the video and have a go.
‫Welcome back.
‫So let's go over to our head of file.
‫We'll add a new function here with a float return value called get distance moved.
‫And then we're going to have no arguments in the parentheses.
‫And then we're going to go over to our moving platform.
‫CP We're going to add in a float, a moving platform, colon, colon distance moved or get distance
‫moved, I should say parentheses.
‫And then we'll put a new set of braces on the next line.
‫And what we want to return is literally just the expression we're using to create our distance move
‫float so I can cut that out and paste it in here.
‫So it's f vector, colon, colon, dist, start location, get active location and put semicolon at
‫the end of that line.
‫And that means that in should platform return.
‫Now instead of having this distance moved variable, I'm going to delete that line that's half completed.
‫We're going to return get distance moved.
‫Parentheses is greater than the move distance.
‫So we've kind of taken what was going into a variable and pulled it out into its own function instead.
‫And this means that whenever we want to refer to the distance moved, if we did want to have kept that
‫log message in MOVE platform, then we can easily just call the function, get distance moved from any
‫of the functions in our class.
‫Great stuff.
‫So in this lecture we've learned about return statements and return values, and we've used them to
‫create two new functions in our class.
‫I'll see you in the next lecture.