c++ – What does it mean that a declaration shadows a parameter?

c++ – What does it mean that a declaration shadows a parameter?

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;
}

c++ – What does it mean that a declaration shadows a parameter?

#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 d = doublenumber(a);

cout << Double :  << d << endl;

return 0;
}

There are some problem with your code. Your declaration and definition of function dies not match. So remove declaration as no necessity of it.

You are declaring local x variable inside function which will shadow your function arguments.

Leave a Reply

Your email address will not be published.