I am trying to make a function that returns double the integer number that I will pass to it. I am getting the following error message with my code:
declaration of 'int x' shadows a parameter int x; "
Here is my code:
#include <iostream>
int doublenumber();
using namespace std;
int doublenumber(int x)// <-- this is the function which returns double the value .
{
int x;
return 2 * x;
cout << endl;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin >> a;
doublenumber(a);
return 0;
}
Declaration Shadows a Parameter Error in C++A variable defined inside an if-else block, function, or a class cannot be used unless and until you have defined it as public . And a public variable, function, or class can be accessed throughout the program.
Shadowing is a computer programming phenomenon in which a variable declared in one scope (like decision block, method, or inner class) has the same name as another declaration of the enclosing scope. In this case, the declaration shadows the declaration of the enclosing scope.
In computer programming, variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope.
You have x
as a parameter and then try to declare it also as a local variable, which is what the complaint about "shadowing" refers to.
I did it because your advice was so helpful, and this is the final result :
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int n= doublenumber(a);
cout << "the double value is : " << n << endl;
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With