1 00:00:06,000 --> 00:00:09,070 In this video, we'll learn how we can call c++ functions 2 00:00:09,070 --> 00:00:10,930 using default argument values. 3 00:00:11,990 --> 00:00:14,990 As we've seen, when we call a function, we must provide all 4 00:00:14,990 --> 00:00:17,850 the arguments that, that function requires, and they must be in the 5 00:00:17,850 --> 00:00:19,330 right order and of the right type. 6 00:00:19,820 --> 00:00:22,200 But sometimes when we call functions some of the argument 7 00:00:22,200 --> 00:00:25,990 values that we pass in tend to be the same values most of the time. 8 00:00:26,849 --> 00:00:29,500 For example, if we have a function that calculates the cost 9 00:00:29,500 --> 00:00:32,280 of an item, we can provide the function with the base cost of 10 00:00:32,409 --> 00:00:34,259 the item and the sales tax rate. 11 00:00:34,920 --> 00:00:39,270 Suppose that 98% of our customers live in a region where the tax rate is 6%, 12 00:00:39,970 --> 00:00:43,790 that means that we have to provide the tax rate in every function call even 13 00:00:43,790 --> 00:00:46,240 though it will almost always be 6%. 14 00:00:46,960 --> 00:00:50,440 C++ allows us to provide default values for arguments. 15 00:00:50,700 --> 00:00:53,690 So in the case of the sales tax, if we omit the argument from the 16 00:00:53,690 --> 00:00:57,830 function call, then the c++ compiler will automatically replace 17 00:00:57,830 --> 00:01:00,090 it with the default value of 6%. 18 00:01:00,790 --> 00:01:03,640 However, if we have a customer whose sales tax rate is 8%, then 19 00:01:03,940 --> 00:01:07,350 we can provide it explicitly and override the default value. 20 00:01:08,170 --> 00:01:11,300 As we'll see in the next few slides, we can add default arguments to 21 00:01:11,300 --> 00:01:14,360 the function prototype or the function definition but not both. 22 00:01:14,780 --> 00:01:17,240 But best practice is to do it in the function prototype. 23 00:01:18,610 --> 00:01:22,249 Default arguments must appear at the tail end of the parameter list. 24 00:01:22,559 --> 00:01:24,840 We can also have multiple default arguments. 25 00:01:25,299 --> 00:01:27,770 Let's see an example that demonstrates default arguments. 26 00:01:30,990 --> 00:01:34,390 In this example, we have a function with no default arguments. 27 00:01:34,680 --> 00:01:37,700 In the next slide, we'll modify it so it does have a default argument. 28 00:01:38,400 --> 00:01:41,410 The function is called calculate cost and it expects the base 29 00:01:41,410 --> 00:01:43,269 cost of an item and the tax rate. 30 00:01:44,030 --> 00:01:47,529 The function calculates the tax amount and adds that to the base costs and 31 00:01:47,529 --> 00:01:49,280 returns that value to the caller. 32 00:01:50,310 --> 00:01:53,410 In the main function, we're calling the function with 100.0 33 00:01:53,410 --> 00:01:58,260 as the base cost and 0.06 or 6% percent as the tax rate. 34 00:01:59,240 --> 00:02:03,409 In this case, 106.0 is returned from the function and assigned to cost. 35 00:02:03,639 --> 00:02:04,630 That's pretty easy. 36 00:02:04,760 --> 00:02:09,429 But if 98% of our customers will have a 6% tax rate, I'm forced to supply 37 00:02:09,430 --> 00:02:11,030 this every time I call the function. 38 00:02:11,520 --> 00:02:15,189 Why not let the compiler default the tax rate to 6% 39 00:02:15,190 --> 00:02:16,530 unless I tell it otherwise. 40 00:02:16,880 --> 00:02:18,270 Let's do that in the next slide. 41 00:02:21,299 --> 00:02:24,780 Here, we have the same function except that the function prototype 42 00:02:24,789 --> 00:02:28,120 has been modified to provide a default value for the tax rate. 43 00:02:29,060 --> 00:02:31,250 Notice that the parameter tax rate in the function 44 00:02:31,250 --> 00:02:36,309 prototype now has the default initialization to 0.06 added to it. 45 00:02:37,160 --> 00:02:40,179 This effectively tells the compiler that if this argument 46 00:02:40,180 --> 00:02:44,540 is not provided by the function call, then use the value 0.06 47 00:02:44,810 --> 00:02:46,690 for tax rate, simple as that. 48 00:02:47,000 --> 00:02:49,130 Now notice the two calls in the main function. 49 00:02:49,500 --> 00:02:54,610 The first call simply calls calc cost with 200.0 which is the base cost. 50 00:02:55,610 --> 00:02:58,770 Since the argument for tax rate is not provided, the compiler 51 00:02:58,770 --> 00:03:00,799 will use 6% for the tax rate. 52 00:03:01,410 --> 00:03:03,810 In the second call, we are providing the tax rate. 53 00:03:04,000 --> 00:03:07,660 In this case, the compiler will use that and ignore the default value. 54 00:03:08,590 --> 00:03:09,760 This can be pretty handy. 55 00:03:09,990 --> 00:03:12,580 Suppose we have a function that prints documents and we have to 56 00:03:12,580 --> 00:03:16,260 supply the document name, the printer to print to, the paper size, 57 00:03:16,260 --> 00:03:17,970 the resolution and so forth. 58 00:03:18,829 --> 00:03:21,859 Again, most of the time we print to the default printer 59 00:03:21,860 --> 00:03:23,019 with default settings. 60 00:03:23,250 --> 00:03:25,590 But sometimes, we need higher resolution or a different 61 00:03:25,590 --> 00:03:26,910 printer, you get the idea. 62 00:03:27,520 --> 00:03:30,770 In this case, we can provide multiple default parameters for 63 00:03:30,870 --> 00:03:34,030 everything except the document name which is always required. 64 00:03:34,440 --> 00:03:36,409 This makes writing the code much simpler. 65 00:03:37,390 --> 00:03:40,190 Let's add another default parameter to the calc cost function. 66 00:03:42,870 --> 00:03:45,700 In this example, we'll calculate the cost of an item based on the 67 00:03:45,700 --> 00:03:49,229 base cost of the item, the sales tax and the shipping charge. 68 00:03:49,840 --> 00:03:53,540 As before, most of the times the sales tax will be 6%. 69 00:03:54,059 --> 00:03:58,750 And in this example, the standard typical shipping charge is $3.50. 70 00:03:59,270 --> 00:04:04,940 So most customers will have a 6% tax rate and a $3.50 shipping charge. 71 00:04:05,450 --> 00:04:08,420 We can easily add a second default argument to this function. 72 00:04:08,880 --> 00:04:11,239 Notice that in the function prototype, we added another 73 00:04:11,240 --> 00:04:15,580 parameter named shipping and we default this value to $3.50. 74 00:04:16,649 --> 00:04:19,950 The function body now calculates the tax for the item, adds the 75 00:04:19,950 --> 00:04:23,140 shipping charge, adds this to the original cost of the item and 76 00:04:23,140 --> 00:04:24,580 returns the result to the caller. 77 00:04:25,099 --> 00:04:28,670 In the main function, we're calling calc cost in three different ways. 78 00:04:29,330 --> 00:04:32,470 In the first call, we're providing all three arguments to the function. 79 00:04:32,900 --> 00:04:36,400 In this case, no default values will be used and the tax rate will 80 00:04:36,400 --> 00:04:39,900 be 8% and shipping will be $4.25. 81 00:04:40,980 --> 00:04:43,139 In the second call, we're providing the cost of the 82 00:04:43,139 --> 00:04:45,290 item and the tax rate of 8%. 83 00:04:45,390 --> 00:04:48,069 In this case, the default value for shipping will be used. 84 00:04:48,369 --> 00:04:51,140 But the tax rate will use the 8% we provided. 85 00:04:51,590 --> 00:04:54,420 In the final example, we're only providing the cost of the item. 86 00:04:54,610 --> 00:04:56,830 So in this case, both default values are used. 87 00:04:56,860 --> 00:05:01,860 The tax rate will default to 6% and the shipping charge to $3.50. 88 00:05:02,710 --> 00:05:05,529 As you can see, default argument values give us the ability to 89 00:05:05,529 --> 00:05:08,900 make our code less verbose and potentially prevent errors by using 90 00:05:08,900 --> 00:05:12,010 default values rather than having to supply all of the arguments 91 00:05:12,150 --> 00:05:13,530 to the function all the time. 92 00:05:14,929 --> 00:05:17,549 In the next video, we'll learn about function overloading. 93 00:05:18,860 --> 00:05:21,399 Before we talk about function overloading in the next video, 94 00:05:21,759 --> 00:05:23,210 let me go over default arguments. 95 00:05:23,510 --> 00:05:26,720 I'm in the section 11 workspace in the default arguments project. 96 00:05:26,880 --> 00:05:29,030 And this is the same example you saw on the slides. 97 00:05:29,310 --> 00:05:33,030 Here, I've got a function called calculate cost that expects $3. 98 00:05:33,030 --> 00:05:39,486 The base cost, a tax rate, which is a default argument at 0.06 or 6% and 99 00:05:39,740 --> 00:05:42,310 the default shipping which is $3.50. 100 00:05:42,900 --> 00:05:46,240 Now if those arguments are not supplied, then the defaults kick in. 101 00:05:46,410 --> 00:05:47,590 It's really pretty simple. 102 00:05:47,940 --> 00:05:49,170 And here's the calculation. 103 00:05:49,170 --> 00:05:52,850 Notice, I don't have to include the function default arguments 104 00:05:52,850 --> 00:05:56,429 here in the function definition, only in the function prototype and 105 00:05:56,429 --> 00:05:58,000 that's usually where you see them. 106 00:05:59,090 --> 00:06:03,300 So the code here says base cost plus equals base cost times the tax rate, 107 00:06:03,300 --> 00:06:04,920 so I'm figuring out what's my tax. 108 00:06:05,340 --> 00:06:07,620 And I'm adding the shipping to that and then I'm adding 109 00:06:07,620 --> 00:06:08,719 the whole thing to base cost. 110 00:06:08,740 --> 00:06:10,049 So I'm not taxing the shipping. 111 00:06:10,610 --> 00:06:12,129 And then we've got some sample calls. 112 00:06:12,200 --> 00:06:14,589 In this first example, I'm calling calculate costs 113 00:06:14,730 --> 00:06:16,200 with all three arguments. 114 00:06:16,350 --> 00:06:19,060 So in this case, none of the defaults will be used. 115 00:06:19,420 --> 00:06:23,590 I'll use 100 for the base cost, I'll use 8% for the tax rate and I'll use 116 00:06:23,590 --> 00:06:26,080 four $4.25 for the shipping charge. 117 00:06:26,620 --> 00:06:29,620 I expect to get back 112 and 25. 118 00:06:30,210 --> 00:06:33,329 For this example, here, I'm providing the first two arguments, 119 00:06:33,349 --> 00:06:34,840 and I'm not providing the shipping. 120 00:06:35,129 --> 00:06:39,309 So in this case, it's going to use a 100 as the base cost, 8% 121 00:06:39,309 --> 00:06:42,949 for the tax rate and it'll default to 350 here for the shipping. 122 00:06:43,839 --> 00:06:45,309 And then this last example, 123 00:06:45,309 --> 00:06:47,100 I'm supplying the base cost only. 124 00:06:47,100 --> 00:06:51,100 So in this case, 6% and %3.50 are what're going to be used. 125 00:06:51,729 --> 00:06:54,750 So if I run this program, you'll see that our results are 126 00:06:54,750 --> 00:06:59,490 exactly it was as we expected: 112.25, 111.50 and 215.50. 127 00:07:00,230 --> 00:07:03,120 Okay, so you can see default arguments are pretty easy to use. 128 00:07:03,420 --> 00:07:06,069 I could also default the base cost if I wanted to. 129 00:07:06,270 --> 00:07:11,709 I could say that the base cost is a 100. Maybe that's the typical 130 00:07:11,740 --> 00:07:13,020 item that most people order. 131 00:07:13,160 --> 00:07:21,039 So in this case, I could just simply just say cost is calc cost, right, 132 00:07:21,040 --> 00:07:22,470 with nothing in the parentheses. 133 00:07:22,900 --> 00:07:25,129 So I'm not supplying any arguments here. 134 00:07:26,469 --> 00:07:27,300 That's it. 135 00:07:27,309 --> 00:07:29,089 So what's going to happen here is it's going to use 136 00:07:29,150 --> 00:07:30,720 all three default arguments. 137 00:07:30,720 --> 00:07:32,750 So let me print that out real quick. 138 00:07:35,700 --> 00:07:38,980 So in this case, we expect the same result as -- well, if we would have 139 00:07:38,980 --> 00:07:44,439 done 106, so we'd have 100 is the base cost that's being used by default. 140 00:07:44,799 --> 00:07:48,640 We're using a 6% tax rate so that's another $6 and 141 00:07:48,900 --> 00:07:50,380 our shipping would be 3.50. 142 00:07:52,350 --> 00:07:54,810 So in this case, we expect 109.50. 143 00:07:54,890 --> 00:07:59,230 So let's give that a shot, and that's what we're getting here, 109.50. 144 00:07:59,960 --> 00:08:01,190 This is pretty straightforward. 145 00:08:01,200 --> 00:08:06,200 The only rule with default arguments is that once you create a parameter 146 00:08:06,200 --> 00:08:09,710 that has a default value, then you've got to default the rest of them. 147 00:08:09,730 --> 00:08:13,549 So in other words, I can't say that base cost is 100 and then 148 00:08:13,550 --> 00:08:18,959 not have one here because that's going to give me an error, right. 149 00:08:19,240 --> 00:08:21,580 Once you start, they all have to be on the tail end. 150 00:08:21,700 --> 00:08:24,989 So in this case, I could just say 0.06. 151 00:08:25,700 --> 00:08:27,560 So that's really the only rule there. 152 00:08:28,100 --> 00:08:29,880 And I'll run that again just to make sure I've got no 153 00:08:29,930 --> 00:08:31,470 errors, and there you go. 154 00:08:32,340 --> 00:08:36,219 So let's add another default type function to this application. 155 00:08:36,789 --> 00:08:38,198 How about a greeting function? 156 00:08:38,429 --> 00:08:39,558 So let's do something like this. 157 00:08:39,570 --> 00:08:43,030 Let's create a function prototype here for a function 158 00:08:43,129 --> 00:08:46,190 called greeting that expects a string, which is someone's name. 159 00:08:46,490 --> 00:08:49,220 Now let me include string because I need the library. 160 00:08:53,780 --> 00:08:54,890 So that's a string name. 161 00:08:55,150 --> 00:08:55,560 Perfect. 162 00:08:55,560 --> 00:08:58,250 So that would be Frank Metropoulos, Joe Miller, whatever you want. 163 00:08:58,779 --> 00:09:02,250 Then I've got another string, which is the prefix that we 164 00:09:02,250 --> 00:09:03,230 want in front of their name. 165 00:09:03,240 --> 00:09:05,819 Mr., Dr., Mrs., whatever you like. 166 00:09:06,340 --> 00:09:07,870 And I'm going to prefix it to Mr. 167 00:09:07,870 --> 00:09:09,850 Let's say that most of my customers are male. 168 00:09:11,320 --> 00:09:13,130 And I also want a suffix. 169 00:09:15,500 --> 00:09:18,149 And in this case, the suffix I'm going to default it 170 00:09:18,170 --> 00:09:19,410 to just the empty string. 171 00:09:19,950 --> 00:09:21,740 Okay, so that's my function prototype. 172 00:09:22,640 --> 00:09:24,170 Let's implement this function now. 173 00:09:24,340 --> 00:09:25,700 We'll implement it right down here. 174 00:09:28,440 --> 00:09:31,620 And at this point, we don't need those default arguments. 175 00:09:31,620 --> 00:09:34,219 We can just get rid of them from here, and it cleans 176 00:09:34,230 --> 00:09:35,400 up the code a little bit. 177 00:09:36,750 --> 00:09:38,290 All right, so what do we want to do here. 178 00:09:38,310 --> 00:09:40,360 We just really want to display some stuff, right. 179 00:09:40,560 --> 00:09:45,020 So let's just say I want an output statement, and I'm going to say hello, 180 00:09:46,910 --> 00:09:53,599 followed by a space, followed by the prefix and what we could do here 181 00:09:53,600 --> 00:09:57,280 is we could append a space, right. 182 00:09:58,010 --> 00:10:05,400 We can use concatenation here, and the name and another space and the suffix. 183 00:10:06,760 --> 00:10:09,960 Okay, so we're just using concatenation here, just like 184 00:10:09,960 --> 00:10:11,770 we've learned in string class. 185 00:10:13,560 --> 00:10:16,960 And then we'll use the endline to finish it off. 186 00:10:19,950 --> 00:10:23,580 Okay, so let's run that, and give it a few example runs here. 187 00:10:23,580 --> 00:10:29,279 So let's say how about -- let's do a really simple one, how about greeting. 188 00:10:31,150 --> 00:10:33,190 And I'm going to supply all of these arguments. 189 00:10:33,190 --> 00:10:36,620 So I'm going to say Glenn Jones is the name. 190 00:10:38,940 --> 00:10:40,730 Okay, the prefix will be Dr. 191 00:10:42,360 --> 00:10:44,330 and the suffix will be M.D. 192 00:10:49,139 --> 00:10:53,650 Okay, so if we run that, we expect this to display Dr Glenn Jones M.D. 193 00:10:54,420 --> 00:10:56,180 Okay, so let's give it a shot. 194 00:10:57,870 --> 00:10:59,810 And I forgot the semicolon. 195 00:10:59,810 --> 00:11:01,000 I'll fix that real quick. 196 00:11:03,440 --> 00:11:04,030 And there you go. 197 00:11:04,049 --> 00:11:06,079 Hello Dr Glenn Jones M.D. 198 00:11:06,400 --> 00:11:08,500 Perfect, now let's run some of the other examples. 199 00:11:08,750 --> 00:11:13,840 So how about we do something like greeting James Rogers. 200 00:11:16,500 --> 00:11:24,069 How about professor and Ph.D for the suffix. 201 00:11:27,200 --> 00:11:27,470 Okay. 202 00:11:27,520 --> 00:11:31,650 We'll run that, and now we'll say hello Professor James 203 00:11:31,650 --> 00:11:33,300 Rogers Ph.D, pretty easy. 204 00:11:33,900 --> 00:11:35,390 Now let's use some defaults. 205 00:11:35,400 --> 00:11:39,589 In this case, we'll say greeting, let's say, Frank Miller. 206 00:11:42,260 --> 00:11:44,380 And Dr. 207 00:11:45,740 --> 00:11:47,069 No suffix in this case. 208 00:11:47,069 --> 00:11:49,760 So it'll default to the blank or the empty space. 209 00:11:49,770 --> 00:11:53,979 So in this case, it'll just say Dr Frank Miller, which is what I want. 210 00:11:54,860 --> 00:11:58,489 And then let's do one more, and we'll just say greeting, 211 00:12:01,279 --> 00:12:02,600 let's say, William Smith. 212 00:12:05,380 --> 00:12:10,840 So in this case, it's going to use all the default values. 213 00:12:11,000 --> 00:12:16,819 So it's going to use Mr William Smith followed by the empty 214 00:12:16,820 --> 00:12:18,460 string right there for the suffix. 215 00:12:18,779 --> 00:12:21,500 Okay, so in this case, we're just going to see Mr William Smith. 216 00:12:22,270 --> 00:12:22,750 There you go. 217 00:12:24,100 --> 00:12:32,130 If we have Mary Howard, and she would be Mrs. 218 00:12:37,250 --> 00:12:42,540 And we can give her a title as well, whatever we like. 219 00:12:45,450 --> 00:12:46,550 There you go. 220 00:12:47,679 --> 00:12:50,640 So now we've got Mrs Mary Howard Ph.D instead of Dr. 221 00:12:50,640 --> 00:12:51,800 We could have said doctor here as well. 222 00:12:51,800 --> 00:12:53,720 You can see all the different combinations. 223 00:12:54,039 --> 00:12:55,600 This can come in pretty handy. 224 00:12:55,670 --> 00:12:58,210 I used to work with someone who used to say that if something is always 225 00:12:58,210 --> 00:13:01,640 required, then it should never be required, which makes a lot of sense. 226 00:13:01,640 --> 00:13:04,690 And I think that's part of the idea behind using default 227 00:13:04,719 --> 00:13:08,170 arguments and using overloading which we'll talk about next.