Default Argument and Unary Scope Resolution in C++

Default Argument
Unary Scope Resolution Operator

Default Argument

It is not uncommon for a program to invoke a function repeatedly with the same argument value for a particular parameter.In such cases, the programmer can specify that such an argument is a default argument

,and the programmer can provide a default value for that argument.When a program omits a default argument in a function call, the compiler rewrites the function call and inserts the default value of that argument to be passed as an argument to the function call.
Default argument must be the rightmost(trailing) arguments in a funcion’s parameter list.When one is calling a function with two or more default arguments, if an omitted argument is not the rightmost argument in the argument list, then all arguments to the right of that argument also must be omitted.Default arguments should be specified with the first occurrence of the function name-typically, in the function prototype.Default values can be constants, global variables or function calls.Default arguments also can be used with inline functions.

Program of Default Argument in C++

#include<iostream>
using std::cout;
using std::endl;
int boxVolume(int length=1,int width=1,int height=1);
int main()
{
cout<<"The default box volume is:"<<boxVolume();
cout<<"/n/nThe volume of a box with length 10,\n"
    <<"width 1 and height 1 is:"<<boxVolume(10);
cout<<"/n/nThe volume of a box with length 10,\n"
    <<"width 5 and height 1 is:"<<boxVolume(10,5);
cout<<"/n/nThe volume of a box with length 10,/n"
    <<"width 5 and height 2 is:"<<boxVolume(10,5,2)
    <<endl;
return 0;
}
int boxVolume(int length,int width,int height )
{
return length*width*height;
}

OUTPUT:

The default box volume is:1

The volume of a box with length 10,
width 1 and height 1 is:10

Unary Scope Resolution Operator

It is possible to declare local and global variables of the same name.C++ provides that unary scope resolution operator(: :) to access a global variable when a local variable of the same name is in scope. The unary scope resolution operator cannot be used to access a local variable of the same name in an outer block. A global variable can be accessed directly without the unary scope resolution operator if the name of the global variable is not the same as the name of a local variable in scope.

Program of Unary Scope Resolution Operator

#include<iostream>
using std::cout;
using std::endl;
#include<iomanip>
using std::setprecision;
const double PI=3.14159265358979
int main()
{
const float PI=static_cast<float>(::PI);
cout<<setprecision(20)
    <<"Local float value of PI="<<PI
    <<"/nGlobal double value of PI="<<endl
return 0;
}

OUTPUT:

Local float value of PI=3.1415927410125732
Global double value of PI=3.14159265358979

Using the unary scope resolution operator(: :) with a given variable name is optional when the only variable with that name is a global variable

It is an error to attempt to use the unary scope resolution operator(: :) to access a non-global variable in an outer block-it is a syntax error if no global variable with that name exists; it is a logic error if a global variable with that name exists(because the unary scope resolution operator will cause the program to refer to the global variable when, in fact, you are trying to improperly access the non-global variable in the outer block)

Always using the unary scope resolution operator(: :) to refer to global variables makes programs clearer, easier to modify and avoids subtle errors.

Translate »