We were told that names of parameters are not compulsory in function declaration only their type is required. I tried writing a program without mentioning the type of parameter in function declaration and the program neither gives an error nor any warning. example of code So can we leave empty () in function declaration as we already mentioning name and type of parameter in the function definition?
Asked
Active
Viewed 459 times
-1
-
3Post the code as text, not as pictures. – Lundin Feb 10 '21 at 09:45
-
You can, but it's considered bad practice. The original version of C did not use parameter types in a function declaration; that was added with C89. The original syntax is still supported for backward compatibility's sake, but any new code *should* specify parameter types in both the declaration and definition. – John Bode Feb 10 '21 at 15:24
1 Answers
0
A function declaration like
int fun();
declares fun
as a function which returns an int
, and takes an indeterminate and unknown number of arguments of indeterminate and unknown type.
A complete declaration can only be deduced by the compiler with the first use of the function.
Of course the compiler won't be able to do error checking when you call the function, so in your example it's possible to call it using the wrong arguments:
fun("foobar", 123, 456.789, 'x');
The compiler will only report an error when it sees the full and complete declaration when you define (implement) the function later, as it won't match the deduced declaration from the call.

Some programmer dude
- 400,186
- 35
- 402
- 621
-
1More importantly, this form is formally obsolete since forever and should never be used. See 6.11.6. – Lundin Feb 10 '21 at 09:47
-
So if we keep bracket () empty in a function declaration , the compiler will assume type according to mentioned in Function defination, right? – ChirAdi Feb 10 '21 at 09:55
-
1@ChirAdi No, if no prototype format function is visible, the compiler will silently go haywire and apply something called "the default argument promotions", essentially taking a guess at what types the function expect. Then try to call the function, which in turn may or may not result in a crash. At best, there will be linker errors. At worst, everything will get built cleanly and you get strange run-time bugs. This is a huge flaw in the C language. Simply never use empty parenthesis and always prototype style declarations. – Lundin Feb 10 '21 at 10:04