1
00:00:00,480 --> 00:00:04,530
In the last lesson, we looked at functions that return some sort of value.

2
00:00:05,010 --> 00:00:09,570
We created a function called format_name that returned a string. In this

3
00:00:09,570 --> 00:00:10,080
lesson

4
00:00:10,080 --> 00:00:14,160
let's see what happens when a function has more than one return statement.

5
00:00:14,520 --> 00:00:18,600
When the computer encounters a line that has the word return on it,

6
00:00:19,140 --> 00:00:22,560
then it knows that this line is the end of the function.

7
00:00:23,100 --> 00:00:28,100
If I add a line of code after the return keyword, notice what happens when I run

8
00:00:29,970 --> 00:00:33,630
this code. It doesn't ever get executed.

9
00:00:33,930 --> 00:00:38,930
And this has because the return tells the computer that this is the end of the

10
00:00:39,480 --> 00:00:42,420
function and you should now exit the function.

11
00:00:43,020 --> 00:00:47,130
You can actually have multiple return keywords within the same function,

12
00:00:47,640 --> 00:00:51,300
and you can even have a empty return keyword.

13
00:00:51,360 --> 00:00:55,170
So just the return keyword without anything afterwards.

14
00:00:55,590 --> 00:00:56,790
So for example,

15
00:00:56,820 --> 00:01:01,820
we could check whether if the fname is equal to an empty string or the lname

16
00:01:04,470 --> 00:01:07,620
is equal to an empty string. So this case,

17
00:01:07,620 --> 00:01:12,540
it means that when we called it format name, maybe we didn't give it any inputs.

18
00:01:13,050 --> 00:01:17,430
For example, let's say that instead of just calling the function as it is,

19
00:01:17,670 --> 00:01:20,400
we actually used the input function.

20
00:01:20,580 --> 00:01:25,580
So what is your first name and what is your last name?

21
00:01:28,350 --> 00:01:28,890
Now

22
00:01:28,890 --> 00:01:33,890
what happens is it'll ask us for a input and it will take these two inputs

23
00:01:36,990 --> 00:01:41,990
and then call that function and return the formatted version to be printed.

24
00:01:43,470 --> 00:01:47,820
So now in this case, it's possible that we might have just not given it

25
00:01:47,880 --> 00:01:52,880
a first name or not given it a last name and it probably shouldn't go through

26
00:01:53,730 --> 00:01:57,660
these lines of code. But at the moment, it's still running this function

27
00:01:57,960 --> 00:02:01,650
trying to get the title case from the empty string and then printing it.

28
00:02:02,310 --> 00:02:06,360
And I can prove this to you by just adding an extra word in here.

29
00:02:06,870 --> 00:02:09,479
So again, empty first name, empty, last name,

30
00:02:09,900 --> 00:02:13,590
and then it's print result and then of course nothing.

31
00:02:14,220 --> 00:02:19,220
How can we get it to bypass the rest of the code if the user typed in an empty

32
00:02:19,830 --> 00:02:21,990
first name or last name? Well,

33
00:02:21,990 --> 00:02:25,410
we could use what I mentioned before, an early return.

34
00:02:25,860 --> 00:02:29,400
So we could just write return without anything afterwards

35
00:02:29,730 --> 00:02:32,730
and this is going to escape the function.

36
00:02:33,120 --> 00:02:35,850
So it's basically going to terminate the function early.

37
00:02:36,690 --> 00:02:41,010
Now this time, if I leave an empty first name and last name,

38
00:02:41,250 --> 00:02:46,250
you'll see it prints none because there is no output from this to print.

39
00:02:47,700 --> 00:02:48,600
Now, of course,

40
00:02:48,630 --> 00:02:53,340
it's probably safer to actually return something that tells the developer what's

41
00:02:53,340 --> 00:02:57,240
actually going on so we can give a meaningful message,

42
00:02:57,300 --> 00:03:00,580
like return, something like this.

43
00:03:01,090 --> 00:03:06,090
So this way we can catch the cases when something is not quite right and then

44
00:03:06,400 --> 00:03:11,200
exit our function instead of wasting time for it to continue working on

45
00:03:11,200 --> 00:03:12,820
something that we don't want it to do.

46
00:03:14,140 --> 00:03:17,050
We've covered a number of things in today's lesson

47
00:03:17,530 --> 00:03:22,530
and I want you to combine this with your existing knowledge to tackle the

48
00:03:22,990 --> 00:03:24,850
challenge in the next lesson

49
00:03:25,300 --> 00:03:30,300
to get to grips with this concept of functions with inputs and with outputs.

