Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a function with no parameters (compared to the actual function definition) compile?

People also ask

What happens when you create a function without any parameters?

Thus, if you declare a parameter and a call is made without any parameter value supplied, the parameter will be undefined .

Can a function be defined without parameters?

Answer : A function cannot be defined without parameters.

Why does a function need parameters?

Parameters are essential to functions, because otherwise you can't give the function-machine an input.

What happens when you create a function without any parameters in C?

In C, void no_args() declares a function that takes an unspecified (but not variable) number of parameters (and returns nothing). So all your calls are valid (according to the prototype) in C. In C, use void no_args(void) to declare a function that truly takes no parameters (and returns nothing).


All the other answers are correct, but just for completion

A function is declared in the following manner:

  return-type function-name(parameter-list,...) { body... }

return-type is the variable type that the function returns. This can not be an array type or a function type. If not given, then int is assumed.

function-name is the name of the function.

parameter-list is the list of parameters that the function takes separated by commas. If no parameters are given, then the function does not take any and should be defined with an empty set of parenthesis or with the keyword void. If no variable type is in front of a variable in the paramater list, then int is assumed. Arrays and functions are not passed to functions, but are automatically converted to pointers. If the list is terminated with an ellipsis (,...), then there is no set number of parameters. Note: the header stdarg.h can be used to access arguments when using an ellipsis.

And again for the sake of completeness. From C11 specification 6:11:6 (page: 179)

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.


In C func() means that you can pass any number of arguments. If you want no arguments then you have to declare as func(void). The type you're passing to your function, if not specified defaults to int.


int func(); is an obsolescent function declaration from the days when there was no C standard, i.e. the days of K&R C (before 1989, the year the first "ANSI C" standard was published).

Remember that there were no prototypes in K&R C and the keyword void was not yet invented. All you could do was to tell the compiler about the return type of a function. The empty parameter list in K&R C means "an unspecified but fixed" number of arguments. Fixed means that you must call the function with the same number of args each time (as opposed to a variadic function like printf, where the number and type can vary for each call).

Many compilers will diagnose this construct; in particular gcc -Wstrict-prototypes will tell you "function declaration isn't a prototype", which is spot on, because it looks like a prototype (especially if you are poisoned by C++!), but isn't. It's an old style K&R C return type declaration.

Rule of thumb: Never leave an empty parameter list declaration empty, use int func(void) to be specific. This turns the K&R return type declaration into a proper C89 prototype. Compilers are happy, developers are happy, static checkers are happy. Those mislead by^W^Wfond of C++ may cringe, though, because they need to type extra characters when they try to exercise their foreign language skills :-)


  • The empty parameter list means "any arguments", so the definition isn't wrong.
  • The missing type is assumed to be int.

I would consider any build that passes this to be lacking in configured warning/error level though, there's no point in being this allowing for actual code.


It's K&R style function declaration and definition. From C99 Standard (ISO/IEC 9899:TC3)

Section 6.7.5.3 Function Declarators (including prototypes)

An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied. (If both function types are "old style", parameter types are not compared.)

Section 6.11.6 Function declarators

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

Section 6.11.7 Function definitions

The use of function definitions with separate parameter identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.

Which the old style means K&R style

Example:

Declaration: int old_style();

Definition:

int old_style(a, b)
    int a; 
    int b;
{
     /* something to do */
}

C assumes int if no type is given on function return type and parameter list. Only for this rule following weird things are possible.

A function definition looks like this.

int func(int param) { /* body */}

If its a prototype you write

int func(int param);

In prototype you can only specify the type of parameters. Parameters' name is not mandatory. So

int func(int);

Also if you dont specify parameter type but name int is assumed as type.

int func(param);

If you go farther, following works too.

func();

Compiler assumes int func() when you write func(). But dont put func() inside a function body. That'll be a function call


As stated @Krishnabhadra, all previous responses from other users, have a correct interpretation, and I just want to make a more detailed analysis of some points.

In the Old-C as in ANSI-C the "untyped formal parameter", take the dimencion of your work register or instruction depth capability (shadow registers or instruction cumulative cycle), in an 8bit MPU, will be an int16, in a 16bit MPU and so will be an int16 an so on, in the case 64bit architectures may choose to compile options like: -m32.

Although it seems simpler implementation at high level, For pass multiple parameters, the work of the programmer in the control dimencion data type step, becomes more demanding.

In other cases, for some microprocessors architectures, the ANSI compilers customized, leveraged some of this old features to optimize the use of the code, forcing the location of these "untyped formal parameters" to work within or outside the work register, today you get almost the same with the use of "volatile" and "register".

But it should be noted that the most modern compilers, not make any distinction between the two types of parameters declaration.

Examples of a compilation with gcc under linux:

main.c

main2.c

main3.c  
In any case the statement of the prototype locally is of no use, because there is no call without parameters reference to this prototype will be remiss. If you use the system with "untyped formal parameter", for an external call, proceed to generate a declarative prototype data type.

Like this:

int myfunc(int param);