﻿1
00:00:04,440 --> 00:00:05,490
‫Hello and welcome.

2
00:00:05,490 --> 00:00:10,860
‫In this lecture, we're going to be looking at how we can return values out of our functions using different

3
00:00:10,860 --> 00:00:13,800
‫return types and a return statement.

4
00:00:13,800 --> 00:00:16,260
‫Let's dive in and see how this works.

5
00:00:16,860 --> 00:00:23,820
‫So last lecture, we said that if our function doesn't return a value, then the return type should

6
00:00:23,820 --> 00:00:26,040
‫be void, which is what we've done here.

7
00:00:26,040 --> 00:00:34,170
‫But what if we wanted to take a function such as get basically checking whether we should turn around

8
00:00:34,500 --> 00:00:40,170
‫this function here in the if statement, if we could say should turn around instead, it would be a

9
00:00:40,170 --> 00:00:42,690
‫much cleaner function at the end of the day.

10
00:00:42,690 --> 00:00:44,520
‫So how do we do that?

11
00:00:44,520 --> 00:00:49,530
‫How do we achieve a function that can return a Boolean expression?

12
00:00:50,040 --> 00:00:55,920
‫Well, the way we do it is simply by using a type instead of the void.

13
00:00:55,920 --> 00:01:03,660
‫So if we were to want to return a boolean expression because type bool like we would for a variable.

14
00:01:03,660 --> 00:01:08,130
‫But the thing that distinguishes the function from the variable is the fact that we put parentheses

15
00:01:08,130 --> 00:01:10,440
‫after it that makes it a function rather than a variable.

16
00:01:10,620 --> 00:01:18,510
‫And what we could do is call it should platform return and then we put our parentheses.

17
00:01:18,510 --> 00:01:24,420
‫And in this case, I don't have anything that I want to pass in as an argument to this function, so

18
00:01:24,420 --> 00:01:29,250
‫I can just leave the parameter list empty, put semicolon at the end of the line and then copy this

19
00:01:29,250 --> 00:01:31,620
‫entire line and go over to the moving platform.

20
00:01:31,620 --> 00:01:37,140
‫CP And right at the end of the file you could put it anywhere you like really between any of the functions.

21
00:01:37,140 --> 00:01:39,690
‫Just make sure it's not inside a function.

22
00:01:39,840 --> 00:01:41,160
‫I'm going to paste it in here.

23
00:01:41,160 --> 00:01:46,170
‫I'm going to indent it and I'm going to add the class and.

24
00:01:46,850 --> 00:01:48,380
‫The scope resolution operates.

25
00:01:48,380 --> 00:01:56,210
‫So a moving platform colon colon should platform return, then remove the semicolon, add some braces

26
00:01:56,210 --> 00:01:56,900
‫instead.

27
00:01:57,500 --> 00:02:04,160
‫And what we want to do here is if we want it to actually return a result, we need to use a special

28
00:02:04,160 --> 00:02:10,100
‫keyword return, followed by an expression of the correct type.

29
00:02:10,100 --> 00:02:15,050
‫So in this case it has to be a boolean expression so I could return true and then put semicolon on the

30
00:02:15,050 --> 00:02:19,100
‫end, or I could return false and then put a semicolon at the end.

31
00:02:19,580 --> 00:02:21,380
‫But it doesn't have to be that simple.

32
00:02:21,380 --> 00:02:23,750
‫It could actually be any expression.

33
00:02:23,750 --> 00:02:29,000
‫It could be another call to a function, it could be a variable, it could be a use of some operators

34
00:02:29,000 --> 00:02:31,790
‫as we have up here inside our statement.

35
00:02:32,000 --> 00:02:34,820
‫So, in fact, let's go ahead and try doing that.

36
00:02:34,820 --> 00:02:36,800
‫I'm going to take this expression.

37
00:02:36,800 --> 00:02:41,330
‫The distance moved and is is greater than mov distance.

38
00:02:41,330 --> 00:02:49,370
‫I'm going to cut it out and I'm going to put it as the return expression for this should platform return

39
00:02:49,370 --> 00:02:50,180
‫function.

40
00:02:50,540 --> 00:02:56,210
‫You notice it's going to complain here because distance move isn't a local variable within our should

41
00:02:56,210 --> 00:02:58,220
‫platform return function.

42
00:02:58,520 --> 00:03:00,290
‫So we're going to have to move some more things over.

43
00:03:00,290 --> 00:03:06,830
‫But before we do that, I don't want to forget to put a call to this should platform return in an if

44
00:03:06,830 --> 00:03:12,530
‫statement so should platform return and then remember to put the parentheses so the function actually

45
00:03:12,530 --> 00:03:14,900
‫gets called inside our F statement.

46
00:03:14,900 --> 00:03:17,180
‫So now it's starting to read a bit better to me.

47
00:03:17,180 --> 00:03:21,410
‫If platform should return, then we do all of the following.

48
00:03:22,100 --> 00:03:29,600
‫So now what we need to do is bring across the distance moved variable that we've got in MOVE platform.

49
00:03:29,600 --> 00:03:31,400
‫We don't actually need it inside MOVE platform.

50
00:03:31,400 --> 00:03:35,990
‫I'm going to cut it out and bring it and put it on the line before I return statement.

51
00:03:35,990 --> 00:03:43,760
‫Usually return statement is the last thing in a function because once you've returned out of a function,

52
00:03:43,880 --> 00:03:45,590
‫you actually don't do anything else.

53
00:03:45,590 --> 00:03:53,720
‫If I were to put something like a log message, so I'll put a you log after my return statement with

54
00:03:53,720 --> 00:03:55,010
‫some sort of message.

55
00:03:55,430 --> 00:04:01,790
‫Usually what's going to happen is you'll get a warning from your compiler saying that this line will

56
00:04:01,790 --> 00:04:05,480
‫never be reached and that you probably didn't mean to do that.

57
00:04:05,780 --> 00:04:08,930
‫So the return statement usually is the last thing.

58
00:04:09,590 --> 00:04:16,820
‫So having moved across the line that defines the distance moved in should platform return function,

59
00:04:16,820 --> 00:04:22,520
‫you can see it's now saying, hey, I don't have a current location and well the current location we

60
00:04:22,520 --> 00:04:28,130
‫can just very easily grab, we've set the current location just before we're calling should platform

61
00:04:28,130 --> 00:04:28,610
‫return.

62
00:04:28,610 --> 00:04:32,330
‫So actually get active location is exactly what we want.

63
00:04:32,330 --> 00:04:38,900
‫So I'm just going to put that instead of a variable, I'm just going to put a call to get active location

64
00:04:39,230 --> 00:04:42,200
‫in the place and should platform return.

65
00:04:42,200 --> 00:04:48,590
‫So now what we've got is the distance moved is equal to the of distance between the start location and

66
00:04:48,590 --> 00:04:50,480
‫the get active current location.

67
00:04:50,810 --> 00:04:56,000
‫So if that's greater than configured move distance, then yes, the platform should return.

68
00:04:56,000 --> 00:05:01,520
‫That's what this function is saying and it allows us to use it at a higher level of abstraction from

69
00:05:01,520 --> 00:05:03,890
‫within our MOVE platform function.

70
00:05:04,190 --> 00:05:09,590
‫Now you'll notice that it's now complaining that we can't get the distance move to print out overshoot.

71
00:05:09,590 --> 00:05:10,970
‫Well, that's no big deal.

72
00:05:11,000 --> 00:05:13,760
‫I don't actually want to print that anymore.

73
00:05:13,760 --> 00:05:15,470
‫It's kind of messing up my function anyway.

74
00:05:15,470 --> 00:05:18,200
‫So I'm going to delete the overshoot variable line.

75
00:05:18,200 --> 00:05:23,390
‫I'm going to delete the get name line, I'm going to delete the log message because it was only there

76
00:05:23,390 --> 00:05:26,900
‫temporarily, hence the log temp name.

77
00:05:27,290 --> 00:05:32,210
‫So now our move platform function is starting to look a lot cleaner.

78
00:05:32,210 --> 00:05:35,480
‫We can see that we are doing some updating of the location here.

79
00:05:35,480 --> 00:05:38,480
‫We can see whether the platform should return and if it should.

80
00:05:38,480 --> 00:05:41,660
‫Then we are updating the location and flipping it around.

81
00:05:41,660 --> 00:05:48,740
‫Now one thing that I think would look a little bit better here is if we had an alt statement instead

82
00:05:48,740 --> 00:05:51,230
‫of having this bunch of code at the top.

83
00:05:51,230 --> 00:05:58,190
‫So if the platform should return, we will execute this else we will execute the normal code in terms

84
00:05:58,190 --> 00:05:59,570
‫of moving it forward.

85
00:05:59,570 --> 00:06:03,860
‫So remember we cast your mind back to when we talked about the alt statement.

86
00:06:03,890 --> 00:06:07,610
‫It just comes after an if statement and we just do it like this.

87
00:06:07,610 --> 00:06:12,800
‫After the braces, we have a new line and type else, then another new line and open some new parentheses

88
00:06:12,800 --> 00:06:17,120
‫and we can just move this code that's up here at the top of our tick.

89
00:06:17,120 --> 00:06:20,510
‫We can take it and move it down into the alt statement.

90
00:06:20,510 --> 00:06:22,850
‫So now it seems pretty straightforward.

91
00:06:22,850 --> 00:06:26,900
‫Should the platform return then do the code that does platform returning else?

92
00:06:26,900 --> 00:06:30,740
‫We're moving the platform forward with our platform velocity.

93
00:06:30,740 --> 00:06:35,030
‫Now I'd like to challenge you to create your own function with a return value.

94
00:06:35,030 --> 00:06:38,060
‫I'd like you to add a get distance moved function.

95
00:06:38,060 --> 00:06:41,990
‫So we've currently calculate this into a variable called distance moved.

96
00:06:42,020 --> 00:06:45,860
‫I'd like you to replace that with a call to a function instead.

97
00:06:46,310 --> 00:06:53,630
‫So it should return a float because that's how the type of distance moved, we should call it in should

98
00:06:53,630 --> 00:06:54,410
‫platform return.

99
00:06:54,410 --> 00:06:59,000
‫So instead of using the vector dist, we should be using our own implementation.

100
00:06:59,000 --> 00:07:02,000
‫So what code should we should it contain?

101
00:07:02,000 --> 00:07:04,190
‫What code should we put into it with?

102
00:07:04,220 --> 00:07:08,960
‫The hint is that we've already typed this code, so you are just copying and pasting it into the new

103
00:07:08,960 --> 00:07:09,680
‫function.

104
00:07:09,680 --> 00:07:14,960
‫But have a little think about what the return expression should be, such that what we're returning

105
00:07:14,960 --> 00:07:17,390
‫is the distance moved value.

106
00:07:17,420 --> 00:07:19,220
‫Pause the video and have a go.

107
00:07:22,950 --> 00:07:23,850
‫Welcome back.

108
00:07:24,150 --> 00:07:26,150
‫So let's go over to our head of file.

109
00:07:26,160 --> 00:07:33,330
‫We'll add a new function here with a float return value called get distance moved.

110
00:07:33,330 --> 00:07:36,240
‫And then we're going to have no arguments in the parentheses.

111
00:07:36,420 --> 00:07:39,150
‫And then we're going to go over to our moving platform.

112
00:07:39,150 --> 00:07:51,600
‫CP We're going to add in a float, a moving platform, colon, colon distance moved or get distance

113
00:07:51,600 --> 00:07:53,550
‫moved, I should say parentheses.

114
00:07:53,550 --> 00:07:58,020
‫And then we'll put a new set of braces on the next line.

115
00:07:58,410 --> 00:08:04,320
‫And what we want to return is literally just the expression we're using to create our distance move

116
00:08:04,320 --> 00:08:07,950
‫float so I can cut that out and paste it in here.

117
00:08:07,950 --> 00:08:14,280
‫So it's f vector, colon, colon, dist, start location, get active location and put semicolon at

118
00:08:14,280 --> 00:08:15,240
‫the end of that line.

119
00:08:15,390 --> 00:08:17,610
‫And that means that in should platform return.

120
00:08:17,610 --> 00:08:22,560
‫Now instead of having this distance moved variable, I'm going to delete that line that's half completed.

121
00:08:22,950 --> 00:08:27,630
‫We're going to return get distance moved.

122
00:08:28,080 --> 00:08:31,890
‫Parentheses is greater than the move distance.

123
00:08:31,890 --> 00:08:39,660
‫So we've kind of taken what was going into a variable and pulled it out into its own function instead.

124
00:08:39,660 --> 00:08:45,180
‫And this means that whenever we want to refer to the distance moved, if we did want to have kept that

125
00:08:45,180 --> 00:08:51,990
‫log message in MOVE platform, then we can easily just call the function, get distance moved from any

126
00:08:51,990 --> 00:08:53,880
‫of the functions in our class.

127
00:08:53,880 --> 00:08:54,450
‫Great stuff.

128
00:08:54,450 --> 00:09:00,570
‫So in this lecture we've learned about return statements and return values, and we've used them to

129
00:09:00,570 --> 00:09:03,120
‫create two new functions in our class.

130
00:09:03,120 --> 00:09:04,590
‫I'll see you in the next lecture.

