1 00:00:00,180 --> 00:00:06,240 Welcome to another video of our pipeline listeners today we're going to talk about functions and python 2 00:00:07,830 --> 00:00:13,560 functions or just a repeatable blocks of code, meaning you can have a block of code and repeat the 3 00:00:13,560 --> 00:00:21,210 block whenever you want by calling the function name a function can accept inputs or arguments inside 4 00:00:21,360 --> 00:00:22,120 parentheses. 5 00:00:22,740 --> 00:00:25,470 You can also return values to the calling program. 6 00:00:27,030 --> 00:00:30,360 Let's create a very simple function that takes no arguments. 7 00:00:31,260 --> 00:00:33,690 We start our function by the word def. 8 00:00:35,790 --> 00:00:41,040 It's short for define followed by the name of the function you want to create. 9 00:00:48,000 --> 00:00:49,290 Now let's print something. 10 00:01:02,220 --> 00:01:06,270 Now, if we want to call this function from our program, we simply type its name. 11 00:01:11,560 --> 00:01:18,580 Right now, if we want our function to return something to us, we type return followed by whatever 12 00:01:18,580 --> 00:01:22,720 we want to return or just using the word return and we'll return nothing. 13 00:01:23,230 --> 00:01:28,050 But all of that has to happen inside the function itself, not from the main program. 14 00:01:37,890 --> 00:01:43,260 Let's create a variable in our main program now to store the value of X. We get from the function. 15 00:01:57,150 --> 00:01:57,490 Good. 16 00:01:58,350 --> 00:02:04,920 Now let's see how we pass arguments to dysfunction, let's create a function that does a simple addition 17 00:02:04,920 --> 00:02:06,060 to two numbers. 18 00:02:15,890 --> 00:02:21,800 See, here we are passing X and Y to the function, then we can we can we can create a variable called 19 00:02:21,800 --> 00:02:25,010 result and we saw the addition of X and Y into it. 20 00:02:35,620 --> 00:02:41,170 Now, let's call this function, but we need to pass a value for X and Y, let's pass five, four, 21 00:02:41,170 --> 00:02:42,730 six and ten for Y. 22 00:02:52,840 --> 00:02:58,150 Notice that when passing an argument, they have to be in the same quarter as the function. 23 00:02:59,140 --> 00:03:07,120 So now if we pass 10 first and then five, 10 will be stored in X and five will be stored in Y, but 24 00:03:07,120 --> 00:03:11,010 in this example, five will be stored in X and 10 will be stored in one. 25 00:03:11,950 --> 00:03:14,130 Of course, since we're adding them, it doesn't matter. 26 00:03:14,350 --> 00:03:16,690 But maybe if you're dividing it will. 27 00:03:23,150 --> 00:03:28,510 Now, what happens if we call a function that takes two arguments, but we only pass one of them? 28 00:03:29,570 --> 00:03:30,230 Let's see. 29 00:03:39,350 --> 00:03:45,110 As you noticed, it didn't like that the function takes at least two arguments, but only one given. 30 00:03:46,220 --> 00:03:47,480 Is there a way around this? 31 00:03:48,080 --> 00:03:52,610 Well, yeah, we can have an optional argument, but it must be specified. 32 00:03:52,730 --> 00:03:55,670 Let's let's see how we accomplish this. 33 00:04:11,100 --> 00:04:16,680 Now, whether we pass S. or not, it will still use the default value of C, which is 10. 34 00:04:17,660 --> 00:04:22,780 So even if we passed only two arguments still here, we'll use the default value for. 35 00:04:31,930 --> 00:04:36,340 But if we want to change the default value of C, we can pass a different value for it. 36 00:04:44,760 --> 00:04:45,090 Good. 37 00:04:45,480 --> 00:04:51,120 Now let's create our first script that will have three functions add, subtract and multiply. 38 00:04:53,500 --> 00:05:01,450 Like the best script, the first line is our Python interpreter, keep in mind that while our variable 39 00:05:01,450 --> 00:05:08,050 name inside all functions are the same, which is result, but they are only accessible from inside 40 00:05:08,050 --> 00:05:08,890 the functionally. 41 00:05:09,610 --> 00:05:16,000 So you can add the same variable inside each function, but that variable belongs only to that function. 42 00:05:17,770 --> 00:05:19,330 Now let's run the script. 43 00:05:26,810 --> 00:05:30,590 Now, we have reached the end of this lesson, thank you and see you in the next one.