Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
WEBVTT
1
00:00:00.000 --> 00:00:04.000
Alright here's an exercise for you
2
00:00:04.000 --> 00:00:08.000
I want you to write a function that takes two numbers and returns
3
00:00:08.000 --> 00:00:12.000
the maximum of the two. Call that function, give a different
4
00:00:12.000 --> 00:00:16.000
argument, and make sure it's working properly. So pause the video,
5
00:00:16.000 --> 00:00:20.000
do this exercise, and when you're done come back and continue watching.
6
00:00:24.000 --> 00:00:28.000
I'm going to start by defining a function, call it max,
7
00:00:28.000 --> 00:00:32.000
here we need two parameters, we can call them number 1, and
8
00:00:32.000 --> 00:00:36.000
number2, or we can use shorter names like
9
00:00:36.000 --> 00:00:40.000
a, and b, now earlier I told you not to use
10
00:00:40.000 --> 00:00:44.000
mysterious variable names like a and b, but in this particular case
11
00:00:44.000 --> 00:00:48.000
doesn't really matter, because a and b are kind of self explanatory, so we are not
12
00:00:48.000 --> 00:00:52.000
dealing with a complex logic, we don't have to guess what a and b
13
00:00:52.000 --> 00:00:56.000
are as you will see in a second. So in this function,
14
00:00:56.000 --> 00:01:00.000
you want to have some logic, we want to compare the value of a with b, so we're
15
00:01:00.000 --> 00:01:04.000
gonna use an if statement, so if
16
00:01:04.000 --> 00:01:08.000
a is greater than b, we want to return
17
00:01:08.000 --> 00:01:12.000
a. else you want to return b.
18
00:01:12.000 --> 00:01:16.000
That's the simplest implementation, it's not the best way, we're going to optimize
19
00:01:16.000 --> 00:01:20.000
this step by step, but before going any further, let's make sure
20
00:01:20.000 --> 00:01:24.000
that this function actually works. So I'm going to declare
21
00:01:24.000 --> 00:01:28.000
a variable called number and set it to max of
22
00:01:28.000 --> 00:01:32.000
1 and 2. Now let's display number on the
23
00:01:32.000 --> 00:01:36.000
console. Save the changes, so
24
00:01:36.000 --> 00:01:40.000
max is 2, so if the second argument is greater,
25
00:01:40.000 --> 00:01:44.000
our function works, what if the first argument is greater, so
26
00:01:44.000 --> 00:01:48.000
let's change 1 to 3, save, now we see 3,
27
00:01:48.000 --> 00:01:52.000
perfect, what if both arguments are equal,
28
00:01:52.000 --> 00:01:56.000
once again, save the changes, we still see 3, perfect,
29
00:01:56.000 --> 00:02:00.000
now before going any further, did you notice how I called this function
30
00:02:00.000 --> 00:02:04.000
with different arguments, I called it with different test cases.
31
00:02:04.000 --> 00:02:08.000
First I assume that the second argument is greater,
32
00:02:08.000 --> 00:02:12.000
then I assume if the first argument is greater, and finally I assume
33
00:02:12.000 --> 00:02:16.000
that both arguments are equal. So when writing code, when writing functions,
34
00:02:16.000 --> 00:02:20.000
you should test your functions with different possible values, okay?
35
00:02:20.000 --> 00:02:24.000
Now, let's get back to this max function and clean up this code.
36
00:02:24.000 --> 00:02:28.000
But first thing I want to improve here is to remove
37
00:02:28.000 --> 00:02:32.000
this else keyword. Why? Because of 8 is greater
38
00:02:32.000 --> 00:02:36.000
than b, we're going to return a, so we'll jump out of this function.
39
00:02:36.000 --> 00:02:40.000
So none of the code after line 6 will be executed, in fact,
40
00:02:40.000 --> 00:02:44.000
it will never get to this point, so we don't really need the else
41
00:02:44.000 --> 00:02:48.000
keyword. Okay? So if a is greater than b, we'll
42
00:02:48.000 --> 00:02:52.000
return a, otherwise we'll return b. So this is a
43
00:02:52.000 --> 00:02:56.000
cleaner implementation. But hey we can make this even cleaner. So earlier
44
00:02:56.000 --> 00:03:00.000
we learned about the conditional operator. Remember this?
45
00:03:00.000 --> 00:03:04.000
So we add a condition in parenthesis.
46
00:03:04.000 --> 00:03:08.000
Question mark, if this question evaluates to true,
47
00:03:08.000 --> 00:03:12.000
we use 1 value, otherwise we use the other value.
48
00:03:12.000 --> 00:03:16.000
We can rewrite these two lines, using our conditional operator
49
00:03:16.000 --> 00:03:20.000
so what is the condition here? If a is greater than b.
50
00:03:20.000 --> 00:03:24.000
If this condition is true, we want to return a,
51
00:03:24.000 --> 00:03:28.000
otherwise we want to return b. All we need here is
52
00:03:28.000 --> 00:03:32.000
a return statement and terminate this with a semi colon.
53
00:03:32.000 --> 00:03:36.000
So, this line is exactly equivalent to these two lines.
54
00:03:36.000 --> 00:03:40.000
Now, save the changes,
55
00:03:40.000 --> 00:03:44.000
both arguments are equal, you get 3, so far so good,
56
00:03:44.000 --> 00:03:48.000
let's try another test case. I'm going to make the first argument
57
00:03:48.000 --> 00:03:52.000
larger, let's say 5, we get 5,
58
00:03:52.000 --> 00:03:56.000
now let's make the second argument larger, 10, beautiful.
59
00:03:56.000 --> 00:03:59.433
So our function is working.
5254
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.