yCalleecharan
yCalleecharan

Reputation: 4704

C function prototype: void f(). Is it recommended?

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

Upvotes: 7

Views: 6484

Answers (3)

AnT stands with Russia
AnT stands with Russia

Reputation: 320699

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.

Upvotes: 10

Alex Budovski
Alex Budovski

Reputation: 18456

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.

Upvotes: 7

Anders Abel
Anders Abel

Reputation: 69280

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.

Upvotes: 4

Related Questions