Using same name for function and parameter

BLRCalvin
2010-02-23
2013-03-12
  • BLRCalvin

    BLRCalvin - 2010-02-23

    Hi,

    According to ANSI C standard, can a same name be used for both function and its parameter/ a variable in its scope?

    When I tested the following code with SDCC (v2.9 & snapshot #5703 ), I got error:

    int power(int number, char power){ //"power" is used as function name and parameter name
    char i;
    int tmp = 1;

    if(power == 0)
                                return 1;
    else if(power == 1)
          return number;

    for(i=1;i<=power;i++){
    tmp *= number; 
    }
    return tmp;
    }//End of function

    Error:

    Test_FuncName.c:15: error 98: conflict with previous definition of 'power' for attribute 'type'
    from type 'char'
    to type 'int function    ( int, char) '

    Command used to compile:

    sdcc -mmcs51 -model-small -S Test_FuncName.c

    SDCC versions used for testing:

    SDCC #5703
    SDCC : mcs51/gbz80/z80/ds390/pic16/pic14/TININative/ds400/hc08 2.9.7 #5703 (Feb 21 2010) (MINGW32)

    SDCC 2.9:
    SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.9.0 #5416 (Mar 22 2009) (MINGW32)

    Please provide your feedback.

    Thanks,
    Raghu.

     
  • Oliver Sedlacek

    Oliver Sedlacek - 2010-02-23

    In the C99 standard, section 6.2.1 treats all identifiers identically, whether they are variables of function names. As the function name is in scope when the parameter is declared, you definitely have a clash. I would avoid this practice anyway as I find it confusing.

     
  • Massimo Spataro

    Massimo Spataro - 2010-02-24

    Is not correct name and variable same.

    user _porwer for variable definition.

    int _power = 0;

    and power for function

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks