1 00:00:00,790 --> 00:00:02,640 - [Instructor] In this video, I'm going to demonstrate 2 00:00:02,640 --> 00:00:04,807 default parameter values. 3 00:00:04,807 --> 00:00:08,200 Now, when you're defining functions in Python, 4 00:00:08,200 --> 00:00:12,420 it's extremely common to provide default values 5 00:00:12,420 --> 00:00:15,070 for each of the parameters you define 6 00:00:15,070 --> 00:00:18,240 so that people can have the flexibility 7 00:00:18,240 --> 00:00:21,900 of calling your functions a number of different ways. 8 00:00:21,900 --> 00:00:23,850 So for the purpose of this example, 9 00:00:23,850 --> 00:00:27,403 let's once again jump into ipython, 10 00:00:28,480 --> 00:00:31,710 and let's define a new function. 11 00:00:31,710 --> 00:00:33,910 We'll call it rectangle 12 00:00:35,500 --> 00:00:36,333 area, 13 00:00:37,230 --> 00:00:39,860 and for a rectangle's area, 14 00:00:39,860 --> 00:00:44,480 we need to be able to calculate the length times the width, 15 00:00:44,480 --> 00:00:47,100 so we need two different pieces of information 16 00:00:47,100 --> 00:00:49,460 to perform that calculation. 17 00:00:49,460 --> 00:00:53,000 So let's define a length parameter, 18 00:00:53,000 --> 00:00:56,870 and just for argument's sake, no pun intended, 19 00:00:56,870 --> 00:01:00,330 let's give the length a default value of two, 20 00:01:00,330 --> 00:01:02,520 and let's have a width parameter as well, 21 00:01:02,520 --> 00:01:05,340 and we'll give that a default value of three. 22 00:01:05,340 --> 00:01:09,280 Now, when you're doing default arguments, 23 00:01:09,280 --> 00:01:12,010 you typically wanna choose realistic values 24 00:01:12,010 --> 00:01:15,300 that somebody would normally want as their default. 25 00:01:15,300 --> 00:01:18,320 So you might use one for the length and one for the width, 26 00:01:18,320 --> 00:01:20,690 or even zero for each of those. 27 00:01:20,690 --> 00:01:24,320 That would be up to you as the definer of the function. 28 00:01:24,320 --> 00:01:27,400 But we want to demonstrate the basic concept here, 29 00:01:27,400 --> 00:01:29,990 so we chose some values that we probably 30 00:01:29,990 --> 00:01:32,400 would not use normally. 31 00:01:32,400 --> 00:01:34,190 And as we typically do, 32 00:01:34,190 --> 00:01:37,430 we're going to give this a docstring, 33 00:01:37,430 --> 00:01:41,683 so return a rectangle's area. 34 00:01:43,420 --> 00:01:47,160 And the return statement for this is straightforward. 35 00:01:47,160 --> 00:01:50,660 We simply want to return the length times the width. 36 00:01:50,660 --> 00:01:55,330 So a relatively simple function definition in this case. 37 00:01:55,330 --> 00:01:57,150 Now, because of the fact 38 00:01:57,150 --> 00:01:59,860 that we've provided default arguments, 39 00:01:59,860 --> 00:02:02,870 we actually have a couple of different ways 40 00:02:02,870 --> 00:02:04,780 that we can call this function. 41 00:02:04,780 --> 00:02:08,290 So we can say let's call rectangle area 42 00:02:08,290 --> 00:02:10,520 with no arguments whatsoever. 43 00:02:10,520 --> 00:02:13,690 Now there is no function defined that takes no arguments, 44 00:02:13,690 --> 00:02:16,790 but there is a function defined with the same name 45 00:02:16,790 --> 00:02:20,490 that has default values for every one of its parameters. 46 00:02:20,490 --> 00:02:24,120 So this function call that I'm about to execute 47 00:02:24,120 --> 00:02:26,030 is actually going to be treated 48 00:02:26,030 --> 00:02:30,770 as if I pass in two for the length and three for the width. 49 00:02:30,770 --> 00:02:35,000 So let's give that a shot and we see that we get six back. 50 00:02:35,000 --> 00:02:38,810 Now, if I call rectangle area and let's say 51 00:02:38,810 --> 00:02:41,900 I just pass it a single value, ten, 52 00:02:41,900 --> 00:02:45,050 in this case, the Python interpreter is going to take 53 00:02:45,050 --> 00:02:48,550 that value and use it as the first parameter 54 00:02:48,550 --> 00:02:49,660 in the parameter list, 55 00:02:49,660 --> 00:02:51,480 so the length will be ten, 56 00:02:51,480 --> 00:02:53,968 and because we're not supplying a second parameter, 57 00:02:53,968 --> 00:02:55,590 the width will be three. 58 00:02:55,590 --> 00:02:57,880 So ten times three should give me 30, 59 00:02:57,880 --> 00:02:59,940 and indeed it does. 60 00:02:59,940 --> 00:03:03,160 Now of course we can always call rectangle area width 61 00:03:03,160 --> 00:03:05,530 both arguments that we specified, 62 00:03:05,530 --> 00:03:08,560 in which case it will simply use those two values 63 00:03:08,560 --> 00:03:10,510 to calculate the results. 64 00:03:10,510 --> 00:03:13,790 Now in the next video, we'll take a look at a concept 65 00:03:13,790 --> 00:03:17,930 called keyword arguments and talk about other ways 66 00:03:17,930 --> 00:03:20,813 in which we could call this same function.