Different Values For Default Arguments in Different Scopes

Different Values For Default Arguments in Different Scopes

In the same scope, it’s not possible to have different values for default arguments. For example, this would be an error:

 void foo(int i = 0){    cout << i;}void foo(int = 1);//error

But default arguments can indeed take different values in different scopes. For example:

 int Func(void){    foo(int = 1);//ok, 1 will be used as the default value in the scope ofthis function}

To enforce this, a parameter must be supplied to the function. Declare the function without a default parameter:

 int Func(void){    foo(int);//disable the default parameter.    foo(1);//ok.    foo();//error, function foo() takes one parameter.}

The opposite is also true--a function can be declared to take adefault parameter even if it’s not defined that way.

 void foo(int i){	cout << i;}int Func(){	void foo(int = 1);//enable default parameter	foo();//ok.}

See also  Is Machine Learning Automating Creativity in Graphic Design?

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist