Default parameter: circle area

Description

Write a function with default value to find the circle area. Its prototype is:

double area(double r=1.0);

When the parameter r is specified when the function is called, the area of the circle with radius R is calculated; otherwise, the area of the circle with radius 1 is calculated.

Where, PI value is 3.14.

Input

A real number is the radius of a circle.

Output

The output has 2 lines. The first row is the area of the circle with the radius of the input value, and the second row is the area of the circle with the radius of 1.

Sample Input
19
Sample Output
1133.54
3.14
HINT

Append Code
append.cc,

int main()
{
    double r;
    cin>>r;
    cout<<area(r)<<endl;
    cout<<area()<<endl;
    return 0;
}

AC code

#include <iostream>

using namespace std;
double area(double r=1.0)
{
    return 3.14*r*r;
}
int main()
{
    double r;
    cin>>r;
    cout<<area(r)<<endl;
    cout<<area()<<endl;
    return 0;
}

Explanation of default parameters
1. When there are default parameters, if no arguments are provided, c + + will automatically take the default parameters as the values of the corresponding parameters.
For example:

#include <iostream>

using namespace std;
double area(double r=1.0)
{
    return 3.14*r*r;
}
int main()
{
    double r;
    cin>>r;
    cout<<area(r)<<endl;//The default value is invalid when an argument is provided during the call.
    cout<<area()<<endl;//When the call does not provide an argument, r=1 is used as the default value.
    return 0;
}

2. When specifying a default value in a parameter with multiple functions, all default parameters must appear to the right of the parameter without a default value.
For example:

    int f(int i,int j=2;int k=3);//Correct;
    int g(int i,int j=0;int k);//Error because k has no default parameter;
    int h(int i=1,int j,int k=0);//Error because j has no default value;

3. You can use expressions as default parameters, but local variables cannot.
For example:

#include <iostream>
#include <string>
using namespace std;

string name="tom";
double h=0.8,len=1.1;
void dog(string dogname=name,double high=h,double lenth=len)
{
    cout<<dogname<<endl<<h<<endl<<len<<endl;
}
int main()
{
    name="jack";//The global variable is modified here, and does not need to be redefined when modifying the global variable;
    double h=2.1;//This is a local variable, because h is redefined, local variable H is defined, and global variable H is hidden, but the value of global variable H is not affected;
    dog();
    return 0;
}


4. When calling a function with a default parameter value, if an argument has a default value, all arguments on the right side should have a default value.
For example:

   int f(int i=1,int j=2,int k=0)
    {
        return i+j+k;
    }
    f();//Correct; (1,2,0)
    f(3);//Correct, (3,2,0)
    f(2,3);//Correct, (2,3,0)
    f(4,5,6);//Correct, (4,5,6)
    f(,2,3);//Error, because i is default, but j and k are not default;

One must have a value, followed by a value, a default, followed by a default, which can be replaced by an argument by default

Posted by Megienos on Sat, 04 Apr 2020 11:38:23 -0700