Consequences of calling a function with fewer arguments in C?

I wrote a function that takes some argument and a pointer argument. when calling a function, sometimes I need to loop along the pointer for use inside the function, and sometimes not. what are the implications of calling a function with fewer arguments? it compiles correctly and at runtime it is still fine, but is it good programming? is it better if i call the function with a dummy variable? Thanks and sorry for the starter question.

+2


source to share


3 answers


If you call a function with too few arguments and the compiler doesn't complain, then you are doing something wrong.

You can write a function declaration / definition that doesn't define how many arguments it requires:

void func();
/* ... */
func();
func(arg1);
func(arg1, arg2);

      

All three of these calls will be accepted by the compiler, but at least two of them are incorrect.

This form of function declaration / definition has been deprecated since the ANSI C. 1989 standard.

Never use this form.



Function declarations should always be written as prototypes, i.e. declarations that define the number and type of parameters. As a special case, (void)

denotes a function without parameters.

void func(int arg);
/* ... */
func();           /* rejected by compiler */
func(arg1);       /* accepted -- but only if arg1 is of type int or convertible to int */
func(arg1, arg2); /* rejected by compiler */

      

If you manage to write code that calls a function with the wrong number of arguments and gets past the compiler, the behavior is undefined. It may seem "working", but it can explode in your face when, for example, you compile it with a different compiler or with the same compiler and different options.

One complication: some functions are variable, taking a variable number of arguments. The most common example of this is printf

. For variational functions, the required arguments are usually specified in the function documentation - and it is just as important that the arguments are correct. The difference is that for variadic functions, your compiler doesn't necessarily tell you that the call is wrong.

The syntax , ...

(in a function declaration) and macros defined in <stdarg.h>

are the only legal way to write and use C functions that take a variable number and type of arguments.

+2


source


One of the differences between C ++ and plain C is that it includes name mangling , which allows you to specify a single function with different return types and parameters. Two functions that have the same name but different parameters:

int foo();
int foo(int param1, char* param2);

      

can be done in C ++ because it actually changes the name of the functions behind the scenes when it compiles it. If you want to pass a different number of parameters, this is essentially the same as having two different function names that it calls:

int foo1();
int foo2(int param1, char* param2);

      



When you pass fewer parameters than you expect, some compilers should at least throw warnings; others won't compile the program at all.

Let's say that you are passing 2 parameters to a function that expects 3. When you try to refer to this third parameter in your function, what do you expect from the value? In most cases, this will be the cost of garbage, and definitely something your function is not expecting.

I would suggest simply passing a dummy value to functions like this: a NULL

for a pointer, 0

or a negative "uninitialized" value for other types. You can at least check these values ​​in your function. Or, just write a second function that takes a different number of parameters.

+1


source


I wouldn't do that. I would call a function with only a declared number of arguments.

This is extremely confusing for the reader of the code.

The reader of the code might even think that the function is overloaded and looks to other function definitions with fewer arguments.

Omitted arguments can have any meaning at all β€” anything is considered legal behavior by the C compiler. The program can run fine in a test environment and then fail for any reason or no reason.

If I absolutely need to have a variable number of arguments, for a legitimate purpose I would try to put the arguments in a list instead of using a variable number of arguments. If someone else required me to use variable arguments, I would use varargs instead of the default behavior, and then only when all the arguments are of the same type.

+1


source







All Articles