Default arguments and omission of arguments

In this C ++ tutorial, we will learn what a default argument is, what it is for, and how to use it, as well as omitting an argument in a function call.

Default Argument

We have already learned how to send information to functions by using parameters and arguments.
Suppose we want to add two numbers, a and b, the function would be:
float sum2(float a, float b)
{
    return a+b;
}
Now suppose we want to calculate the sum of three variables, we would have to do a function like this:
float sum3(float a, float b, float c)
{
    return a+b+c;
}
Note that we would have to use another function, with another name, for the same purpose: adding the arguments. Not very smart, do you agree?
It would be nice if the same function added 2 or 3 arguments as much as the user wants.

To add two numbers, it would be interesting to do: sum (1,2);
To add three numbers, we would do: sum (1,2,3);

This is where the concept of standard argument comes in.
Simply declare the prototype of the function above:
  • float sum(float a, float b, float c = 0.0);

And its scope:
float sum(float a, float b, float c = 0.0)
{
    return a+b+c;
}
What happens is as follows:
The default value of c is 0.0

If you do: sum (1,2,3), the value of c will be 3.

If you do: sum (1,2), you will not be setting value for c, so it will default to 0, got it? Argument with default value if you do not provide this value.

Test:
#include <iostream>
using namespace std;

float sum(float a, float b, float c = 0.0)
{
    return a+b+c;
}

int main()
{
    cout<<sum(1,2)<<endl;
    cout<<sum(1,2,3)<<endl;

    return 0;
}
It works for 2 or 3 variables, just like the way you want it!

Arguments omission

The previous code works to add 2 or 3 numbers.
To add 2, 3 or 4 numbers, we could do:
float sum(float a, float b, float c = 0.0, float d = 0.0)
{
    return a+b+c+d;
}
Now you can do:

sum (1,2);
sum (1,2,3);
sum (1,2,3,4);

In the first case, we omitted argument c and d.
In the second example, we omitted the argument d.
In the last example, we did not omit any arguments.

That is, default arguments are automatically replaced when we enter the arguments.

Rules in Using Default Arguments

When we enter and pass arguments to a function, they are copied from left to right.

For example: sum (1,2,3)
1 goes to 'a', 2 goes to 'b' and value 3 goes to parameter 'c'. The value of the argument 'd', then, is the default argument, which we set to 0.

Another rule is that once we use a default argument in one parameter, all other subsequent parameters must also have default arguments as well. For example, the following function prototype is valid:

  • float volume (base float, float height = 1, float width = 1);

'height' is a default argument, and the following as well.

The following function prototype is invalid:

  • float volume (float height = 1, base float, float width = 1);

Since the first argument is default, all others must be, and 'base' is a normal parameter without default argument, which must necessarily be provided by the function call.

Another point is that parameters with default arguments must be declared on the first occurrence of the function declaration. That is, if you use a prototype of a function and then elsewhere you will define the scope of your function, the default arguments must already be defined in the prototype, which will come before. For example, you might even abbreviate it like this:

  • Prototype: double area (double = 1.0, float 2.0);
  • Function definition: double area (double length, float witdh) {return length * width; }

So, to recap, be the function:
double area (double length = 1.0, float witdh = 2.0) 
{ 
   return length*width; 
} 
If we make the following function calls:

  • area() - values 1.0 and 2.0 will be used to calculate the area respectively, ie we are using the two default arguments, no one is overwritten.
  • area(3) - values 3 and 2.0 will be used to calculate the area, ie the first default argument has been overwritten with 3
  • area(3,6) - values 3 and 6 will be used to calculate the area, ie the two default arguments have been overwritten.

Let's see this in practice? Test the following code:
#include <iostream>
using namespace std;

void area(float = 1.0, float = 1.0);

int main()
{
    cout << "No argument passed:"<<endl;
    area();

    cout << "\nFirst argument passed:"<<endl;
    area(2.0);

    cout << "\nBoth argument passed:"<<endl;
    area(2.0, 3.0);

    return 0;
}

void area(float length, float width)
{
    cout << "Area: " << length * width << endl;
}



Everything worked? Yes? So let's follow in our C++ tutorials about functions.

No comments:

Post a Comment