1
00:00:01,200 --> 00:00:08,220
In this lecture, I show you how to use functions, which is an excellent way by which you can improve

2
00:00:08,280 --> 00:00:14,300
your organization of the program, will designed functions make the program easier to use?

3
00:00:14,310 --> 00:00:14,670
Of course.

4
00:00:14,670 --> 00:00:15,000
Right.

5
00:00:15,000 --> 00:00:21,000
And to maintain two functions allow you to split the logic of your problem into smaller chunks.

6
00:00:21,270 --> 00:00:25,740
And we'll be using quite a few of them in the sections that follow.

7
00:00:26,790 --> 00:00:31,760
Let me show you a few simple examples to help you get started with functions.

8
00:00:32,310 --> 00:00:36,320
So let's say that you would like to add two numbers together.

9
00:00:36,780 --> 00:00:42,730
We've already seen this type of operation many, many times earlier.

10
00:00:43,080 --> 00:00:49,110
We also learned about fireboats so we can have things such as number one and number two.

11
00:00:49,830 --> 00:00:57,640
Let's give it a couple of numbers and then you can say no one has two and you get an answer back.

12
00:00:57,750 --> 00:00:59,970
Now we're setting the parameters of a program.

13
00:01:00,120 --> 00:01:06,900
Another thing that we can do is the next level of parameterization is to create a simple function that

14
00:01:06,900 --> 00:01:10,490
has a single job, and that is to add two numbers.

15
00:01:11,070 --> 00:01:19,470
So to create a function, we use the death or the reserved Caywood and then we provide a name for our

16
00:01:19,470 --> 00:01:21,480
function, let's call it addition.

17
00:01:21,960 --> 00:01:28,980
And then inside parentheses, we provide one of zero actually or more parameters.

18
00:01:29,400 --> 00:01:36,330
You could write a function that doesn't take any parameters, so it would be defined with an empty set

19
00:01:36,330 --> 00:01:36,940
of parameters.

20
00:01:37,290 --> 00:01:40,850
In our case, we need two numbers in order to have an addition.

21
00:01:41,160 --> 00:01:48,390
So one can say in one command number two for the second parameter and to finish the definition, we

22
00:01:48,390 --> 00:02:00,630
use the colon symbol inside the block of the new function to use a simple print statement which just

23
00:02:00,630 --> 00:02:04,010
adds the two numbers.

24
00:02:04,950 --> 00:02:13,320
Just like that, all right, now we have a function defined and to use it, we simply call it name,

25
00:02:13,560 --> 00:02:15,110
I just press the case.

26
00:02:15,110 --> 00:02:20,760
So code completion worked and I need to pass in the appropriate number of parameters.

27
00:02:21,090 --> 00:02:24,270
If I do pass anything, I'm getting an error message.

28
00:02:24,270 --> 00:02:29,040
And you can see that Python knows that we need to pass in two parameters.

29
00:02:29,940 --> 00:02:30,480
Try again.

30
00:02:30,870 --> 00:02:36,180
This time I'm going to pass number two and number four for these two parameters.

31
00:02:36,690 --> 00:02:39,380
And there is the answer comes back.

32
00:02:40,080 --> 00:02:47,670
So I called the addition function, which performed the calculation and then printed out the result.

33
00:02:48,310 --> 00:02:59,610
Let's do a little variation of the functional scenarios that I use, the print function and inside the

34
00:02:59,610 --> 00:03:02,160
print function, I perform the calculation.

35
00:03:02,370 --> 00:03:11,850
But very often what you'll be doing is that your function will contain several steps of processing and

36
00:03:11,850 --> 00:03:21,120
then in the end you will want to return the result of those several steps of processing to whoever made

37
00:03:21,120 --> 00:03:22,560
the call for the function.

38
00:03:22,830 --> 00:03:28,500
And that means that we need to introduce at this point the return key word.

39
00:03:28,950 --> 00:03:35,040
So I'm going to redefine my additional function like this.

40
00:03:35,340 --> 00:03:41,160
I'm still expecting to receive the two parameters and there's my colon symbol.

41
00:03:41,520 --> 00:03:50,970
Now inside, I'm going to create a new variable called result, and in it I'm going to store the result

42
00:03:50,970 --> 00:03:53,660
of the addition between the number one and the number two.

43
00:03:54,690 --> 00:04:03,390
And then once the result is calculated and ready, I'm going to use the return key word to send it back

44
00:04:03,390 --> 00:04:06,840
to whoever made the call to the addition function.

45
00:04:07,350 --> 00:04:10,660
I'm just saying return result and that's it.

46
00:04:11,550 --> 00:04:19,360
Now, when I call the function, just call it without the print statement, I just kind of hit the up

47
00:04:19,380 --> 00:04:20,700
error actually to go.

48
00:04:21,390 --> 00:04:21,900
That's work.

49
00:04:22,530 --> 00:04:26,130
So I'm just going to type in the edition.

50
00:04:28,890 --> 00:04:32,600
Name of the function and pass a couple of numbers to say two or three.

51
00:04:33,370 --> 00:04:36,040
You see that the result comes back is five.

52
00:04:36,320 --> 00:04:40,950
Now, the result was because of the function call.

53
00:04:40,970 --> 00:04:43,780
It was not printed by the function itself.

54
00:04:44,780 --> 00:04:50,390
The result was sent back to the caller, which was this line of code right here.

55
00:04:50,630 --> 00:04:53,380
And it was printed out because I'm working on the show.

56
00:04:53,690 --> 00:04:59,420
If you were doing this in such a program, you would need to use the print command so it would look

57
00:04:59,420 --> 00:05:00,080
like this.

58
00:05:04,430 --> 00:05:07,310
And the results would come back like that.

59
00:05:07,340 --> 00:05:13,070
So this is the result of the command coming back with the value that was calculated by the addition

60
00:05:13,070 --> 00:05:18,620
function and then sent back to the print function by the return statement.

61
00:05:19,730 --> 00:05:25,700
Another thing that you can do with functions is instead of using anonymous parameters, you can use

62
00:05:25,700 --> 00:05:27,020
named parameters.

63
00:05:27,020 --> 00:05:35,630
And that also gives you the opportunity to set defaults and also to make it possible for the user to

64
00:05:35,630 --> 00:05:39,380
not provide a parameter if a default is present.

65
00:05:39,950 --> 00:05:41,620
So I'll show you an example.

66
00:05:42,940 --> 00:05:52,390
We're going to redefine addiction, but this time I'm going to have numb with a default value.

67
00:05:52,780 --> 00:05:58,800
Now I've got a name for the perimeter and I've got a different value and I'm going to do a good story.

68
00:05:58,960 --> 00:05:59,950
They should have been number one.

69
00:06:01,940 --> 00:06:04,590
And I'm also going to have a number two.

70
00:06:04,750 --> 00:06:09,060
And again, I'm going to have one as the default for number two.

71
00:06:11,260 --> 00:06:24,330
And then inside I'm going to create the result variable of thing number one, two and three can result.

72
00:06:25,210 --> 00:06:25,780
All right.

73
00:06:25,960 --> 00:06:28,510
Now I'm going to.

74
00:06:30,600 --> 00:06:38,070
Copy the print command, but I'm not going to pass any parameters and see what happens.

75
00:06:39,100 --> 00:06:40,180
OK, it worked.

76
00:06:40,840 --> 00:06:50,650
So the result is two, which is what one plus one gives us, it means that the addition function did

77
00:06:50,650 --> 00:06:54,330
not find any use of parameters, so it just went with its default.

78
00:06:55,990 --> 00:07:01,330
Another thing that you can do now, because we have defaults and we've got named Parramatta's, is that

79
00:07:01,330 --> 00:07:03,400
we can go and do something like this.

80
00:07:04,100 --> 00:07:07,870
And number two equals three.

81
00:07:08,630 --> 00:07:13,400
Notice that I have not included a parameter for number one and I'm just using this notation.

82
00:07:13,400 --> 00:07:17,790
Number two equals three instead of just simply passing a number.

83
00:07:18,880 --> 00:07:19,210
Right.

84
00:07:19,240 --> 00:07:24,430
So let's go for two equals three, and that will give us four.

85
00:07:24,850 --> 00:07:30,190
So addition, does the default one for number one.

86
00:07:30,190 --> 00:07:35,200
And then it used my user parameter for name to which was three.

87
00:07:36,840 --> 00:07:43,140
Of course, you can still override the defaults by.

88
00:07:44,740 --> 00:07:49,210
Providing both addition sanction parameters like this.

89
00:07:50,270 --> 00:07:53,720
OK, so that's about it with an introduction to functions.

90
00:07:54,230 --> 00:07:59,900
Now you are ready to move on to the next lecture where we talk about classes and objects.
