1 00:00:00,000 --> 00:00:04,000 The function you have seen in the previous lesson is very simple, 2 00:00:04,001 --> 00:00:07,000 and it will do the same thing every time you call it, 3 00:00:07,001 --> 00:00:10,000 which is just to print hello, and then, well, let's just remove that. 4 00:00:10,001 --> 00:00:14,000 So it's just going to print hello every time you call the function. 5 00:00:14,001 --> 00:00:17,000 Now you can add parameters to a function. 6 00:00:17,001 --> 00:00:19,632 A parameter is simply a variable you are going 7 00:00:19,644 --> 00:00:22,000 to pass to the function when you call it. 8 00:00:22,001 --> 00:00:28,000 And then inside the function, you can use this variable to do anything you want. 9 00:00:28,001 --> 00:00:33,000 And let's see this with an example. Let's continue with our say hello function. 10 00:00:33,001 --> 00:00:37,000 Let's say we want now to say hello with the name. 11 00:00:37,001 --> 00:00:41,000 So we want to pass a name to the function to say hello to that name. 12 00:00:41,001 --> 00:00:45,000 I'm going to add a parameter here, user name. 13 00:00:45,001 --> 00:00:50,000 So this is going to be a variable inside the function. 14 00:00:50,001 --> 00:00:57,000 And now I can use the variable in the function, for example, hello plus user name. 15 00:00:58,000 --> 00:01:01,000 So as you can see, I can reuse the parameter. 16 00:01:01,001 --> 00:01:03,000 So this is a variable. This is the same thing. 17 00:01:03,001 --> 00:01:09,000 I can reuse the parameter I got in the function directly 18 00:01:09,001 --> 00:01:12,000 inside the function, inside the execution of the function. 19 00:01:12,001 --> 00:01:15,000 Now if I run this, I'm going to have an error. 20 00:01:15,001 --> 00:01:20,000 Why is that? Because here I am calling the function say hello, 21 00:01:20,001 --> 00:01:25,000 and you can see missing one required positional argument username. 22 00:01:26,000 --> 00:01:31,000 Actually, let's remove this. Let's just call it once. 23 00:01:31,001 --> 00:01:34,054 Okay, so say hello. The thing now is that there 24 00:01:34,066 --> 00:01:37,000 is no function say hello with zero parameter. 25 00:01:37,001 --> 00:01:40,000 There is one function say hello with one parameter. 26 00:01:40,001 --> 00:01:46,000 So I need to give it a parameter. And here I'm going to give it a username, Bob. 27 00:01:46,001 --> 00:01:52,000 So say hello, Bob. And then Bob is going to be passed as a parameter here, 28 00:01:52,001 --> 00:01:55,000 and we can use it in the function. Let's run that. 29 00:01:56,000 --> 00:01:59,000 Hello, Bob. Okay, so that is very convenient. 30 00:01:59,001 --> 00:02:02,000 Now what I can do is I can do say hello. 31 00:02:02,001 --> 00:02:06,000 So I can use the autocompletion here, and let's say John. 32 00:02:06,001 --> 00:02:11,000 And as you can see, hello, Bob, and hello, John. 33 00:02:11,001 --> 00:02:14,000 So we have the same exact same code for the function, 34 00:02:14,001 --> 00:02:18,000 but with the parameters, we can make it more dynamic. 35 00:02:18,001 --> 00:02:21,000 Okay, depending on what we pass in the parameter, 36 00:02:21,001 --> 00:02:25,000 the execution of the function would be different here. 37 00:02:25,001 --> 00:02:28,000 And of course, you can add as many parameters as you want. 38 00:02:28,001 --> 00:02:30,000 Let's say you want also the user age. 39 00:02:30,001 --> 00:02:41,000 So I can do hello username, and then plus comma, you are, and then plus user age. 40 00:02:41,001 --> 00:02:44,250 So now those two functions are, those two 41 00:02:44,262 --> 00:02:48,000 calls to the functions are not correct anymore, 42 00:02:48,001 --> 00:02:51,000 because now we need to pass two parameters. 43 00:02:51,001 --> 00:02:53,000 And the order is very important. 44 00:02:53,001 --> 00:02:57,000 You need to pass the first user name and then the user age. 45 00:02:57,001 --> 00:03:03,000 So Bob, and let's say 45, and John, and let's say 42. 46 00:03:03,001 --> 00:03:08,000 I'm going to run the main, and you can see we have an error. 47 00:03:08,001 --> 00:03:12,000 Okay, and this is why this is a cast error. 48 00:03:12,001 --> 00:03:18,000 You can only concatenate string with string and not integer. 49 00:03:18,001 --> 00:03:25,000 So here we have a user age that we just concatenate with this string. 50 00:03:25,001 --> 00:03:27,000 The problem is that we pass an integer here. 51 00:03:27,001 --> 00:03:31,000 Okay, that's quite normal because that's a user age. 52 00:03:31,001 --> 00:03:33,000 So what do we need to do in the print function? 53 00:03:33,001 --> 00:03:38,000 We need to cast this integer into a string. 54 00:03:38,001 --> 00:03:40,000 So we need to call the str here. 55 00:03:40,001 --> 00:03:45,000 We need to cast to str inside this username. 56 00:03:45,001 --> 00:03:49,000 We didn't have the error before because we pass a string. 57 00:03:49,001 --> 00:03:52,000 So this we know this is going to be a string. 58 00:03:52,001 --> 00:03:57,000 This might be an integer, so we are going to cast it to string. 59 00:03:57,001 --> 00:03:59,000 Now I run it, and it's going to work. 60 00:03:59,001 --> 00:04:01,000 Hello, Bob, you are 45. 61 00:04:01,001 --> 00:04:02,000 Hello, John, you are 42. 62 00:04:02,001 --> 00:04:07,000 Okay, so you can make this function much more complex with many lines of code, 63 00:04:07,001 --> 00:04:10,000 and then you can call it as many times as you want. 64 00:04:10,001 --> 00:04:13,131 And with different parameters, you are going 65 00:04:13,143 --> 00:04:16,000 to have different code executions, okay? 66 00:04:16,001 --> 00:04:20,000 But you just need to write the function once, and that's it. 67 00:04:20,001 --> 00:04:25,000 So to recap, with parameters, you can make your functions more dynamic. 68 00:04:25,001 --> 00:04:30,816 With just one block of code in a function, you can repeat a set of instructions with 69 00:04:30,828 --> 00:04:34,000 different values any number of times you want.