Re: I think this is a disadvantag

Discussion in 'C Programming' started by Keith Thompson, Jun 16, 2013.

  1. "paskali" <> writes:
    > On the general rules i think a thing like this is not possible:
    >
    > int sum;
    >
    > ....
    >
    > int sum(int num) {
    >
    > .....
    >
    > return sum;
    >
    > }
    >
    > C does not permit (in my case, at least) to use the same name of a
    > variable in a function, in the case i have the needing to be the
    > first a global item. It does not seem to know the difference between
    > the action "to sum" (a function) and the result "the sum" (a variable).
    >
    > Is this a limit of my compiler?


    C does not have overloading, except in some very limited ways. You
    can't define two entities with the same name in the same scope.

    The name of a variable, or of a function, can be used as an expression.
    Any expression can be used as a statement if you append a semicolon to
    it. So this:

    {
    int sum = 0;
    sum;
    }

    is a legal, but not very useful, code fragment (the value of sum is
    evaluated and discarded).

    So if your declarations above, defining sum both as an int object and as
    a function were legal, then this:

    int main(void) {
    sum;
    }

    would be ambiguous; there would be no way to decide which "sum" the name
    refers to. That's why multiple declarations like that are illegal.

    And I'd say it's a good thing that it's illegal. What's confusing to
    the compiler is likely to be confusing to the reader. If, for example,
    the compiler allowed multiple declarations with the same name, and then
    permitted only unambiguous references, that would impose extra work on
    anyone reading your code to figure out which "sum" you're referring to.

    Picking distinct names for distinct entities makes your code legal *and*
    more readable.

    > Some time i play with an old (prehistoric/dinosaur) compiler (k&r 1st
    > version) that make me able to do as wrote, of course in this form:
    >
    > int sum;
    > ...
    > int sum(num) int num; {
    > .....
    > return sum;
    > }


    If you say some old compiler accepts that, I'll take your word for it,
    but I'm a bit surprised. I would have thought that even K&R1 wouldn't
    permit that. Perhaps the compiler you're using is just failing to catch
    the error.

    > I think that does not lack on readability and clarity.


    I think it does.

    > On the modern compiler i have always to make a solution like this:
    >
    > int sum;
    >
    > ....
    >
    > int sum_num(int num) {
    >
    > .....
    >
    > return sum;
    >
    > }
    >
    >
    > What do you think about?


    I think that "sum_num" isn't a very good name, but there are better ways
    to write a function that adds a series of numbers. A good starting
    point would be to define how the function gets access to the sequence.
    Once you've done that, it's likely that the function and the data it
    operates on will naturally have different names.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 16, 2013
    #1
    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. Preben Holm
    Replies:
    2
    Views:
    547
    Preben Holm
    Apr 28, 2005
  2. Replies:
    0
    Views:
    580
  3. Replies:
    1
    Views:
    848
    Jack Klein
    Apr 13, 2005
  4. David Mark
    Replies:
    17
    Views:
    211
    David Mark
    Mar 23, 2010
  5. Xavier Roche

    Re: I think this is a disadvantag

    Xavier Roche, Jun 15, 2013, in forum: C Programming
    Replies:
    36
    Views:
    638
    Tim Rentsch
    Jul 5, 2013
Loading...

Share This Page