Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,300 --> 00:00:01,510
Welcome back.
2
00:00:01,530 --> 00:00:06,330
In this video I want to talk about an important concept which is return.
3
00:00:06,330 --> 00:00:11,290
It's a key word in Python that we are going to see a lot when working with functions.
4
00:00:11,340 --> 00:00:12,180
So let's have a look.
5
00:00:12,450 --> 00:00:19,560
If I let's say I want to create a function called sum which by the way already exists in Python but
6
00:00:19,590 --> 00:00:21,030
we're going to create our own.
7
00:00:21,030 --> 00:00:25,650
And I'm going to say number one for number one and number two.
8
00:00:25,830 --> 00:00:36,240
Well are some function is going to add name one plus name two nice and simple function.
9
00:00:36,240 --> 00:00:48,120
Now if I run some with number four and five guess what's about to happen I'm going to hit run and nothing
10
00:00:48,270 --> 00:00:49,070
happens.
11
00:00:49,230 --> 00:00:53,970
Which I mean might not be surprising to you because we're not printing anything right.
12
00:00:54,150 --> 00:00:56,710
But what if I run print here.
13
00:00:56,880 --> 00:00:58,440
Will this change.
14
00:00:58,500 --> 00:01:02,430
Let's see.
15
00:01:02,760 --> 00:01:05,750
I get none.
16
00:01:05,800 --> 00:01:07,760
Is that what you expected.
17
00:01:07,870 --> 00:01:10,120
And we've seen none before.
18
00:01:10,120 --> 00:01:10,770
Right.
19
00:01:10,780 --> 00:01:19,360
I mean remember when we had a list let's say one two three and then we did something like clear on it
20
00:01:20,420 --> 00:01:29,430
in our editor it gave us this little arrow and said none and we talked about this idea of on lists something
21
00:01:29,430 --> 00:01:37,920
like clear changes the actual list and clears everything and empties the list but it doesn't return
22
00:01:37,950 --> 00:01:45,940
a new list it just modifies the existing list but instead just returns None to us.
23
00:01:45,960 --> 00:01:55,350
Well functions are just like that they always have to return something and when they don't return anything
24
00:01:55,380 --> 00:02:05,530
like there's no return keyword here it automatically returns None so if we don't have something like
25
00:02:05,770 --> 00:02:13,480
return here it's always going to return nothing but if we add return it's going to say as soon as we
26
00:02:13,480 --> 00:02:21,420
get to lying to hate I want you to exit this function and when you exit this function I want you to
27
00:02:21,420 --> 00:02:24,450
return whatever this expression gives us.
28
00:02:24,750 --> 00:02:37,390
If I click Run I get nine if we change this to 10 plus five we get 15 and this is a good general rule
29
00:02:37,390 --> 00:02:41,710
with functions because functions can do two things right.
30
00:02:41,710 --> 00:02:44,690
One it can return something.
31
00:02:44,710 --> 00:02:51,880
So at the end of the function whatever after the function gets run we return something either none or
32
00:02:51,880 --> 00:02:59,140
some sort of a value some sort of a data type or we can have a function that doesn't return anything
33
00:02:59,170 --> 00:03:01,870
but perhaps modifies something.
34
00:03:01,870 --> 00:03:04,630
So for example I could print
35
00:03:07,770 --> 00:03:14,700
and if I hit run here I return None because I don't have that return statement.
36
00:03:14,700 --> 00:03:24,390
However my function does something it modifies Well the look of this tax write a prints high for us.
37
00:03:24,540 --> 00:03:34,360
So a function either modifies something in our program or returns something and this is a topic that
38
00:03:34,360 --> 00:03:38,940
is quite advanced that we'll get into later on when we talk about functional programming.
39
00:03:39,370 --> 00:03:52,130
But a good rule of thumb with functions is this a function should do one thing really well and usually
40
00:03:52,130 --> 00:04:02,740
a function should return something Now this doesn't mean that any function that maybe does multiple
41
00:04:02,740 --> 00:04:10,210
things or any function that doesn't return something is bad but it's generally good practice to make
42
00:04:10,210 --> 00:04:14,020
your code simple and readable to have these two rules.
43
00:04:14,110 --> 00:04:19,530
For example here if I have print and additions.
44
00:04:19,690 --> 00:04:23,040
Well this function is doing multiple things.
45
00:04:23,110 --> 00:04:24,730
It's worrying about printing.
46
00:04:24,730 --> 00:04:26,770
And it's also worrying about something.
47
00:04:26,950 --> 00:04:32,060
And the definition this function is called some.
48
00:04:32,170 --> 00:04:40,300
I mean if I didn't know or I didn't see this and all I did was run some I think having printing high
49
00:04:41,230 --> 00:04:44,770
would be surprising because the name doesn't really describe what it does.
50
00:04:44,770 --> 00:04:46,690
I mean I'm expecting this to be summed.
51
00:04:46,750 --> 00:04:49,550
Why is my program saying hi.
52
00:04:49,660 --> 00:04:55,840
So we want to make sure that our function does one thing and one thing only really well because as we
53
00:04:55,840 --> 00:04:59,920
will see later on we can combine and run different functions together.
54
00:05:00,370 --> 00:05:08,050
And then also we want to make sure that we return something because this way what we can do is we can
55
00:05:08,050 --> 00:05:13,110
actually assign this return value to a variable.
56
00:05:13,150 --> 00:05:25,230
For example I can say total equals to some 10 5 and because this function returns something and when
57
00:05:25,290 --> 00:05:28,640
it's done running is going to have 15 as a value.
58
00:05:28,680 --> 00:05:32,330
That's the same thing as what we're doing here.
59
00:05:32,470 --> 00:05:34,120
And here's the cool part.
60
00:05:34,210 --> 00:05:44,060
Now I can run maybe some 10 plus total.
61
00:05:44,200 --> 00:05:52,560
Now let's go through this one by one I define my function the Python interpreter is going to say all
62
00:05:52,560 --> 00:05:54,500
right I know what some is now.
63
00:05:54,540 --> 00:05:55,640
You've defined it.
64
00:05:55,680 --> 00:05:57,090
I'm going to keep it in memory.
65
00:05:57,090 --> 00:05:59,720
It's going to keep going keep going until line 7.
66
00:05:59,730 --> 00:06:06,670
And it's going to say hey I want a variable total and I want that total variable to equal some 10 5.
67
00:06:06,780 --> 00:06:12,060
And before we even assign anything to total Python interpreter is going to say some.
68
00:06:12,120 --> 00:06:13,200
What's some again.
69
00:06:13,200 --> 00:06:16,800
Oh I remember here it's in memory OK at some.
70
00:06:16,830 --> 00:06:19,890
I'm gonna give it argument's 10 and five.
71
00:06:19,890 --> 00:06:25,050
So it's going to go run that function as soon as we end this closing bracket.
72
00:06:25,050 --> 00:06:29,860
It's going to go into the sum it's going to say Hey Number one is going to equal 10.
73
00:06:29,910 --> 00:06:33,610
Hey num two is going to equal five.
74
00:06:33,680 --> 00:06:41,300
Now we go to line two and it's going to say hey I want you to add 10 plus five because I know what these
75
00:06:41,300 --> 00:06:42,690
variables are.
76
00:06:42,860 --> 00:06:48,530
And then once you're done with that and this turns into 15 I want you to return.
77
00:06:48,530 --> 00:06:54,800
So the Python interpreter finally when it's done calling some is going to say hey assign the value of
78
00:06:54,800 --> 00:06:57,790
15 to the variable total.
79
00:06:58,100 --> 00:06:59,580
And then it's going to go to the next line.
80
00:06:59,630 --> 00:07:01,210
Hey I want to print something.
81
00:07:01,220 --> 00:07:02,060
What do you want to print.
82
00:07:02,180 --> 00:07:04,670
Well I want to print this.
83
00:07:05,210 --> 00:07:06,880
It's going to say hey what some.
84
00:07:06,890 --> 00:07:08,180
I know what's on this.
85
00:07:08,210 --> 00:07:09,270
I have it in memory.
86
00:07:09,410 --> 00:07:15,110
And then again it's going to say 10 and then the next argument is going to be total Hey what's total.
87
00:07:15,110 --> 00:07:18,040
Let me go in memory and find out oh it's 15.
88
00:07:18,140 --> 00:07:25,990
So it's like saying 15 and this is the power of functions right.
89
00:07:26,070 --> 00:07:35,940
I'm able to return things and assign them to variables and call them like this and I can keep running
90
00:07:35,940 --> 00:07:41,620
them if I wanted to if I want to make this even simpler I can go some instead of total.
91
00:07:41,700 --> 00:07:44,560
We'll be right here and look at that.
92
00:07:44,610 --> 00:07:52,690
Now I have it all in one line and if I click run again it still works OK.
93
00:07:53,270 --> 00:07:57,310
But what if we do something like this.
94
00:07:57,710 --> 00:08:04,300
What if I say define some and then I'll say define inside of here.
95
00:08:04,670 --> 00:08:18,890
Another function and this function takes num 1 and num to an end here we finally return number one and
96
00:08:18,890 --> 00:08:20,950
number two.
97
00:08:21,730 --> 00:08:22,540
Let's test the set.
98
00:08:22,570 --> 00:08:23,620
What do you think will happen.
99
00:08:24,430 --> 00:08:38,810
Well first let's say total equals some 10 plus 20 and then I'm going to print Total if I click Run I
100
00:08:38,810 --> 00:08:41,590
get none now.
101
00:08:41,650 --> 00:08:42,840
Why is that.
102
00:08:43,420 --> 00:08:44,680
And this should be obvious to you.
103
00:08:44,800 --> 00:08:49,080
Because here I'm saying hey I want you to define some.
104
00:08:49,090 --> 00:08:51,920
And what are we going to do with the function some.
105
00:08:52,000 --> 00:08:55,960
Well inside of here I'm going to define another function.
106
00:08:55,960 --> 00:09:02,950
But this return statement is actually not run right because all we're doing inside of this block this
107
00:09:02,950 --> 00:09:12,560
code block is this part we're defining the function but we're never calling another function so this
108
00:09:12,650 --> 00:09:20,850
returns None and we've lost the ability to well to call this function.
109
00:09:20,930 --> 00:09:23,510
So how can we solve this.
110
00:09:23,510 --> 00:09:25,530
One way is to return.
111
00:09:25,580 --> 00:09:26,120
Right.
112
00:09:26,150 --> 00:09:30,350
So at the end here we have to make sure that the indentation is right.
113
00:09:30,530 --> 00:09:34,070
And we're going to return another func.
114
00:09:34,190 --> 00:09:37,180
So let's add return and see what happens here.
115
00:09:37,430 --> 00:09:44,020
If I click Run All right I get this function now.
116
00:09:44,080 --> 00:09:48,370
So total is going to equal this function that we returned.
117
00:09:48,610 --> 00:09:51,600
And it's in memory OK.
118
00:09:51,620 --> 00:09:54,290
But I'm not getting the result that I want.
119
00:09:54,290 --> 00:09:54,850
Right.
120
00:09:54,860 --> 00:09:56,030
And why is that.
121
00:09:56,030 --> 00:09:59,480
Because while we're not calling this function.
122
00:09:59,480 --> 00:10:01,460
So there's a few ways that we can do this.
123
00:10:01,550 --> 00:10:06,170
We can either give it the parameters or the arguments in here.
124
00:10:06,200 --> 00:10:08,240
So let's say 10 20.
125
00:10:08,300 --> 00:10:19,060
And if I click Run I get 30 or if we want to keep whatever we give it here I can just simply say another
126
00:10:19,060 --> 00:10:19,840
func.
127
00:10:19,930 --> 00:10:29,610
So if we give it num 1 and num two and I click Run Hey look at that I get 30.
128
00:10:29,710 --> 00:10:36,120
Now this video you might have to watch over and over because it does stretch your mind a little bit.
129
00:10:36,250 --> 00:10:42,380
And to be honest this function is not very good because it's well it's very confusing.
130
00:10:42,490 --> 00:10:44,980
Your function should be easy to understand.
131
00:10:45,550 --> 00:10:54,360
So we might want to change this up a bit maybe instead over here of having the same parameter names.
132
00:10:54,400 --> 00:11:05,050
Just to clarify it maybe we can say and one and and two and then here we do and one plus and two to
133
00:11:05,050 --> 00:11:12,310
clarify that when we return this other function I'm using the parameters number one and number two that
134
00:11:12,310 --> 00:11:20,340
we called here because this definition of a function doesn't really care what these things are.
135
00:11:20,340 --> 00:11:24,470
It only cares when another function gets called.
136
00:11:24,530 --> 00:11:30,710
Now if you need to pause the video and look at this and maybe practice this a little bit that's completely
137
00:11:30,710 --> 00:11:31,790
understandable.
138
00:11:31,880 --> 00:11:34,890
But I wanted to show you the power of return.
139
00:11:35,060 --> 00:11:41,810
You can have nested functions just like you could with conditionals but you have to be careful that
140
00:11:42,050 --> 00:11:45,090
your function either returns something.
141
00:11:45,140 --> 00:11:47,790
Otherwise it's going to return None.
142
00:11:48,110 --> 00:11:52,550
And most of the time you're going to want to return something from a function.
143
00:11:52,550 --> 00:11:59,180
So we can do something like this where we assign it to a variable so we can use it later on in our programs.
144
00:11:59,180 --> 00:12:00,720
I'll see in the next one by
145
00:12:03,800 --> 00:12:13,620
one last thing a return keyword automatically exits the function so that in here if I added another
146
00:12:13,620 --> 00:12:24,410
piece of code like print Hello or maybe even have return five in here you see that it's still valid
147
00:12:24,650 --> 00:12:34,420
but if I run this I still get 30 because the interpreter never gets to line five and six because as
148
00:12:34,420 --> 00:12:39,930
soon as we return something from a function it exits that function.
149
00:12:40,570 --> 00:12:47,530
And the reason that this part doesn't exit the function is because remember inside of this function
150
00:12:47,860 --> 00:12:48,950
we're defining.
151
00:12:49,060 --> 00:12:55,360
So we're not actually running this part we're just saying hey when we see another function coming up
152
00:12:55,480 --> 00:12:59,040
in our code we know what it is but we're not going to run it.
153
00:12:59,050 --> 00:13:00,460
So it's going to look like this.
154
00:13:00,560 --> 00:13:05,590
And as soon as it sees the return statement when it gets cold it's going to say alright I'm exiting
155
00:13:06,160 --> 00:13:10,820
out of this function with this value just a little heads up.
15330
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.