Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

consequences of calling a function with fewer arguments in C?

I wrote a function that that takes some argument and a pointer argument . when calling the function , sometimes I need to pass along the pointer for use inside the function and sometimes I don't . what are the consequences of calling a function with fewer arguments ? it compiles correctly and during runtime its still fine , but is this good programming ? is it better if I call the function with a dummy variable ? Thanks and sorry for beginner question .

like image 227
yaboty Avatar asked May 23 '14 16:05

yaboty


People also ask

What does too few arguments to function mean in C?

error: too few arguments to function(.......) means you are passing fewer arguments than the parameters. You should pass an equal parameter as your function hold. If your function holds three parameters then you must input three parameters for calling the function.

What does it mean to have too few arguments to call a function?

At times, you may have noticed an error “You've Entered Too Few Arguments For This Function” while working with Excel. It mainly happens when you don't fill up the required spaces for the arguments to perform a function in an Excel formula.

What will happen if you call any function with more or less arguments than required in Python?

Passing many arguments will have no effect to the function, as long as the parameters are filled in.

What happens if the function is called with no arguments given?

The C specification says that a function declared without any argument (not even a void argument) takes an unspecified number of arguments. If you want to explicitly say that a function takes no arguments, you need to specify a single void argument type without a name.


2 Answers

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

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

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

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

That form of function declaration/definition has been obsolescent since the 1989 ANSI C standard.

Never use this form.

Functions declaration should always be written as prototypes, i.e., declarations that specify the number and type(s) of the parameters. As a special case, (void) denotes a function with no 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 an incorrect number of arguments and get it past the compiler, the behavior is undefined. It might appear to "work", but it could blow up 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 variadic, taking a variable number of arguments. The most common example of this is printf. For variadic functions, the required arguments are typically specified by the function's documentation -- and it's just as important to get the arguments right. The difference is that, for variadic functions, your compiler won't necessarily tell you that a call is incorrect.

The , ... syntax (in the function declaration) and the macros defined in <stdarg.h> are the only legitimate way to write and use C functions that take a variable number and type(s) of arguments.

like image 108
Keith Thompson Avatar answered Oct 06 '22 01:10

Keith Thompson


One of the differences of C++ over plain C is that it incorporates name mangling, which lets you specify a single function with varying return types and parameters. Two functions that share the same name but have different parameters:

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

can be done in C++ because it actually alters the name of functions behind-the scenes as it compiles it. When you want to pass in a different number of parameters, it's essentially the same as having two different function names that it calls:

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

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

Let's say that you pass in 2 parameters to a function that expects 3. When you try to reference that 3rd parameter within your function, what do you expect the value to be? Most of the time, it will be a garbage value, and definitely something your function does not expect.

I would suggest just passing in a dummy value to functions like that: a NULL for a pointer or a 0 or negative "uninitialized" kind of value for other types. You can at least test for those values in your function. Or just write a second function that takes a different number of parameters.

like image 42
OnlineCop Avatar answered Oct 06 '22 00:10

OnlineCop