MISRA.FUNC.NO_PARAMSFunction without parameters is missing void parameter type. MISRA C 2012 Rule 8.2: Function types shall be in prototype form with named parametersC90 [Undefined 22-25], C99 [Undefined 36-39, 73, 79] Category: Required Analysis: Decidable, Single Translation Unit Applies to: C90, C99 RationaleThe early version of C, commonly referred to as K&R C [30], did not provide a mechanism for checking the number of arguments or their types against the corresponding parameters. The type of an object or function did not have to be declared in K&R C since the default type of an object and the default return type of a function was int. The C90 standard introduced function prototypes, a form of function declarator in which the parameter types were declared. This permitted argument types to be checked against parameter types. It also allowed the number of arguments to be checked except when a function prototype specified that a variable number of arguments was expected. The C90 standard did not require the use of function prototypes for reasons of backward compatibility with existing code. For the same reason, it continued to permit types to be omitted in which case the type would default to int. The C99 standard removed the default int type from the language but continued to allow K&R-style function types in which there was no means to supply parameter type information in a declaration and it was optional to supply parameter type information in a definition. The mismatch between the number of arguments and parameters, their types and the expected and actual return type of a function provides potential for undefined behaviour. The purpose of this rule along with Rule 8.1 and Rule 8.4 is to avoid this undefined behaviour by requiring parameter types and function return types to be specified explicitly. Rule 17.3 ensures that this information is available at the time of a function call, thereby requiring the compiler to diagnose any mismatch that is detected. This rule also requires that names be specified for all the parameters in a declaration. The parameter names can provide useful information regarding the function interface and a mismatch between a declaration and definition might be indicative of a programming error.
Note: An empty parameter list is not valid in a prototype. If a function type has no parameters its prototype form uses the keyword void.
ExampleThe first example shows declarations of some functions and the corresponding definitions for some of those functions. /* Compliant */ extern int16_t func1 ( int16_t n ); /* Non-compliant - parameter name not specified */ extern void func2 ( int16_t ); /* Non-compliant - not in prototype form */ static int16_t func3 ( ); /* Compliant - prototype specifies 0 parameters */ static int16_t func4 ( void ); /* Compliant */ int16_t func1 ( int16_t n ) { return n; } /* Non-compliant - old style identifier and declaration list */ static int16_t func3 ( vec, n ) int16_t *vec; int16_t n; { return vec[ n - 1 ]; } This example section shows the application of the rule to function types other than in function declarations and definitions. /* Non-compliant - no prototype */ int16_t ( *pf1 ) ( ); /* Compliant - prototype specifies 0 parameters */ int16_t ( *pf1 ) ( void ); /* Non-compliant - parameter name not specified */ typedef int16_t ( *pf2_t ) ( int16_t ); /* Compliant */ typedef int16_t ( *pf3_t ) ( int16_t n ); MISRA-C 2004 Rule 16.5 (required): Functions with no parameters shall be declared and defined with the parameter list void.[Koenig 59—62] Functions shall be declared with a return type (see Rule 8.2), that type being void if the function does not return any data. Similarly, if the function has no parameters, the parameter list shall be declared as void. Thus for example, a function, 'myfunc', which neither takes parameters nor returns a value would be declared as: Examplevoid myfunc ( void ); |