views:

197

answers:

5
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?

+3  A: 

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)

Stefano Borini
+3  A: 

That's old-style C. It's seldom seen anymore.

Michael Kohne
+11  A: 

That is K&R C parameter declaration syntax, which is valid in ANSI C but not in C++.

Jeff Leonard
+1 Valid, but rare and not recommended. :)
280Z28
A: 

It's a function prototype. If you didn't do it this way you'd have to write the function out entirely before main, otherwise the compiler wouldn't know what the function was when you used it in main. It's not very descriptive, so it's not used anymore. You'd want to use something like:

int someFunction(int someParamX int someParamY);
mikeyickey
The example given in the question is actually a function *definition*, notice the lack of a semicolon after `int func(x)`.
Greg Hewgill
There is nothing that stops a function definition from having a prototype. But the shown function definition does not have a prototype. So -1 by me too
Johannes Schaub - litb
+2  A: 

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.

Mike Mu