Tim Matthews
Tim Matthews

Reputation: 5121

Declaring function parameters after function name

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?

Upvotes: 5

Views: 590

Answers (5)

Mike Mu
Mike Mu

Reputation: 1007

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.

Upvotes: 5

mikeyickey
mikeyickey

Reputation: 37

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);

Upvotes: -8

Jeff Leonard
Jeff Leonard

Reputation: 3294

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

Upvotes: 12

Michael Kohne
Michael Kohne

Reputation: 12044

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

Upvotes: 4

Stefano Borini
Stefano Borini

Reputation: 143795

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)

Upvotes: 4

Related Questions