1
00:00:00,310 --> 00:00:01,420
In the last lesson,

2
00:00:01,450 --> 00:00:06,450
we saw a very simple form of a function that allows for an input.

3
00:00:07,360 --> 00:00:08,470
Now in this lesson,

4
00:00:08,500 --> 00:00:13,150
I want to take it even further and I want to create a function that allows for

5
00:00:13,150 --> 00:00:14,650
multiple inputs.

6
00:00:15,370 --> 00:00:19,390
So let's comment out of the previous line of code and let's create a new comment,

7
00:00:19,720 --> 00:00:21,490
functions with more than one input.

8
00:00:22,840 --> 00:00:27,310
And I'm going to create a new function called greet_with.

9
00:00:28,000 --> 00:00:31,660
And in this case, it's going to take two parameters.

10
00:00:31,960 --> 00:00:36,460
It's going to be name and location. If you remember,

11
00:00:36,460 --> 00:00:40,840
this is how we added a parameter previously. Now, as a challenge,

12
00:00:40,870 --> 00:00:45,310
I want you to quickly think about how you might add two parameters,

13
00:00:45,340 --> 00:00:49,540
one's called name and one's called location, into this function

14
00:00:49,540 --> 00:00:52,450
declaration. Pause the video, have a brief think,

15
00:00:52,600 --> 00:00:55,420
and then we'll go through it together. All right.

16
00:00:55,420 --> 00:00:58,600
So we know that if we wanted to add one parameter name,

17
00:00:58,660 --> 00:01:02,740
then this is how we would do it. We would just add it inside the parentheses.

18
00:01:03,490 --> 00:01:06,070
Now, if we wanted to have more than one parameter,

19
00:01:06,520 --> 00:01:11,520
all we have to do is just add a comma and then add the second parameter which we

20
00:01:11,860 --> 00:01:13,720
said was going to be called location.

21
00:01:14,290 --> 00:01:18,190
So now this particular function is going to take two inputs,

22
00:01:18,190 --> 00:01:22,360
the name and the location. And then inside this function,

23
00:01:22,390 --> 00:01:27,130
we're going to use the name to print something like hello name,

24
00:01:28,000 --> 00:01:29,980
and then we're going to print and ask them,

25
00:01:30,400 --> 00:01:34,540
what is it like in their particular location.

26
00:01:35,140 --> 00:01:39,850
See if you can modify this to use the actual parameters and to replace them with

27
00:01:39,850 --> 00:01:44,800
print statements so that we use these parameters inside of our function.

28
00:01:45,100 --> 00:01:48,640
Pause the video now and give that a go. All right.

29
00:01:48,640 --> 00:01:53,260
So essentially we want to create a print statement here and print statements

30
00:01:53,320 --> 00:01:57,400
print strings so we have to add some quotation marks around that text.

31
00:01:57,880 --> 00:01:58,810
And then finally,

32
00:01:58,810 --> 00:02:03,810
I'm going to use a fstring to replace this parameter name inside this string so

33
00:02:06,850 --> 00:02:11,350
that the data that gets passed in gets replaced here, and it says,

34
00:02:11,380 --> 00:02:13,210
hello, whatever their name is.

35
00:02:13,870 --> 00:02:16,270
And then I'm just going to do the same thing over here

36
00:02:20,230 --> 00:02:23,890
and notice that with replit and a lot of other text editors,

37
00:02:23,890 --> 00:02:26,860
you can simply highlight a word or highlight or a sentence,

38
00:02:27,100 --> 00:02:32,100
and then use the open curly brace to actually add brace around both sides of the

39
00:02:33,340 --> 00:02:37,750
word. And you'll notice that I did the same thing with the quotation marks.

40
00:02:37,750 --> 00:02:41,170
So it highlight the whole sentence and then hit the double-quote key

41
00:02:41,440 --> 00:02:45,400
and it will keep adding quotes around both sides of your highlight.

42
00:02:46,570 --> 00:02:48,160
But I only actually need one.

43
00:02:48,700 --> 00:02:51,880
So that is our function completed.

44
00:02:52,480 --> 00:02:56,230
And now it means I can call this function by calling greet_with,

45
00:02:56,710 --> 00:03:00,880
and it's going to prompt me to add both of these inputs.

46
00:03:01,390 --> 00:03:04,870
And the first piece of data is going to be the name

47
00:03:04,990 --> 00:03:09,610
and you can see it's giving you a hint by underlining the name of the parameter

48
00:03:09,610 --> 00:03:12,550
here. And let's put Jack Bauer

49
00:03:14,170 --> 00:03:18,790
and then we can add the second piece of data and separate it with a comma.

50
00:03:19,270 --> 00:03:23,020
And now you can see this underline has moved to the location parameter.

51
00:03:23,080 --> 00:03:25,810
So again, a hint as to what kind of data it wants.

52
00:03:26,260 --> 00:03:29,950
So let's say that Jack Bauer is nowhere. Now,

53
00:03:29,950 --> 00:03:31,750
if we go ahead and run this code,

54
00:03:31,840 --> 00:03:36,550
then you'll see that it's going to print hello and replace name with Jack Bauer.

55
00:03:36,910 --> 00:03:40,120
And then what is it like in nowhere? So no

56
00:03:40,120 --> 00:03:42,100
where gets replaced with this location.

57
00:03:42,700 --> 00:03:47,700
So that means that you can now put in as many pieces of inputs as you want and

58
00:03:48,070 --> 00:03:53,070
modify the functionality of your function to make your function do different

59
00:03:53,980 --> 00:03:57,850
things each time. Now here's a question.

60
00:03:58,390 --> 00:04:02,620
What happens if I call the same function greet_with,

61
00:04:03,070 --> 00:04:06,760
but I switch the order of the data that I give it.

62
00:04:07,210 --> 00:04:10,660
So let's say the first piece of data I give it is nowhere,

63
00:04:11,320 --> 00:04:14,980
and then the second piece of data is Jack Bauer.

64
00:04:15,370 --> 00:04:19,690
So we've just switched the order of these pieces of data. Now,

65
00:04:19,750 --> 00:04:21,279
what do you expect to happen?

66
00:04:21,640 --> 00:04:25,450
Pause for a moment and have a think about what you expect to be printed in here

67
00:04:25,990 --> 00:04:29,080
and then continue. All right,

68
00:04:29,080 --> 00:04:34,080
so let's click run and you can see that it's now complete nonsense.

69
00:04:34,840 --> 00:04:39,430
Hello, nowhere. What is it like in Jack Bauer? And what actually happened here

70
00:04:39,640 --> 00:04:44,350
is it takes the position of the data, looks at both of these arguments,

71
00:04:44,710 --> 00:04:47,620
and the first argument gets assigned to the first parameter

72
00:04:47,950 --> 00:04:51,940
the second argument gets assigned at the second parameter. So in this case,

73
00:04:51,970 --> 00:04:55,960
when it's actually gone in here, name is now equal to nowhere,

74
00:04:55,990 --> 00:05:00,990
which is why this line printed thi.sAand Jack Bauer is now assigned to location

75
00:05:01,570 --> 00:05:06,520
which is why it printed this second line like so. And in Python programming,

76
00:05:06,550 --> 00:05:11,550
this is called a positional argument because when we call the function,

77
00:05:11,950 --> 00:05:16,950
we haven't specified anywhere which particular parameter we want to associate

78
00:05:17,800 --> 00:05:21,910
these pieces of data with. So it's just gone and looked at the position.

79
00:05:22,030 --> 00:05:26,290
Now this is the default way of calling functions, because on one hand,

80
00:05:26,320 --> 00:05:27,640
when you're typing out the code,

81
00:05:27,670 --> 00:05:32,020
you get the hints here as to which piece of data you need to enter.

82
00:05:32,470 --> 00:05:37,360
But also you can refer to the function and look at the order of the parameters.

83
00:05:38,260 --> 00:05:42,610
Even if we had more inputs, let's say in this case, we had a,

84
00:05:42,610 --> 00:05:47,380
b and c, and we put the arguments one, two and three,

85
00:05:47,740 --> 00:05:51,310
then it means that our variables that gets created

86
00:05:51,310 --> 00:05:52,840
will be a equals one,

87
00:05:53,260 --> 00:05:56,950
b equals two and c equals three.

88
00:05:59,060 --> 00:06:03,590
Now, if we switch around the order of the arguments in the function call,

89
00:06:04,250 --> 00:06:08,600
now what will happen is a is going to be equal to the first argument.

90
00:06:08,600 --> 00:06:13,600
So a is now equal to three, b as equal to the second argument and c is equal to

91
00:06:13,640 --> 00:06:18,560
the third argument. So it might be doing slightly unpredictable things in here.

92
00:06:19,010 --> 00:06:22,730
So whenever you're creating code and you're using these positional arguments,

93
00:06:23,030 --> 00:06:25,700
and you're just inserting the data one by one like this,

94
00:06:26,000 --> 00:06:28,250
and it does something completely unexpected,

95
00:06:28,340 --> 00:06:32,990
then be sure to check your positioning and to make sure that it matches with the

96
00:06:32,990 --> 00:06:36,770
position of the parameters. Now,

97
00:06:36,800 --> 00:06:41,180
what if you wanted to be more clear when you actually call the function

98
00:06:41,210 --> 00:06:44,690
so you don't ever encounter this problem? Well,

99
00:06:44,690 --> 00:06:49,340
you could use something called keyword arguments instead. So now,

100
00:06:49,370 --> 00:06:53,300
instead of just adding the arguments into the function call like this,

101
00:06:53,690 --> 00:06:58,690
we can actually add each of the parameter names and an equal sign to say that

102
00:06:58,880 --> 00:07:00,260
the first parameter a

103
00:07:00,260 --> 00:07:04,730
should be equal to one, b should be equal to two and c equals to three.

104
00:07:05,300 --> 00:07:07,940
And now when we actually change the order around,

105
00:07:07,970 --> 00:07:09,830
it doesn't matter how we order it,

106
00:07:10,160 --> 00:07:14,540
it still going to abide by these bindings. So c

107
00:07:14,540 --> 00:07:17,180
will still be three and a will still be one. 

108
00:07:18,830 --> 00:07:22,160
As a challenge, I want you to take this previous function,

109
00:07:22,640 --> 00:07:24,710
greet_with_name and location,

110
00:07:24,950 --> 00:07:29,120
and I want you to call this function down here, but this time,

111
00:07:29,150 --> 00:07:33,650
instead of using positional arguments, I want you to use keyword arguments.

112
00:07:34,160 --> 00:07:36,860
So pause the video and try and give that a go.

113
00:07:39,770 --> 00:07:42,620
All right. So when we call the function, we still use the name.

114
00:07:42,650 --> 00:07:46,610
So it's greet_with, so everything up to the first parentheses is the name.

115
00:07:47,150 --> 00:07:50,750
And then we add in each of these parameter names,

116
00:07:51,410 --> 00:07:56,180
and then we add an equal sign and finally, we give it the actual value.

117
00:07:56,240 --> 00:07:57,620
So let's say Angela,

118
00:07:57,800 --> 00:08:02,690
and then the location is going to be equal to London.

119
00:08:03,710 --> 00:08:05,030
Now, when I hit run,

120
00:08:05,090 --> 00:08:09,200
you can see it does pretty much the same as before. It puts Angela into the name,

121
00:08:09,260 --> 00:08:13,940
London into location. But this time if I switch the order around it

122
00:08:14,000 --> 00:08:17,330
no longer matters. And when I hit run again,

123
00:08:17,360 --> 00:08:19,550
you can see it does exactly the same thing,

124
00:08:19,970 --> 00:08:24,470
because it now knows which argument is associated with which parameter.

125
00:08:25,400 --> 00:08:27,500
So this can make your code less error prone

126
00:08:27,770 --> 00:08:30,230
but it does make each line of code longer.

127
00:08:30,620 --> 00:08:35,620
I recommend using your judgment to figure out when you want to use which type of

128
00:08:35,900 --> 00:08:39,679
argument and depending on the need, you can pick between these two.

129
00:08:41,210 --> 00:08:46,070
Now in the next lesson, I've got a coding exercise for you to put into practice

130
00:08:46,340 --> 00:08:48,020
everything that you've learned so far.

131
00:08:48,260 --> 00:08:51,110
Head over there when you're ready and let's give it a go.

