WEBVTT

0
00:00.230 --> 00:04.700
In the last lesson, we looked at functions that return some sort of value,

1
00:04.730 --> 00:08.900
we created a function called format_name that returned a string,

2
00:08.930 --> 00:14.270
in this lesson, let's see what happens when a function has more than one return statement.

3
00:14.270 --> 00:21.170
When the computer encounters a line that has the word return on it, then it knows that this line is

4
00:21.170 --> 00:22.790
the end of the function.

5
00:22.970 --> 00:30.880
If I add a line of code after the return keyword, notice what happens when I run this code.

6
00:30.880 --> 00:38.980
It doesn't ever get executed, and this is because the return tells the computer that this is the end

7
00:38.980 --> 00:42.730
of the function, and you should now exit the function.

8
00:42.760 --> 00:50.140
You can actually have multiple return keywords within the same function, and you can even have an empty

9
00:50.140 --> 00:51.250
return keyword.

10
00:51.250 --> 00:55.330
So just the return keyword without anything afterwards.

11
00:55.330 --> 01:05.070
So for example, we could check whether if the f_name is equal to an empty string or the l_name is equal

12
01:05.070 --> 01:06.570
to an empty string.

13
01:06.570 --> 01:12.810
So in this case, it means that when we called format_name(), maybe we didn't give it any inputs.

14
01:12.810 --> 01:19.770
For example, let's say that instead of just calling the function as it is, we actually used the input()

15
01:19.770 --> 01:20.430
function.

16
01:20.430 --> 01:27.620
So, "What is your first name?" and "What is your last name?"

17
01:28.130 --> 01:36.770
Now what happens is it'll ask us for an input and it will take these two inputs,

18
01:36.770 --> 01:42.530
and then call that function and return the formatted version to be printed.

19
01:43.280 --> 01:50.000
So now in this case, it's possible that we might have just not given it a first name or not given it

20
01:50.000 --> 01:50.990
a last name,

21
01:50.990 --> 01:57.080
And it probably shouldn't go through these lines of code, but at the moment it's still running this

22
01:57.080 --> 02:01.790
function, trying to get the title case from the empty string and then printing it.

23
02:02.120 --> 02:06.560
And I can prove this to you by just adding an extra word in here.

24
02:06.560 --> 02:09.620
So again, empty first name, empty last name,

25
02:09.620 --> 02:11.840
and then it print's result,

26
02:11.840 --> 02:13.840
and then, of course, nothing.

27
02:14.020 --> 02:17.050
How can we get it to bypass the rest of the code?

28
02:17.050 --> 02:21.280
If the user typed in an empty first name or last name?

29
02:21.310 --> 02:28.720
Well, we could use what I mentioned before an early return so we could just write return without anything

30
02:28.720 --> 02:29.500
afterwards,

31
02:29.500 --> 02:32.860
and this is going to escape the function.

32
02:32.860 --> 02:36.010
So it's basically going to terminate the function early.

33
02:36.580 --> 02:43.530
Now this time if I leave an empty first name and last name, you'll see it prints none because there

34
02:43.530 --> 02:46.710
is no output from this to print.

35
02:47.400 --> 02:53.610
Now, of course, it's probably safer to actually return something that tells the developer what's actually

36
02:53.610 --> 03:00.660
going on so we can give a meaningful message like return something like this.

37
03:00.990 --> 03:08.120
So this way we can catch the cases when something is not quite right, and then exit our function instead

38
03:08.120 --> 03:13.070
of wasting time for it to continue working on something that we don't want it to do.

39
03:13.970 --> 03:20.690
We've covered a number of things in today's lesson, and I want you to combine this with your existing

40
03:20.690 --> 03:25.010
knowledge to tackle the challenge in the next lesson

41
03:25.010 --> 03:30.980
to get to grips with this concept of functions with inputs and with outputs.