1 00:00:01,090 --> 00:00:03,990 Having a good understanding of what a function argument means, 2 00:00:03,990 --> 00:00:08,950 we can now talk about another important component known as default arguments. 3 00:00:08,950 --> 00:00:11,970 Sometimes, if an argument is not passed through, but 4 00:00:11,970 --> 00:00:15,450 you want the function to have a starting value no matter what, 5 00:00:15,450 --> 00:00:18,840 this is where a default argument will come to your assistance. 6 00:00:18,840 --> 00:00:21,920 Now you can see it's just hello, dollar sign, name. 7 00:00:22,930 --> 00:00:26,390 Then, we'll simply add name equals and 8 00:00:26,390 --> 00:00:30,560 then whatever our definition for the default argument should be. 9 00:00:30,560 --> 00:00:34,650 In this case we'll just do friend that way I can pass through an argument like this. 10 00:00:34,650 --> 00:00:37,410 So I'll say hello and then Mike. 11 00:00:37,410 --> 00:00:40,080 That way it'll say Hi, Mike, how's it going? 12 00:00:40,080 --> 00:00:43,420 But if I choose to just pass through hello, 13 00:00:43,420 --> 00:00:47,250 remember we didn't define anything outside of the function scope like before, but 14 00:00:47,250 --> 00:00:50,940 now it's gonna say Hi, friend, how's it going? 15 00:00:50,940 --> 00:00:54,010 Now what if we wanted to do multiple default arguments, 16 00:00:54,010 --> 00:00:55,900 just like in our greet function? 17 00:00:55,900 --> 00:01:00,420 We'll simply add the same way we did it before, which is equals friend and 18 00:01:00,420 --> 00:01:04,490 then equals morning on time_of_day and name, respectively. 19 00:01:04,490 --> 00:01:08,690 This way I can still call, greet Mike afternoon and 20 00:01:08,690 --> 00:01:11,240 get, Hi, Mike, good afternoon. 21 00:01:11,240 --> 00:01:16,600 But if I pass with no arguments whatsoever, it'll just say, Hi, friend, 22 00:01:16,600 --> 00:01:17,520 good morning. 23 00:01:17,520 --> 00:01:22,270 Now let's say we wanted to make an argument predefined as optional. 24 00:01:22,270 --> 00:01:26,180 Well the way we do that is in the same way we've done default arguments. 25 00:01:26,180 --> 00:01:30,920 Let's say for instance, I don't really need you to provide me a time of day. 26 00:01:30,920 --> 00:01:35,400 Well, let's do that by simply eliminating it set to morning and 27 00:01:35,400 --> 00:01:37,400 make it equal to Null. 28 00:01:37,400 --> 00:01:42,150 This way, we have to check inside the function if time of day is set. 29 00:01:42,150 --> 00:01:44,770 So we'll check if time_of_day, and 30 00:01:44,770 --> 00:01:49,168 then we'll surround our previous statement with an if and an else. 31 00:01:49,168 --> 00:01:52,890 Now, if time_of_day is equal something other than null, 32 00:01:52,890 --> 00:01:56,780 it'll say hi name and then good time_of_day. 33 00:01:56,780 --> 00:01:59,430 But we need to provide an else as well. 34 00:01:59,430 --> 00:02:04,240 So what we'll do is just say echo hello comma name. 35 00:02:04,240 --> 00:02:05,945 And close that statement with a semicolon. 36 00:02:07,260 --> 00:02:12,490 This way I can still say greet Mike afternoon and get, Hi, Mike, 37 00:02:12,490 --> 00:02:13,560 good afternoon. 38 00:02:13,560 --> 00:02:16,700 But I'm also able to just pass through Mike to our 39 00:02:16,700 --> 00:02:20,580 greet function without the time of day and we'll end up getting this back. 40 00:02:20,580 --> 00:02:21,080 Hi, Mike. 41 00:02:22,500 --> 00:02:25,370 So now let's talk a look here in Workspaces on how to 42 00:02:25,370 --> 00:02:27,780 work with default arguments. 43 00:02:27,780 --> 00:02:32,340 So far we have had some arguments here, we've passed through arguments, 44 00:02:32,340 --> 00:02:35,240 if we don't pass an argument we'll end up getting an error. 45 00:02:35,240 --> 00:02:38,780 So what we'll need to do is create default arguments so 46 00:02:38,780 --> 00:02:42,300 that way we don't get the errors when we forget to pass through. 47 00:02:42,300 --> 00:02:45,260 We'll also look at how to make arguments optional. 48 00:02:45,260 --> 00:02:48,710 So, let's start with creating a function that will show a little 49 00:02:48,710 --> 00:02:53,930 bit of info about someone and maybe who they are or a title. 50 00:02:53,930 --> 00:02:56,260 So let's start with creating that function. 51 00:02:56,260 --> 00:03:01,785 So function and then get info. 52 00:03:01,785 --> 00:03:03,810 'Kay. And we're gonna have two argument here, 53 00:03:03,810 --> 00:03:07,999 one is gonna be the name and next one is going to be the title. 54 00:03:07,999 --> 00:03:09,120 'Kay? 55 00:03:09,120 --> 00:03:12,110 Then open and close our curly braces. 56 00:03:12,110 --> 00:03:13,110 So now we have our function. 57 00:03:14,110 --> 00:03:17,450 Now inside of this function what we want to do is just echo a string. 58 00:03:17,450 --> 00:03:20,850 So echo, and then we'll go ahead and 59 00:03:20,850 --> 00:03:26,090 do double quotes even though we're going to output some variable information. 60 00:03:26,090 --> 00:03:31,330 So let's start with name and then has arrived, 61 00:03:32,990 --> 00:03:38,410 they are with us as a and then title. 62 00:03:38,410 --> 00:03:45,010 So this way in a string format if I was to say get info. 63 00:03:45,010 --> 00:03:46,210 And then pass through two strings. 64 00:03:46,210 --> 00:03:51,910 So let's say Mike and Mike is a frog. 65 00:03:51,910 --> 00:03:54,610 Okay. So we have multiple arguments here and 66 00:03:54,610 --> 00:03:58,030 we have one is the name and next will be in the title. 67 00:03:58,030 --> 00:04:01,550 So notice they are in the same order as they were defined on line three. 68 00:04:01,550 --> 00:04:06,700 So I'm going to go ahead and save this and then take out, take a look at the preview. 69 00:04:06,700 --> 00:04:09,240 So here we see it says Mike has arrived. 70 00:04:09,240 --> 00:04:11,310 They are with us as a frog. 71 00:04:11,310 --> 00:04:13,810 Okay. That seems perfectly reasonable. 72 00:04:13,810 --> 00:04:19,040 Let's go back over and say that we forget to type in frog. 73 00:04:19,040 --> 00:04:21,600 We just save this and hit refresh. 74 00:04:21,600 --> 00:04:22,780 It'll say Mike has arrived. 75 00:04:22,780 --> 00:04:25,030 They are with us as a, nothing. 76 00:04:25,030 --> 00:04:25,780 There's a blank. 77 00:04:25,780 --> 00:04:27,160 Well we don't want that so 78 00:04:27,160 --> 00:04:31,830 we're gonna need to define some kind of default argument here. 79 00:04:31,830 --> 00:04:37,870 So let's go back over to our code and then say, get info, title, and name. 80 00:04:37,870 --> 00:04:41,750 Well let's say we want to define a title to start with so 81 00:04:41,750 --> 00:04:45,770 we say title equals, and this is where our default is. 82 00:04:45,770 --> 00:04:46,710 And we'll say, friend. 83 00:04:46,710 --> 00:04:50,200 All right, now save it. 84 00:04:50,200 --> 00:04:55,750 And if we don't use title as an argument it should come through as friend. 85 00:04:55,750 --> 00:04:58,750 So we'll go ahead and hit our Preview. 86 00:04:58,750 --> 00:04:59,900 Refresh. 87 00:04:59,900 --> 00:05:02,420 And there it says, Mike has arrived, they with us as a friend. 88 00:05:02,420 --> 00:05:03,480 Which is great. 89 00:05:03,480 --> 00:05:04,700 Okay. 90 00:05:04,700 --> 00:05:09,740 Now what if we wanted to take, and make the title completely optional? 91 00:05:09,740 --> 00:05:12,128 Well, that's pretty easy to do. 92 00:05:12,128 --> 00:05:17,790 All we need to do is remove equals friend, and just simply say equals null. 93 00:05:17,790 --> 00:05:23,000 Okay, this way it's not gonna report back any kind of errors or notices for us. 94 00:05:23,000 --> 00:05:24,980 But we need to do something a little different. 95 00:05:24,980 --> 00:05:27,620 Cuz otherwise, it's just gonna come up with that blank again. 96 00:05:27,620 --> 00:05:31,880 So let's create an if statement to test whether or not title exists. 97 00:05:31,880 --> 00:05:34,850 So we'll say if, and then we'll say title. 98 00:05:36,040 --> 00:05:38,520 We don't need to see if it's true or anything like that. 99 00:05:38,520 --> 00:05:41,830 Just simply saying if title, make sure that it exists. 100 00:05:41,830 --> 00:05:43,770 It's anything other than null. 101 00:05:43,770 --> 00:05:45,030 All right. So if title. 102 00:05:45,030 --> 00:05:48,370 We're gonna open and close our curly braces, oh and then else. 103 00:05:48,370 --> 00:05:50,210 We're going to do something different. 104 00:05:50,210 --> 00:05:51,090 All right. 105 00:05:51,090 --> 00:05:53,570 Well I'm actually going to actually use this line. 106 00:05:53,570 --> 00:05:57,310 So, I'm gonna cut it, and then head back up here and paste it in. 107 00:05:57,310 --> 00:05:58,500 Make sure my tabs are good. 108 00:05:58,500 --> 00:05:59,310 Okay. 109 00:05:59,310 --> 00:06:02,920 And then here if we don't have a title, we want to echo something different. 110 00:06:02,920 --> 00:06:10,575 So, echo and then name has arrived, and then welcome. 111 00:06:10,575 --> 00:06:12,089 Perfect, all right. 112 00:06:12,089 --> 00:06:17,280 So now we close that with a semicolon to end our statement. 113 00:06:17,280 --> 00:06:18,170 So if we go down and 114 00:06:18,170 --> 00:06:22,770 run what we have currently by going to our preview button and refreshing it. 115 00:06:22,770 --> 00:06:24,770 Now it says, Mike has arrived, welcome. 116 00:06:24,770 --> 00:06:29,510 Because on line 13 here we did not pass through an argument. 117 00:06:29,510 --> 00:06:34,570 But if we do pass through an argument here such as let's say, frog. 118 00:06:36,190 --> 00:06:36,940 Save it. 119 00:06:36,940 --> 00:06:39,170 And then refresh our preview. 120 00:06:39,170 --> 00:06:42,370 Now it will say, Mike has arrived, they are with us as a frog. 121 00:06:42,370 --> 00:06:45,960 So, in order to get a optional argument we can simply 122 00:06:45,960 --> 00:06:50,760 pass through the default argument as null, or we can pass through a string or 123 00:06:50,760 --> 00:06:54,250 any kind of value we want to use as our default argument 124 00:06:54,250 --> 00:06:58,079 in the argument definition of the function such as in line three.