Arguments that are not used

Discussion in 'C++' started by bg_ie@yahoo.com, Aug 17, 2006.

  1. Guest

    Lets say I have the following function -

    int mouseclick(int x, in y)
    {
    cout << "My x position"<<x;
    }

    I get a warning that I am not using y. What can I do to get rid of this
    warning without changing the function prototype?

    Thanks,

    Barry.
    , Aug 17, 2006
    #1
    1. Advertising

  2. wrote:
    > Lets say I have the following function -
    >
    > int mouseclick(int x, in y)


    change the above line to:

    int mouseclick(int x, int)

    > {
    > cout << "My x position"<<x;
    > }
    >
    > I get a warning that I am not using y. What can I do to get rid of this
    > warning without changing the function prototype?


    Make the above change and the warning should disappear.

    Best regards,

    Tom
    Thomas Tutone, Aug 17, 2006
    #2
    1. Advertising

  3. Howard Guest

    <> wrote in message
    news:...
    > Lets say I have the following function -
    >
    > int mouseclick(int x, in y)
    > {
    > cout << "My x position"<<x;
    > }
    >
    > I get a warning that I am not using y. What can I do to get rid of this
    > warning without changing the function prototype?
    >


    One way is to change the function (in the .cpp file) so that its signature
    is just

    int mouseclick( int, int )
    {
    ...
    }

    Another is to "use" those values, but don't do anything with them, like
    this:

    int mouseclick( int x, int y )
    {
    x;
    y;
    ...
    }

    Another (at least in some compilers) is to use a #pragma:

    int mouseclick( int x, int y )
    {
    #pragma unused x
    #pragma unused y
    ...
    }

    (I could have that syntax wrong. Since it's a pragma, it's probably not the
    most portable solution, anyway.)

    -Howard
    Howard, Aug 17, 2006
    #3
  4. Howard Guest

    "Howard" <> wrote in message
    news:2n1Fg.635792$...
    >
    > <> wrote in message
    > news:...
    >> Lets say I have the following function -
    >>
    >> int mouseclick(int x, in y)
    >> {
    >> cout << "My x position"<<x;
    >> }
    >>
    >> I get a warning that I am not using y. What can I do to get rid of this
    >> warning without changing the function prototype?
    >>

    >
    > One way is to change the function (in the .cpp file) so that its signature
    > is just
    >
    > int mouseclick( int, int )
    > {
    > ...
    > }
    >
    > Another is to "use" those values, but don't do anything with them, like
    > this:
    >
    > int mouseclick( int x, int y )
    > {
    > x;
    > y;
    > ...
    > }
    >
    > Another (at least in some compilers) is to use a #pragma:
    >
    > int mouseclick( int x, int y )
    > {
    > #pragma unused x
    > #pragma unused y
    > ...
    > }
    >
    > (I could have that syntax wrong. Since it's a pragma, it's probably not
    > the most portable solution, anyway.)
    >


    Oh, I just noticed you only wanted to ignore the y. Well, you get the
    idea... :)
    -H
    Howard, Aug 17, 2006
    #4
  5. Marcus Kwok Guest

    wrote:
    > Lets say I have the following function -
    >
    > int mouseclick(int x, in y)
    > {
    > cout << "My x position"<<x;
    > }
    >
    > I get a warning that I am not using y. What can I do to get rid of this
    > warning without changing the function prototype?


    I just comment the variable name in the function header:

    int mouseclick(int x, in /*y*/)
    {
    // implementation
    }

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Aug 17, 2006
    #5
  6. Gavin Deane Guest

    wrote:
    > Lets say I have the following function -
    >
    > int mouseclick(int x, in y)
    > {
    > cout << "My x position"<<x;
    > }
    >
    > I get a warning that I am not using y. What can I do to get rid of this
    > warning without changing the function prototype?


    The compiler is allowed to warn about anything it likes. The people who
    wrote your compiler clearly decided that they'd like to warn you about
    this particular situation. They also almost certainly provided you with
    a way to get rid of that warning if you want to. Your options will most
    likely be at least some of, in increasing order of preference IMO:

    Leave the warning there but ignore it (worst option).
    Turn down the global warning level of the compiler until this warning
    goes away.
    Turn off this particular warning in the compiler.
    Turn off this particular warning for that translation unit only.
    Turn off this particular warning for that piece of code only.
    Rewrite the code (best option if possible and applicable).

    For details of how to manage warning sensitivity in your particular
    compiler, consult its documentation or a forum dedicated to that
    compiler.

    Gavin Deane
    Gavin Deane, Aug 17, 2006
    #6
  7. posted:

    > Lets say I have the following function -
    >
    > int mouseclick(int x, int y)
    > {
    > cout << "My x position"<<x;
    > }
    >
    > I get a warning that I am not using y. What can I do to get rid of this
    > warning without changing the function prototype?



    If you have a very simple function, simply change it to:

    int mouseclick(int x,int)
    {

    }

    If, however, you've a larger function, one whose parameter list you don't
    want to change, then try this:

    int mouseclick(int x,int y)
    {
    (void)y;
    }

    The cast to void makes your intention clear to the compiler.

    --

    Frederick Gotham
    Frederick Gotham, Aug 17, 2006
    #7
  8. Greg Comeau Guest

    In article <>,
    <> wrote:
    >Lets say I have the following function -
    >
    >int mouseclick(int x, in y)
    >{
    > cout << "My x position"<<x;
    >}
    >
    >I get a warning that I am not using y. What can I do to get rid of this
    >warning without changing the function prototype?


    You could not give the parameter a name, but some compiler
    may still emit a diagnostic in such a case. You might also
    be able to get away with in this case perhaps uses such as
    'y = y;' or '(void)y;' but of course if possible get rid
    of the parameter.
    --
    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Aug 18, 2006
    #8
  9. Guest

    >>They also almost certainly provided you with
    a way to get rid of that warning if you want to.

    Thanks for your help guys. I think that its good that the compiler
    warns you and I think its better not to turn of the option. Of course,
    in some situations, you don't need to use all the params so its nice to
    know how to avoid the compiler warning for these situations.
    , Aug 18, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Diener
    Replies:
    14
    Views:
    4,889
    Josiah Carlson
    Apr 6, 2004
  2. Neo
    Replies:
    10
    Views:
    644
    sushant
    Jan 20, 2005
  3. sods
    Replies:
    4
    Views:
    3,231
  4. tutmann
    Replies:
    4
    Views:
    430
  5. jmborr
    Replies:
    1
    Views:
    401
    Stargaming
    Nov 3, 2007
Loading...

Share This Page