In this video, we'll learn how we can call c++ functions
using default argument values.
As we've seen, when we call a function, we must provide all
the arguments that, that function requires, and they must be in the
right order and of the right type.
But sometimes when we call functions some of the argument
values that we pass in tend to be the same values most of the time.
For example, if we have a function that calculates the cost
of an item, we can provide the function with the base cost of
the item and the sales tax rate.
Suppose that 98% of our customers live in a region where the tax rate is 6%,
that means that we have to provide the tax rate in every function call even
though it will almost always be 6%.
C++ allows us to provide default values for arguments.
So in the case of the sales tax, if we omit the argument from the
function call, then the c++ compiler will automatically replace
it with the default value of 6%.
However, if we have a customer whose sales tax rate is 8%, then
we can provide it explicitly and override the default value.
As we'll see in the next few slides, we can add default arguments to
the function prototype or the function definition but not both.
But best practice is to do it in the function prototype.
Default arguments must appear at the tail end of the parameter list.
We can also have multiple default arguments.
Let's see an example that demonstrates default arguments.
In this example, we have a function with no default arguments.
In the next slide, we'll modify it so it does have a default argument.
The function is called calculate cost and it expects the base
cost of an item and the tax rate.
The function calculates the tax amount and adds that to the base costs and
returns that value to the caller.
In the main function, we're calling the function with 100.0
as the base cost and 0.06 or 6% percent as the tax rate.
In this case, 106.0 is returned from the function and assigned to cost.
That's pretty easy.
But if 98% of our customers will have a 6% tax rate, I'm forced to supply
this every time I call the function.
Why not let the compiler default the tax rate to 6%
unless I tell it otherwise.
Let's do that in the next slide.
Here, we have the same function except that the function prototype
has been modified to provide a default value for the tax rate.
Notice that the parameter tax rate in the function
prototype now has the default initialization to 0.06 added to it.
This effectively tells the compiler that if this argument
is not provided by the function call, then use the value 0.06
for tax rate, simple as that.
Now notice the two calls in the main function.
The first call simply calls calc cost with 200.0 which is the base cost.
Since the argument for tax rate is not provided, the compiler
will use 6% for the tax rate.
In the second call, we are providing the tax rate.
In this case, the compiler will use that and ignore the default value.
This can be pretty handy.
Suppose we have a function that prints documents and we have to
supply the document name, the printer to print to, the paper size,
the resolution and so forth.
Again, most of the time we print to the default printer
with default settings.
But sometimes, we need higher resolution or a different
printer, you get the idea.
In this case, we can provide multiple default parameters for
everything except the document name which is always required.
This makes writing the code much simpler.
Let's add another default parameter to the calc cost function.
In this example, we'll calculate the cost of an item based on the
base cost of the item, the sales tax and the shipping charge.
As before, most of the times the sales tax will be 6%.
And in this example, the standard typical shipping charge is $3.50.
So most customers will have a 6% tax rate and a $3.50 shipping charge.
We can easily add a second default argument to this function.
Notice that in the function prototype, we added another
parameter named shipping and we default this value to $3.50.
The function body now calculates the tax for the item, adds the
shipping charge, adds this to the original cost of the item and
returns the result to the caller.
In the main function, we're calling calc cost in three different ways.
In the first call, we're providing all three arguments to the function.
In this case, no default values will be used and the tax rate will
be 8% and shipping will be $4.25.
In the second call, we're providing the cost of the
item and the tax rate of 8%.
In this case, the default value for shipping will be used.
But the tax rate will use the 8% we provided.
In the final example, we're only providing the cost of the item.
So in this case, both default values are used.
The tax rate will default to 6% and the shipping charge to $3.50.
As you can see, default argument values give us the ability to
make our code less verbose and potentially prevent errors by using
default values rather than having to supply all of the arguments
to the function all the time.
In the next video, we'll learn about function overloading.
Before we talk about function overloading in the next video,
let me go over default arguments.
I'm in the section 11 workspace in the default arguments project.
And this is the same example you saw on the slides.
Here, I've got a function called calculate cost that expects $3.
The base cost, a tax rate, which is a default argument at 0.06 or 6% and
the default shipping which is $3.50.
Now if those arguments are not supplied, then the defaults kick in.
It's really pretty simple.
And here's the calculation.
Notice, I don't have to include the function default arguments
here in the function definition, only in the function prototype and
that's usually where you see them.
So the code here says base cost plus equals base cost times the tax rate,
so I'm figuring out what's my tax.
And I'm adding the shipping to that and then I'm adding
the whole thing to base cost.
So I'm not taxing the shipping.
And then we've got some sample calls.
In this first example, I'm calling calculate costs
with all three arguments.
So in this case, none of the defaults will be used.
I'll use 100 for the base cost, I'll use 8% for the tax rate and I'll use
four $4.25 for the shipping charge.
I expect to get back 112 and 25.
For this example, here, I'm providing the first two arguments,
and I'm not providing the shipping.
So in this case, it's going to use a 100 as the base cost, 8%
for the tax rate and it'll default to 350 here for the shipping.
And then this last example,
I'm supplying the base cost only.
So in this case, 6% and %3.50 are what're going to be used.
So if I run this program, you'll see that our results are
exactly it was as we expected: 112.25, 111.50 and 215.50.
Okay, so you can see default arguments are pretty easy to use.
I could also default the base cost if I wanted to.
I could say that the base cost is a 100. Maybe that's the typical
item that most people order.
So in this case, I could just simply just say cost is calc cost, right,
with nothing in the parentheses.
So I'm not supplying any arguments here.
That's it.
So what's going to happen here is it's going to use
all three default arguments.
So let me print that out real quick.
So in this case, we expect the same result as -- well, if we would have
done 106, so we'd have 100 is the base cost that's being used by default.
We're using a 6% tax rate so that's another $6 and
our shipping would be 3.50.
So in this case, we expect 109.50.
So let's give that a shot, and that's what we're getting here, 109.50.
This is pretty straightforward.
The only rule with default arguments is that once you create a parameter
that has a default value, then you've got to default the rest of them.
So in other words, I can't say that base cost is 100 and then
not have one here because that's going to give me an error, right.
Once you start, they all have to be on the tail end.
So in this case, I could just say 0.06.
So that's really the only rule there.
And I'll run that again just to make sure I've got no
errors, and there you go.
So let's add another default type function to this application.
How about a greeting function?
So let's do something like this.
Let's create a function prototype here for a function
called greeting that expects a string, which is someone's name.
Now let me include string because I need the library.
So that's a string name.
Perfect.
So that would be Frank Metropoulos, Joe Miller, whatever you want.
Then I've got another string, which is the prefix that we
want in front of their name.
Mr., Dr., Mrs., whatever you like.
And I'm going to prefix it to Mr.
Let's say that most of my customers are male.
And I also want a suffix.
And in this case, the suffix I'm going to default it
to just the empty string.
Okay, so that's my function prototype.
Let's implement this function now.
We'll implement it right down here.
And at this point, we don't need those default arguments.
We can just get rid of them from here, and it cleans
up the code a little bit.
All right, so what do we want to do here.
We just really want to display some stuff, right.
So let's just say I want an output statement, and I'm going to say hello,
followed by a space, followed by the prefix and what we could do here
is we could append a space, right.
We can use concatenation here, and the name and another space and the suffix.
Okay, so we're just using concatenation here, just like
we've learned in string class.
And then we'll use the endline to finish it off.
Okay, so let's run that, and give it a few example runs here.
So let's say how about -- let's do a really simple one, how about greeting.
And I'm going to supply all of these arguments.
So I'm going to say Glenn Jones is the name.
Okay, the prefix will be Dr.
and the suffix will be M.D.
Okay, so if we run that, we expect this to display Dr Glenn Jones M.D.
Okay, so let's give it a shot.
And I forgot the semicolon.
I'll fix that real quick.
And there you go.
Hello Dr Glenn Jones M.D.
Perfect, now let's run some of the other examples.
So how about we do something like greeting James Rogers.
How about professor and Ph.D for the suffix.
Okay.
We'll run that, and now we'll say hello Professor James
Rogers Ph.D, pretty easy.
Now let's use some defaults.
In this case, we'll say greeting, let's say, Frank Miller.
And Dr.
No suffix in this case.
So it'll default to the blank or the empty space.
So in this case, it'll just say Dr Frank Miller, which is what I want.
And then let's do one more, and we'll just say greeting,
let's say, William Smith.
So in this case, it's going to use all the default values.
So it's going to use Mr William Smith followed by the empty
string right there for the suffix.
Okay, so in this case, we're just going to see Mr William Smith.
There you go.
If we have Mary Howard, and she would be Mrs.
And we can give her a title as well, whatever we like.
There you go.
So now we've got Mrs Mary Howard Ph.D instead of Dr.
We could have said doctor here as well.
You can see all the different combinations.
This can come in pretty handy.
I used to work with someone who used to say that if something is always
required, then it should never be required, which makes a lot of sense.
And I think that's part of the idea behind using default
arguments and using overloading which we'll talk about next.