const parameters

Discussion in 'C Programming' started by gc, Nov 7, 2003.

  1. gc

    gc Guest

    I came across certain code with lot of declarations like:
    int foo(const double bar,const int m);
    I do not see any point in making a non pointer parameter a const, or
    am I missing something?
     
    gc, Nov 7, 2003
    #1
    1. Advertising

  2. gc

    Anupam Guest

    (gc) wrote in message news:<>...
    > I came across certain code with lot of declarations like:
    > int foo(const double bar,const int m);
    > I do not see any point in making a non pointer parameter a const, or
    > am I missing something?

    Why not? I would say that it is definitely to the point if you can
    say to the compiler that you will *not* be changing bar or m inside
    the function foo. Ok, this value will not propagate to the caller,
    which is why making a pointer, a constant is a good idea. However if
    you are sure that your function should not manipulate these values
    inside the function, its a good idea to give yourself a compile time
    escape route.
    For instance :

    <code excerpt>
    void foo(int bar[],const int noElements) {
    unsigned int i;
    for(i=0;i<noElements;i++) }
    /* blah blah... whatever you want to do with bar */
    /* What would happen if you inadvertently put a noElements--
    around here */
    }
    }

    It is a good idea to make a non pointer parameter const for this
    reason.
    Regards,
    Anupam
     
    Anupam, Nov 7, 2003
    #2
    1. Advertising

  3. "gc" <> wrote in message
    news:...
    > I came across certain code with lot of declarations like:
    > int foo(const double bar,const int m);
    > I do not see any point in making a non pointer parameter a const, or
    > am I missing something?


    In the function's implementation, it makes just as much sense
    as making any other local variable 'const'.

    However, the 'const' is irrelevant in the interface of a function.
    And the compiler knows this: the following declaration and definition
    are recognized by the compiler as referring to the same function:

    // file.h
    int foo(double bar,int m);

    // file.cpp
    int foo(const double bar,const int m);


    This is the style I typically use - keep the details away from
    the user's eyes (in the header), but use const whenever it can
    help code maintenance...


    hth - Ivan
    --
    http://ivan.vecerina.com
     
    Ivan Vecerina, Nov 7, 2003
    #3
  4. gc

    rihad Guest

    On 6 Nov 2003 19:15:12 -0800, (gc) wrote:

    >I came across certain code with lot of declarations like:
    >int foo(const double bar,const int m);
    >I do not see any point in making a non pointer parameter a const, or
    >am I missing something?


    It just means that the implementor of foo() cannot modify their copy of passed
    parameters `bar' and `m' inside foo(). It means nothing to the user of foo().
     
    rihad, Nov 7, 2003
    #4
  5. gc <> spoke thus:

    > I came across certain code with lot of declarations like:
    > int foo(const double bar,const int m);


    It's a way of reducing the number of errors you produce. Parameters are
    passed by value, so changing them never affects anything outside the function.
    Declaring parameters const is a way of forcing yourself to remember this.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Nov 7, 2003
    #5
  6. gc

    Ben Pfaff Guest

    rihad <> writes:

    > On 6 Nov 2003 19:15:12 -0800, (gc) wrote:
    >
    > >int foo(const double bar,const int m);
    > >I do not see any point in making a non pointer parameter a const, or
    > >am I missing something?

    >
    > It just means that the implementor of foo() cannot modify their copy of passed
    > parameters `bar' and `m' inside foo().


    It doesn't even mean that, because the function's implementation is
    free to omit the const qualifiers regardless of whether they are
    included in this prototype.

    > It means nothing to the user of foo().


    --
    "The expression isn't unclear *at all* and only an expert could actually
    have doubts about it"
    --Dan Pop
     
    Ben Pfaff, Nov 7, 2003
    #6
    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. Replies:
    11
    Views:
    1,143
  2. Javier
    Replies:
    2
    Views:
    602
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,158
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    923
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    552
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page