Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 8.1 and static functions
#5
According to my copy of K&R (half-way down section A7.3.2), "There are two styes in which functions may be declared. In the new style, the types of parameters are explicit and are part of the type of the function; such a declaration is also called a function prototype."

Now, a function definition is a declaration followed by compound-statement, so ISTM that while a definition like
Code:
static void foo() {...}
is a function with no prototype, the alternative definition
Code:
static void foo(void) {...}
is a function *with* a prototype. For calls to this function, made later in the same translation unit, the prototype is visible at the function call. If anyone adds a call above the function definition, that would be a violation and your checker tool and/or compiler should complain at you.

You can have multiple declarations for an object, so
static void foo(void);
...
static void foo(void) {...}
is legal. If the function was not static, and calls to the function are made from other translation units, then obviously the extra prototype is needed and has to be in the header file that is common to both the file defining the function and the file using it. Again, if the header is absent, I get a warning which I fix. For static functions, defining every static function as
Code:
static void foo(void);
static void foo(void)
{
  ...
}
seems a bit pointless. But if that's necessary to make Lint general enough to check the other case (where the prototype is in a header file) then I don't mind doing it.
<t></t>
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)