1
00:00:00,790 --> 00:00:02,931
Hi guys and welcome back.

2
00:00:02,931 --> 00:00:03,930
In this video we're going to learn about

3
00:00:03,930 --> 00:00:05,870
return values and functions.

4
00:00:05,870 --> 00:00:07,340
And that is something really confusing

5
00:00:07,340 --> 00:00:09,165
for new Python developers.

6
00:00:09,165 --> 00:00:11,870
Here we've got a function called add

7
00:00:11,870 --> 00:00:13,990
that prints X and Y.

8
00:00:13,990 --> 00:00:16,310
If we call add with the values five and eight

9
00:00:16,310 --> 00:00:17,760
it should print 13.

10
00:00:17,760 --> 00:00:19,133
So let's run that and you'll see

11
00:00:19,133 --> 00:00:21,250
that 13 gets printed out.

12
00:00:21,250 --> 00:00:24,060
However, something that we have been doing

13
00:00:24,060 --> 00:00:25,950
with some other functions in our code

14
00:00:25,950 --> 00:00:29,450
has been assigning the value of it to a variable.

15
00:00:29,450 --> 00:00:32,460
For example result, equal, add, five and eight.

16
00:00:32,460 --> 00:00:36,400
If we then print result you could potentially

17
00:00:36,400 --> 00:00:39,680
expect 13 to be printed out.

18
00:00:39,680 --> 00:00:42,090
However, here's what's gonna happen.

19
00:00:42,090 --> 00:00:44,840
We're going to run line five which is going to

20
00:00:44,840 --> 00:00:48,410
start running the function and it's going to print 13.

21
00:00:48,410 --> 00:00:50,490
Then we're going to run line six

22
00:00:50,490 --> 00:00:52,790
which is again going to run the function,

23
00:00:52,790 --> 00:00:55,710
because here, notice there is a function call

24
00:00:55,710 --> 00:00:57,420
and that is going to run this line,

25
00:00:57,420 --> 00:00:59,270
which is going to print 13.

26
00:00:59,270 --> 00:01:04,180
Then we're going to assign something to the result variable,

27
00:01:04,180 --> 00:01:06,890
and finally we're going to print the result variable.

28
00:01:06,890 --> 00:01:11,890
So we should see 13, 13, and then the value

29
00:01:12,220 --> 00:01:13,810
of the result variable printed out.

30
00:01:13,810 --> 00:01:15,810
So we're going to print out three lines.

31
00:01:17,460 --> 00:01:22,320
And you can see that we get 13, 13, and then None.

32
00:01:22,320 --> 00:01:25,320
None is a special value in Python that means

33
00:01:25,320 --> 00:01:28,510
no value, or missing value, or undeclared value.

34
00:01:28,510 --> 00:01:29,730
Take your pick.

35
00:01:29,730 --> 00:01:32,920
So that means that this function here,

36
00:01:32,920 --> 00:01:36,360
when it finished running did something

37
00:01:36,360 --> 00:01:40,010
that made our result variable be equal to None,

38
00:01:40,010 --> 00:01:42,610
because when we printed result it printed None,

39
00:01:42,610 --> 00:01:44,450
therefore result must be None.

40
00:01:44,450 --> 00:01:48,250
So that is where return values come in.

41
00:01:48,250 --> 00:01:50,700
Functions return None.

42
00:01:50,700 --> 00:01:53,200
That's what they do by default.

43
00:01:53,200 --> 00:01:55,520
That means that whenever you call a function

44
00:01:55,520 --> 00:01:59,140
you can assign it's return value to a variable

45
00:01:59,140 --> 00:02:01,900
or you can use it in another part of your programme,

46
00:02:01,900 --> 00:02:05,142
such as a mathematical expression or something like that,

47
00:02:05,142 --> 00:02:07,190
and that return value is what you get

48
00:02:07,190 --> 00:02:08,470
when you call a function.

49
00:02:08,470 --> 00:02:10,530
So, functions return None by default

50
00:02:10,530 --> 00:02:13,044
which means that the result variable

51
00:02:13,044 --> 00:02:14,310
is being assigned to None.

52
00:02:14,310 --> 00:02:16,340
If we want to do it differently

53
00:02:16,340 --> 00:02:19,230
you're going to have to return your own value.

54
00:02:19,230 --> 00:02:21,880
So we're gonna say return X plus Y

55
00:02:21,880 --> 00:02:25,380
without the brackets instead of print.

56
00:02:25,380 --> 00:02:27,280
Now there's an important thing,

57
00:02:27,280 --> 00:02:30,050
which is that if we remove these lines-

58
00:02:30,050 --> 00:02:31,470
I'm just going to put the hash symbol

59
00:02:31,470 --> 00:02:34,610
in front of them which signals that they are a comment

60
00:02:34,610 --> 00:02:37,160
and they won't run when the code runs.

61
00:02:37,160 --> 00:02:39,260
If we press play now you'll see that

62
00:02:39,260 --> 00:02:41,560
nothing gets printed out, because of course

63
00:02:41,560 --> 00:02:44,090
we've executed the add function

64
00:02:44,090 --> 00:02:46,410
but this function does not print anything.

65
00:02:46,410 --> 00:02:48,550
It just returns something.

66
00:02:48,550 --> 00:02:50,720
So, because we're not printing anything

67
00:02:50,720 --> 00:02:52,310
nothing gets printed out.

68
00:02:52,310 --> 00:02:57,290
Now if we do this, we call the add function

69
00:02:57,290 --> 00:02:59,930
which returns the addition of the two arguments,

70
00:02:59,930 --> 00:03:02,360
five and eight, and then make it equal to

71
00:03:02,360 --> 00:03:03,483
the result variable.

72
00:03:04,317 --> 00:03:05,670
That means that result is going to be equal to

73
00:03:05,670 --> 00:03:08,580
the return value of the function which is gonna be 13.

74
00:03:08,580 --> 00:03:10,750
And then we're going to print 13.

75
00:03:10,750 --> 00:03:13,563
so when we run this code you'll see 13.

76
00:03:14,800 --> 00:03:17,380
At this point I'll say that very often

77
00:03:17,380 --> 00:03:19,270
I will see students doing this,

78
00:03:19,270 --> 00:03:24,270
print X and Y, and then they will try to do this.

79
00:03:25,520 --> 00:03:27,953
Print, add, five and eight.

80
00:03:29,200 --> 00:03:32,350
This is a very common thing I see in my course,

81
00:03:32,350 --> 00:03:35,240
and notice that when you run this code,

82
00:03:35,240 --> 00:03:37,940
you're going to get 13 and then None.

83
00:03:37,940 --> 00:03:39,870
Again, that's because the add function

84
00:03:39,870 --> 00:03:43,040
gets called first so that the print function

85
00:03:43,040 --> 00:03:44,490
knows what to print.

86
00:03:44,490 --> 00:03:48,010
So here it prints 13 and then returns None,

87
00:03:48,010 --> 00:03:50,900
because all functions return None by default.

88
00:03:50,900 --> 00:03:54,620
So you get 13 printed out and then you get None printed out.

89
00:03:54,620 --> 00:03:56,790
And this is very common, so if you're seeing this problem

90
00:03:56,790 --> 00:03:58,170
or you've encountered this before,

91
00:03:58,170 --> 00:03:59,473
now you know why that is.

92
00:04:01,260 --> 00:04:03,440
Let's go back to printing the result

93
00:04:03,440 --> 00:04:05,540
of calling the function and we're going

94
00:04:05,540 --> 00:04:08,590
to make it both print and return.

95
00:04:08,590 --> 00:04:10,350
So now when we do this you're going to

96
00:04:10,350 --> 00:04:13,100
see 13 printed out twice.

97
00:04:13,100 --> 00:04:16,480
One, because the function itself prints 13 out,

98
00:04:16,480 --> 00:04:18,070
and the second one because we're actually

99
00:04:18,070 --> 00:04:20,070
printing the return value of the function

100
00:04:20,070 --> 00:04:22,450
that we've assigned to our variable.

101
00:04:22,450 --> 00:04:25,140
Return not only gives a value back to

102
00:04:25,140 --> 00:04:26,780
the caller at the end of the function,

103
00:04:26,780 --> 00:04:29,440
it actually terminates the function entirely.

104
00:04:29,440 --> 00:04:31,810
So if we were to move this return value

105
00:04:31,810 --> 00:04:35,770
to before the print and then press play

106
00:04:35,770 --> 00:04:38,470
you'll see that only one 13 gets printed out,

107
00:04:38,470 --> 00:04:40,420
because as soon as the function reaches

108
00:04:41,864 --> 00:04:44,290
this return statement it returns and exits the function

109
00:04:44,290 --> 00:04:46,370
and gives the value back to the caller.

110
00:04:46,370 --> 00:04:50,230
So this print function never gets called.

111
00:04:50,230 --> 00:04:52,197
Only this one does.

112
00:04:52,197 --> 00:04:55,740
That is another common problem that

113
00:04:55,740 --> 00:04:58,090
some people will put a return value

114
00:04:58,090 --> 00:05:01,170
elsewhere in the function as well as

115
00:05:01,170 --> 00:05:02,640
the one at the end of the function.

116
00:05:02,640 --> 00:05:04,400
This is totally fine, you can do that.

117
00:05:04,400 --> 00:05:06,400
But just make sure that it is what you want.

118
00:05:06,400 --> 00:05:09,453
Because when you return without a value

119
00:05:09,453 --> 00:05:12,440
you're actually going to return None by default.

120
00:05:12,440 --> 00:05:14,590
So by putting a return here in line two

121
00:05:14,590 --> 00:05:16,870
it means that none of these lines are going to run.

122
00:05:16,870 --> 00:05:18,640
Because the function will terminate when you

123
00:05:18,640 --> 00:05:22,930
encounter a return and this here will become None

124
00:05:22,930 --> 00:05:25,570
because there is a return with no value

125
00:05:25,570 --> 00:05:27,330
which means return None.

126
00:05:27,330 --> 00:05:28,520
So our result will be None and

127
00:05:28,520 --> 00:05:30,090
you'll end up printing None out.

128
00:05:30,090 --> 00:05:32,853
If we press play you'll see that none comes out.

129
00:05:35,540 --> 00:05:38,310
Let's go back to our divide function from the last video,

130
00:05:38,310 --> 00:05:41,790
but now instead of printing out, we're going to return.

131
00:05:41,790 --> 00:05:43,170
So here we've got our divide function

132
00:05:43,170 --> 00:05:44,923
with a dividend and a divisor.

133
00:05:44,923 --> 00:05:46,350
If the divisor is not zero,

134
00:05:46,350 --> 00:05:47,920
we're going to return the division,

135
00:05:47,920 --> 00:05:50,660
otherwise we're going to return the string, you fool.

136
00:05:50,660 --> 00:05:53,920
So if we divide 15 by three and we print the result out

137
00:05:53,920 --> 00:05:55,800
we should see a number coming out

138
00:05:55,800 --> 00:05:57,910
and we do get 5.0.

139
00:05:57,910 --> 00:06:01,010
But if you decide to go for 15 and zero

140
00:06:01,010 --> 00:06:02,650
and then you press play,

141
00:06:02,650 --> 00:06:04,833
you'll see that you fool gets printed out.

142
00:06:05,680 --> 00:06:08,060
This is fine, you can totally do two returns

143
00:06:08,060 --> 00:06:10,920
in a single function as long as only one of them runs,

144
00:06:10,920 --> 00:06:12,610
which is what happens in this if statement.

145
00:06:12,610 --> 00:06:15,640
You can still access both returns depending

146
00:06:15,640 --> 00:06:17,510
on these conditions here.

147
00:06:17,510 --> 00:06:20,870
So again a return will terminate the function

148
00:06:20,870 --> 00:06:23,130
when the line is ran and give the value

149
00:06:23,130 --> 00:06:24,870
returned back to the caller.

150
00:06:24,870 --> 00:06:27,498
So in this case, this function call here

151
00:06:27,498 --> 00:06:29,560
will be replaced, essentially,

152
00:06:29,560 --> 00:06:32,900
by whatever the function returns when it runs.

153
00:06:32,900 --> 00:06:34,740
As an aside it's usually not recommended

154
00:06:34,740 --> 00:06:36,530
to have functions returning multiple

155
00:06:36,530 --> 00:06:38,240
different types of data.

156
00:06:38,240 --> 00:06:40,920
For example this function can return a number or a string.

157
00:06:40,920 --> 00:06:43,375
Just because if you wanted to perform mathematics

158
00:06:43,375 --> 00:06:45,870
on the return value of this function

159
00:06:45,870 --> 00:06:50,170
you will encounter problems if you do return a string.

160
00:06:50,170 --> 00:06:53,690
For example if you wanted to print out the result

161
00:06:53,690 --> 00:06:56,680
of dividing this multiplied by five,

162
00:06:56,680 --> 00:06:58,870
then of course you're going to get

163
00:06:58,870 --> 00:07:00,900
you fool, you fool, you fool, you fool.

164
00:07:00,900 --> 00:07:02,540
However if you change this number

165
00:07:02,540 --> 00:07:05,760
then you'll actually get a valid mathematical number.

166
00:07:05,760 --> 00:07:08,200
So, usually not recommended to return

167
00:07:08,200 --> 00:07:09,840
different types of data from a function.

168
00:07:09,840 --> 00:07:12,580
Although as I said, you can do it,

169
00:07:12,580 --> 00:07:15,280
it's just not advised.

170
00:07:15,280 --> 00:07:16,510
That's everything for this video.

171
00:07:16,510 --> 00:07:17,850
Thank you for joining me in this one,

172
00:07:17,850 --> 00:07:19,500
and I'll see you in the next one.

