What Does It Mean That "A Declaration Shadows a Parameter"?

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

3 Answers

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

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest