WEBVTT

0
00:00.170 --> 00:07.010
In the last lesson, we saw a very simple form of a function that allows for an input.

1
00:07.040 --> 00:13.190
Now in this lesson, I want to take it even further, and I want to create a function that allows for

2
00:13.190 --> 00:14.780
multiple inputs.

3
00:15.140 --> 00:19.520
So let's comment out the previous line of code and let's create a new comment.

4
00:19.520 --> 00:21.740
Functions with more than one input.

5
00:22.610 --> 00:27.590
And I'm going to create a new function called greet_with().

6
00:27.620 --> 00:31.760
And in this case it's going to take two parameters,

7
00:31.760 --> 00:35.480
it's going to be name and location.

8
00:35.480 --> 00:39.440
If you remember this is how we added a parameter previously.

9
00:39.440 --> 00:46.220
Now as a challenge, I want you to quickly think about how you might add two parameters, one called name

10
00:46.220 --> 00:50.450
and one called location into this function declaration.

11
00:50.450 --> 00:53.880
Pause the video, have a brief think and then we'll go through it together.

12
00:54.900 --> 00:55.380
All right.

13
00:55.380 --> 00:59.940
So we know that if we wanted to add one parameter name then this is how we would do it.

14
00:59.940 --> 01:02.970
We would just add it inside the parentheses.

15
01:03.180 --> 01:10.080
Now if we want to have more than one parameter, all we have to do is just add a comma and then add

16
01:10.080 --> 01:13.980
the second parameter, which we said was going to be called location.

17
01:13.980 --> 01:20.100
So now this particular function is going to take two inputs the name and the location.

18
01:20.100 --> 01:27.390
And then inside this function, we're going to use the name to print something like, "Hello name."

19
01:27.660 --> 01:34.830
And then we're going to print and ask them what is it like in their particular location.

20
01:35.160 --> 01:40.650
See if you can modify this to use the actual parameters and to replace them with print statements,

21
01:40.650 --> 01:44.920
so that we use these parameters inside our function.

22
01:44.950 --> 01:46.930
Pause the video now and give that a go.

23
01:48.100 --> 01:48.610
All right.

24
01:48.610 --> 01:51.670
So essentially, we want to create a print statement here,

25
01:51.670 --> 01:54.340
and print statements print strings.

26
01:54.340 --> 01:57.550
So we have to add some quotation marks around that text.

27
01:57.550 --> 02:07.120
And then finally, I'm going to use an f-string to replace this parameter name inside this string so that

28
02:07.120 --> 02:13.450
the data that gets passed in gets replaced here, and it says "Hello..." whatever their name is.

29
02:13.450 --> 02:16.540
And then I'm just going to do the same thing over here.

30
02:19.840 --> 02:26.050
And notice that you can simply highlight a word, or highlight a sentence, and then use the open curly

31
02:26.050 --> 02:31.160
brace to actually add the brace around both sides of the word.

32
02:31.160 --> 02:34.880
And you'll notice that I did the same thing with the quotation mark.

33
02:34.880 --> 02:38.390
So highlight the whole sentence, and then hit the double quote key,

34
02:38.390 --> 02:42.830
and it will keep adding quotes around both sides of your highlight.

35
02:43.520 --> 02:45.590
But I only actually need one.

36
02:45.590 --> 02:49.310
So that is our function completed.

37
02:49.310 --> 02:55.550
And now it means that I can call this function by calling greet_with() and it's going to prompt me to

38
02:55.550 --> 02:58.220
add both of these inputs.

39
02:58.220 --> 03:02.240
And the first piece of data is going to be the name,

40
03:02.240 --> 03:04.850
and let's put Jack Bauer.

41
03:05.990 --> 03:11.000
And then we can add the second piece of data and separate it with a comma.

42
03:11.000 --> 03:13.790
So let's say that Jack Bauer is nowhere.

43
03:14.270 --> 03:20.540
Now if we go ahead and run this code then you'll see that it's going to print "Hello" and replace name

44
03:20.540 --> 03:21.650
with "Jack Bauer",

45
03:21.650 --> 03:24.440
and then, "What is it like in Nowhere"

46
03:24.440 --> 03:27.380
So Nowhere gets replaced with this location.

47
03:27.380 --> 03:34.910
So that means that you can now put in as many pieces of inputs as you want, and modify the functionality

48
03:34.910 --> 03:40.310
of your function to make your function do different things each time.

49
03:41.330 --> 03:47.830
Now here's a question what happens if I call the same function greet_with(),

50
03:47.830 --> 03:51.940
but I switch the order of the data that I give it.

51
03:51.940 --> 03:55.750
So let's say the first piece of data I give it is "Nowhere,"

52
03:55.960 --> 04:00.160
and then the second piece of data is "Jack Bauer".

53
04:00.160 --> 04:03.820
So we've just switched the order of these pieces of data.

54
04:03.910 --> 04:06.460
Now what do you expect to happen?

55
04:06.460 --> 04:12.120
Pause for a moment and have a think about what you expect to be printed in here and then continue.

56
04:13.590 --> 04:14.070
All right.

57
04:14.070 --> 04:19.800
So let's click run and you can see that it's now complete nonsense.

58
04:19.800 --> 04:20.160
"Hello

59
04:20.160 --> 04:20.730
Nowhere

60
04:20.730 --> 04:22.350
What is it like in Jack Bauer."

61
04:22.350 --> 04:29.460
And what's actually happened here is it takes the position of the data, looks at both of these arguments,

62
04:29.460 --> 04:32.740
and the first argument gets assigned to the first parameter,

63
04:32.770 --> 04:35.740
the second argument gets assigned to the second parameter.

64
04:35.770 --> 04:42.040
So in this case, when it's actually gone in here name is now equal to "Nowhere," which is why this line

65
04:42.040 --> 04:43.000
printed this,

66
04:43.000 --> 04:49.180
and "Jack Bauer" is now assigned to location, which is why it printed the second line like so.

67
04:49.900 --> 04:56.770
And in Python programming, this is called a Positional Argument, because when we call the function,

68
04:56.770 --> 05:04.300
we haven't specified anywhere which particular parameter we want to associate these pieces of data with.

69
05:04.300 --> 05:06.940
So it's just gone and looked at the position.

70
05:06.940 --> 05:12.280
Now this is the default way of calling functions, because on one hand, when you're typing out the

71
05:12.280 --> 05:17.200
code, you get the hints here as to which piece of data you need to enter.

72
05:17.200 --> 05:22.610
But also you can refer to the function and look at the order of the parameters.

73
05:23.210 --> 05:31.310
Even if we had more inputs, let's say in this case we had a, b and c, and we put the argument 1,

74
05:31.310 --> 05:32.510
2 and 3,

75
05:32.510 --> 05:41.360
Then it means that our variables that gets created will be a = 1, b = 2, and c = 3

76
05:41.360 --> 05:42.140
.

77
05:43.940 --> 05:48.920
Now if we switch around the order of the arguments in the function call,

78
05:48.920 --> 05:53.570
now what will happen is a is going to be equal to the first argument.

79
05:53.570 --> 05:59.870
So a is now equal to three, b is equal to the second argument,  and c is equal to the third argument.

80
05:59.870 --> 06:03.740
So it might be doing slightly unpredictable things in here.

81
06:03.740 --> 06:09.020
So whenever you're creating code and you're using these positional arguments and you're just inserting

82
06:09.020 --> 06:13.280
the data one by one like this, and it does something completely unexpected,

83
06:13.280 --> 06:20.030
then be sure to check your positioning and to make sure that it matches with the position of the parameters.

84
06:21.140 --> 06:27.980
Now what if you wanted to be more clear when you actually call the function so you don't ever encounter

85
06:27.980 --> 06:29.030
this problem?

86
06:29.150 --> 06:33.230
Well, you could use something called Keyword Arguments instead.

87
06:33.230 --> 06:40.290
So now instead of just adding the arguments into the function call like this, we can actually add each

88
06:40.290 --> 06:46.590
of the parameter names and an equal sign to say that the first parameter a = 1,

89
06:46.590 --> 06:49.980
b = 2, and c = 3.

90
06:49.980 --> 06:56.250
And now when we actually change the order around, it doesn't matter how we order it, it's still going

91
06:56.250 --> 06:58.630
to abide by these bindings.

92
06:58.630 --> 07:02.410
So c will still be 3and a will still be 1.

93
07:03.700 --> 07:09.760
As a challenge, I want you to take this previous function, greet_with() name and location,

94
07:09.760 --> 07:13.090
and I want you to call this function down here,

95
07:13.090 --> 07:18.910
but this time, instead of using positional arguments I want you to use keyword arguments.

96
07:18.910 --> 07:22.030
So pause the video and try and give that a go.

97
07:24.580 --> 07:25.120
All right.

98
07:25.120 --> 07:27.640
So when we call the function we still use the name.

99
07:27.640 --> 07:28.900
So it's greet_with().

100
07:28.900 --> 07:31.870
So everything up to the first parentheses is the name.

101
07:31.870 --> 07:36.010
And then we add in each of these parameter names,

102
07:36.010 --> 07:38.410
and then we add an equal sign,

103
07:38.410 --> 07:41.200
and finally we give it the actual value.

104
07:41.200 --> 07:42.640
So let's say, "Angela".

105
07:42.640 --> 07:47.980
And then the location is going to be equal to "London".

106
07:48.550 --> 07:52.600
Now, when I hit Run, you can see it does pretty much the same as before,

107
07:52.600 --> 07:55.930
it puts Angela into name, London into location.

108
07:55.930 --> 08:00.400
But this time if I switch the order around, it no longer matters.

109
08:00.400 --> 08:06.970
And when I hit Run again, you can see it does exactly the same thing, because it now knows which argument

110
08:06.970 --> 08:09.730
is associated with which parameter.

111
08:10.120 --> 08:15.470
So this can make your code less error-prone, but it does make each line of code longer.

112
08:15.500 --> 08:22.550
I recommend using your judgement to figure out when you want to use which type of argument, and depending

113
08:22.550 --> 08:24.890
on the need, you can pick between these two.

114
08:25.910 --> 08:32.120
Now in the next lesson, I've got a coding exercise for you to put into practice everything that you've

115
08:32.120 --> 08:33.230
learned so far.

116
08:33.260 --> 08:36.290
Head over there when you're ready and let's give it a go.