tags:

views:

246

answers:

3

Hi,
I'm learning C and I saw in a book that a function prototype has the form void f() and in the function declaration or in the calling function, the f function takes arguments.
Thus In the function declaration we have something like void f(long double y[], long double A) and in the calling function is f(y, A).
The function is doing operations on the array y i.e. when the function is called, some elements in the array y are changing. A is just a constant numerical value that doesn't change. I have two questions:

  1. If defining the function prototype at the top in the program as void f() a good practice? Or is it better to put it as void f(long double y[], long double A) as in the function declaration?

  2. The called function f is changing elements in the array y. Is void the right return type? The program is working fine as such with the void as described.
    Or should I change all my "voids" to "long double".
    I'm working with long double as I need as much precision as possible though on my machine both double and long double gives me 15 precision digits.

Thanks a lot

+2  A: 

No it isn't good to have a declaration with unspecified arguments. In C (but not in C++) it is allowed to declare a function as f() without specifying the argument, but using that functionality should be avoided. If the function does not accept any arguments use f(void) to explicitly mark that. If the function accepts argument, always include the arguments' types in the declaration.

Anders Abel
Thanks for clarifying. After reading all the 3 answers here, I understand that the code must be made readable as far as possible and that I need to follow good programming standards.
yCalleecharan
+3  A: 

If defining the function prototype at the top in the program as void f() a good practice? Or is it better to put it as void f(long double y[], long double A) as in the function declaration?

Definitely the latter -- the former doesn't give the compiler any type info for compile-time checks. void f() tells the compiler that f takes an unspecified argument list. I.e. anything goes.

The called function f is changing elements in the array y. Is void the right return type? The program is working fine as such with the void as described.

The return type has nothing to do with the parameter being modified. You can have a non-void return type if you want to indicate a success code though.

Alex
After reading all your comments here, I understand that though void f() works, it's not recommended. It's safer to instruct the compiler the right arguments.
yCalleecharan
+7  A: 

Your question suffers from a terminological mix-up.

void f();

is not a function prototype in C. This is a function declaration that does not introduce a prototype. Meanwhile

void f(long double y[], long double A);

is also a function declaration, and this one is a prototype (i.e. it does introduce a prototype for f).

To answer your questions, yes, it is always a good practice to declare functions with prototypes (i.e. it is better to declare them with prototypes than without prototypes). Normally, you should declare prototypes for all your external functions (and void f() is not a prototype).

As for the return types, it is all a matter of your intent. I don't see how the fact that you are changing the elements of the array should make it better to return long double instead of void.

AndreyT
Thanks for pointing out the mistake in terminology and for the last comment on what's the function is "returning".
yCalleecharan