int func(x)
int x;
{
.............
What is this kind of declaration called?
When is it valid/invalid including C or C++, certain standard revisions and compilers?
In a function declaration, or prototype, the type of each parameter must be specified. In the function definition, the type of each parameter must also be specified. In the function definition, if the type of a parameter is not specified, it is assumed to be int .
Function declaration. A function declaration is made of function keyword, followed by an obligatory function name, a list of parameters in a pair of parenthesis (para1, ..., paramN) and a pair of curly braces {...} that delimits the body code.
When functions are defined after main(): Need semicolon because because you are prototyping that function and telling the compiler that the function exits.
However, functions go out of this route as they are read first, but only the function head (= first line). That is why function calls are possible before declaration.
That is K&R C parameter declaration syntax, which is valid in ANSI C but not in C++.
It's still valid, but it's pre-ANSI. That's actually where the K&R indent style got its name. The opening bracket is on the line after the function block because this looks weird:
int func(x)
int x; {
...
}
Anyway, this style is not recommended because of a problem with function prototypes.
K&R style, and I think it's still valid, although discouraged. It probably came from Fortran (where function parameters types are defined inside the function body still in the recent F95)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With