1
00:00:00,620 --> 00:00:02,220
Hi guys, and welcome back.

2
00:00:02,220 --> 00:00:04,400
In this video, we're going to talk about function,

3
00:00:04,400 --> 00:00:06,430
arguments and parameters.

4
00:00:06,430 --> 00:00:09,220
That is, how we can give our functions data

5
00:00:09,220 --> 00:00:10,940
so that they can use it.

6
00:00:10,940 --> 00:00:13,460
For example, the print function makes use of this

7
00:00:13,460 --> 00:00:15,920
so that it can accept a string to print out

8
00:00:15,920 --> 00:00:17,060
into the console.

9
00:00:17,060 --> 00:00:20,880
We're going to learn how to do that in our own functions.

10
00:00:20,880 --> 00:00:23,863
Let me define a function that has two parameters.

11
00:00:27,590 --> 00:00:29,800
What I've done here is I have created a function

12
00:00:29,800 --> 00:00:33,960
called add and inside the brackets I've put the names

13
00:00:33,960 --> 00:00:35,530
of its two parameters.

14
00:00:35,530 --> 00:00:38,210
So that's x and y.

15
00:00:38,210 --> 00:00:41,880
Notice that I have put the word pass inside the function

16
00:00:41,880 --> 00:00:45,680
body, and what that means in Python is do nothing.

17
00:00:45,680 --> 00:00:47,900
It is needed, at least something is needed

18
00:00:47,900 --> 00:00:49,960
inside a function because Python expects

19
00:00:49,960 --> 00:00:51,110
the indented block there.

20
00:00:51,110 --> 00:00:52,900
So that's why I've put that there, but it doesn't

21
00:00:52,900 --> 00:00:54,060
mean anything.

22
00:00:54,060 --> 00:00:57,700
We've got our two parameters here which are x and y.

23
00:00:57,700 --> 00:01:00,380
We have two parameters because they are two variable

24
00:01:00,380 --> 00:01:04,040
names separated by a comma and what happens is that when

25
00:01:04,040 --> 00:01:07,420
Python runs the function, when you call the function itself,

26
00:01:07,420 --> 00:01:11,080
these two variables are created and the function

27
00:01:11,080 --> 00:01:13,790
can use them inside the function body.

28
00:01:13,790 --> 00:01:15,400
When you get to the end of the function body,

29
00:01:15,400 --> 00:01:17,550
the variables disappear.

30
00:01:17,550 --> 00:01:20,550
So what are the values of x and y?

31
00:01:20,550 --> 00:01:23,160
Well, that depends on what you give the function

32
00:01:23,160 --> 00:01:24,010
when you call it.

33
00:01:24,010 --> 00:01:28,360
So for example, if you add and put five comma three,

34
00:01:28,360 --> 00:01:31,740
then these here are all called arguments.

35
00:01:31,740 --> 00:01:33,890
These here are called parameters.

36
00:01:33,890 --> 00:01:38,490
So each argument provides a value to one parameter.

37
00:01:38,490 --> 00:01:43,253
Five is the value of x, and three will be the value of y.

38
00:01:44,410 --> 00:01:47,060
Of course our add function doesn't do anything just now

39
00:01:47,060 --> 00:01:49,050
so we are going to make it do something.

40
00:01:49,050 --> 00:01:51,490
We're going to say result is x plus y,

41
00:01:51,490 --> 00:01:53,650
and then we're going to print result.

42
00:01:53,650 --> 00:01:57,270
So now we have it here our function with two parameters,

43
00:01:57,270 --> 00:02:00,280
and then our function call with two arguments.

44
00:02:00,280 --> 00:02:03,730
When the function call happens, these values get placed

45
00:02:03,730 --> 00:02:06,390
into these variables and the function runs.

46
00:02:06,390 --> 00:02:08,760
So here result will be eight

47
00:02:08,760 --> 00:02:09,940
and then we will print that out.

48
00:02:09,940 --> 00:02:12,880
So let me just run that for you, and you can see that eight

49
00:02:12,880 --> 00:02:14,113
comes out down there.

50
00:02:17,180 --> 00:02:18,420
Let's do another example.

51
00:02:18,420 --> 00:02:21,423
Now a function without any parameters.

52
00:02:22,960 --> 00:02:25,990
So if we have a function such as this say hello function

53
00:02:25,990 --> 00:02:27,440
that doesn't have any parameters,

54
00:02:27,440 --> 00:02:29,840
notice that the brackets are empty.

55
00:02:29,840 --> 00:02:31,460
You cannot give it arguments.

56
00:02:31,460 --> 00:02:34,410
So if you type say hello and you pass in Bob,

57
00:02:34,410 --> 00:02:38,250
this will fail in Python because you are passing an argument

58
00:02:38,250 --> 00:02:40,320
to a function that has no parameters.

59
00:02:40,320 --> 00:02:42,850
So if we run this, you'll see that it says

60
00:02:42,850 --> 00:02:45,330
say hello takes zero positional arguments,

61
00:02:45,330 --> 00:02:46,790
but one was given.

62
00:02:46,790 --> 00:02:49,070
We haven't looked at what positional arguments mean yet,

63
00:02:49,070 --> 00:02:51,360
but Bob is a positional argument.

64
00:02:51,360 --> 00:02:53,020
More on that later on.

65
00:02:53,020 --> 00:02:55,700
So we've given this function one positional argument,

66
00:02:55,700 --> 00:02:57,540
but this function doesn't take any.

67
00:02:57,540 --> 00:02:59,080
It doesn't have any parameters.

68
00:02:59,080 --> 00:03:01,430
So that's why the error happens.

69
00:03:01,430 --> 00:03:04,560
Similarly, if we instead decide to pass in a name,

70
00:03:04,560 --> 00:03:06,503
and we do something like this,

71
00:03:08,210 --> 00:03:11,370
now you can call the function with an argument.

72
00:03:11,370 --> 00:03:14,535
So if we run this, you'll see that we get "Hello, Bob"

73
00:03:14,535 --> 00:03:16,940
because this function prints the string "Hello"

74
00:03:16,940 --> 00:03:20,815
with the name variable inside it, but having a parameter

75
00:03:20,815 --> 00:03:23,540
means that you must pass in an argument.

76
00:03:23,540 --> 00:03:27,250
So you cannot call the function without an argument.

77
00:03:27,250 --> 00:03:31,930
If you do, it says that say hello is missing one required

78
00:03:31,930 --> 00:03:34,480
positional argument and then it gives you the name

79
00:03:34,480 --> 00:03:35,850
of the positional argument.

80
00:03:35,850 --> 00:03:38,570
So it's saying that you need to pass in a value

81
00:03:38,570 --> 00:03:41,640
for this name parameter, but you didn't.

82
00:03:41,640 --> 00:03:43,810
So you should.

83
00:03:43,810 --> 00:03:47,030
So when you put Bob in here, we've said that this is a

84
00:03:47,030 --> 00:03:50,170
positional argument, and the reason why it's called

85
00:03:50,170 --> 00:03:53,740
a positional argument is because if you have multiple

86
00:03:53,740 --> 00:03:58,740
arguments such as these two here, and then you call it

87
00:03:58,770 --> 00:04:02,700
like that, the only way Python has of determining

88
00:04:02,700 --> 00:04:06,450
which parameter takes which value is by position.

89
00:04:06,450 --> 00:04:09,990
So Bob is the first argument or in the first position.

90
00:04:09,990 --> 00:04:13,410
So the name parameter will take the value of Bob,

91
00:04:13,410 --> 00:04:15,520
and Smith is the second argument.

92
00:04:15,520 --> 00:04:19,020
So the second parameter will take the value of that.

93
00:04:19,020 --> 00:04:21,070
So these are called positional arguments

94
00:04:21,070 --> 00:04:23,070
because the position they're in will affect

95
00:04:23,070 --> 00:04:26,450
the value of its relevant parameter.

96
00:04:26,450 --> 00:04:30,040
However, Python also has arguments that are not positional.

97
00:04:30,040 --> 00:04:33,760
They are called keyword or named arguments.

98
00:04:33,760 --> 00:04:38,000
For example, you can do surname equal Bob

99
00:04:38,000 --> 00:04:40,285
and name equal Smith.

100
00:04:40,285 --> 00:04:43,710
Now Python will be able to tell that you want

101
00:04:43,710 --> 00:04:47,210
the surname parameter to take the value of Bob

102
00:04:47,210 --> 00:04:51,210
and the named parameter to take the value of Smith.

103
00:04:51,210 --> 00:04:55,450
These here are named or keyword arguments.

104
00:04:55,450 --> 00:04:58,190
Named or keyword arguments are really helpful

105
00:04:58,190 --> 00:05:01,280
because they allow us to very easily determine

106
00:05:01,280 --> 00:05:05,260
when we are reading this line here which argument

107
00:05:05,260 --> 00:05:07,940
is for which parameter, and indeed if we hadn't

108
00:05:07,940 --> 00:05:11,300
come across this function before, it would also tell us

109
00:05:11,300 --> 00:05:13,550
what the function does to some extent.

110
00:05:13,550 --> 00:05:15,540
If you read this line here, you can see that okay,

111
00:05:15,540 --> 00:05:17,680
you're going to say hello and you're taking a name

112
00:05:17,680 --> 00:05:19,720
and a surname, so you're probably going to print that out.

113
00:05:19,720 --> 00:05:22,250
That allows you to make some inferences about what this

114
00:05:22,250 --> 00:05:23,660
code is going to do.

115
00:05:23,660 --> 00:05:26,910
So this can be really helpful and it's very widely

116
00:05:26,910 --> 00:05:27,943
used in Python.

117
00:05:29,540 --> 00:05:32,630
For example, let's say that you've got a divide function

118
00:05:32,630 --> 00:05:36,030
where if the divisor is not zero, then you're going to print

119
00:05:36,030 --> 00:05:37,030
the dividend by the divisor.

120
00:05:37,030 --> 00:05:39,130
Otherwise, you're going to print something

121
00:05:39,130 --> 00:05:42,890
like "You fool," you can't divide something by zero.

122
00:05:42,890 --> 00:05:44,420
So we've got a function here.

123
00:05:44,420 --> 00:05:47,930
Then you can call this function divide with something

124
00:05:47,930 --> 00:05:50,230
like 15 and zero.

125
00:05:50,230 --> 00:05:52,130
So this may be okay.

126
00:05:52,130 --> 00:05:56,140
You can press play and you get back "you fool."

127
00:05:56,140 --> 00:06:00,730
However, it can be clearer if you tell the reader

128
00:06:00,730 --> 00:06:03,860
of this line which value is which.

129
00:06:03,860 --> 00:06:06,010
Now with something like division it may be obvious

130
00:06:06,010 --> 00:06:09,270
that 15 comma zero is going to divide 15 by zero.

131
00:06:09,270 --> 00:06:12,120
That seems reasonable to a reader I think,

132
00:06:12,120 --> 00:06:15,870
but, again, it can be cleared if you say dividend is 15,

133
00:06:15,870 --> 00:06:17,710
and divisor is zero.

134
00:06:17,710 --> 00:06:20,550
Notice that usually in Python we do not put spaces

135
00:06:20,550 --> 00:06:23,460
around this equal sign here when it's being used

136
00:06:23,460 --> 00:06:28,390
to separate a value from its keyword argument.

137
00:06:28,390 --> 00:06:30,610
As well as passing dividend and divisor like this,

138
00:06:30,610 --> 00:06:32,657
you could reverse them; put the divisor here

139
00:06:32,657 --> 00:06:34,300
and the dividend there and that would be fine,

140
00:06:34,300 --> 00:06:36,480
although maybe a little more bit confusing,

141
00:06:36,480 --> 00:06:40,420
but also not all arguments have to be keyword arguments.

142
00:06:40,420 --> 00:06:42,440
You can have some positional arguments

143
00:06:42,440 --> 00:06:46,230
and then some keyword arguments, but that's exactly it.

144
00:06:46,230 --> 00:06:48,480
The positional arguments have to go first

145
00:06:48,480 --> 00:06:50,770
and the keyword arguments have to go later.

146
00:06:50,770 --> 00:06:54,680
If you do dividend equal 15 and then you skip the keyword

147
00:06:54,680 --> 00:06:57,010
argument there, you're going to get an error and Python

148
00:06:57,010 --> 00:06:59,460
is going to tell you that a positional argument follows

149
00:06:59,460 --> 00:07:02,540
a keyword argument and that is illegal in Python.

150
00:07:02,540 --> 00:07:04,650
So the positional arguments have to go first,

151
00:07:04,650 --> 00:07:06,133
keyword arguments go later.

152
00:07:07,330 --> 00:07:10,410
I recommend that you use keyword arguments

153
00:07:10,410 --> 00:07:12,540
whenever possible just because you're going to make

154
00:07:12,540 --> 00:07:14,630
your code a little bit easier to read,

155
00:07:14,630 --> 00:07:17,300
unless it's really obvious what's going on.

156
00:07:17,300 --> 00:07:19,925
So for example, in a divide function, maybe you don't need

157
00:07:19,925 --> 00:07:21,780
the keyword arguments.

158
00:07:21,780 --> 00:07:24,220
Similarly, in a say hello function that just takes

159
00:07:24,220 --> 00:07:26,700
a person's name, you probably don't need keyword arguments

160
00:07:26,700 --> 00:07:29,860
either, but as soon as you start doing more complicated

161
00:07:29,860 --> 00:07:32,710
things or functions that are maybe not as obvious,

162
00:07:32,710 --> 00:07:34,490
using keyword arguments is really going to help.

163
00:07:34,490 --> 00:07:36,630
So I recommend that you use them.

164
00:07:36,630 --> 00:07:37,640
That's it for this video though.

165
00:07:37,640 --> 00:07:39,040
Thank you for joining me in this one,

166
00:07:39,040 --> 00:07:40,390
and I'll see you next time.

