Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 697
Tim Matthews Avatar asked Jul 20 '09 01:07

Tim Matthews


People also ask

How do you declare a function parameter?

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 .

What is the correct way of declaring a function?

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.

What is necessary after a function name *?

When functions are defined after main(): Need semicolon because because you are prototyping that function and telling the compiler that the function exits.

Can a function be called before it is declared?

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.


3 Answers

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

like image 117
Jeff Leonard Avatar answered Sep 27 '22 20:09

Jeff Leonard


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.

like image 41
Mike Mu Avatar answered Sep 27 '22 20:09

Mike Mu


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)

like image 37
Stefano Borini Avatar answered Sep 27 '22 21:09

Stefano Borini