can we initialize a function argument in c

H

harry

hi,
can we initialize a function argument in c .
like at the time of declaration can we give like that in c

int func(int a, int flags = 0)
so that i can use it for default in future.
 
J

Joachim Schmitz

harry said:
hi,
can we initialize a function argument in c .
like at the time of declaration can we give like that in c

int func(int a, int flags = 0)
so that i can use it for default in future.

Not in ANSI/ISO C. You can in C++ and apparently also with Jacob Navia's
win-lcc, as a propriatiry, non-standard extension.

See also the recent thread titled "implement default variable in C?? is it
possible"

Bye, Jojo
 
C

CBFalconer

harry said:
can we initialize a function argument in c .
like at the time of declaration can we give like that in c

int func(int a, int flags = 0)
so that i can use it for default in future.

No.
 
B

Barry Schwarz

No. There is a workaround.

int func(int a)
{
func2(a, 0);
}

int func2(int a, int flags)
{
}

The defualt version is just a trivial wrapper to the full version of the
function.

I thought the purpose of a default value for a function argument was
to assign a value when the calling statement didn't. (That seems to
be the way it works in those languages which let you do this.) The
code you have for func2 could be just as easily replaced with
int func2(int a)
{
int flags = 0;
}
rendering func unnecessary.

But we still have the question - other than variadic functions, how do
you not provide an argument in the calling statement? Would the
language have to be expanded to accept consecutive commas in the
argument list? Of course, the missing argument would be replaced by
the default value from the prototype.
 
K

Keith Thompson

Barry Schwarz said:
I thought the purpose of a default value for a function argument was
to assign a value when the calling statement didn't. (That seems to
be the way it works in those languages which let you do this.) The
code you have for func2 could be just as easily replaced with
int func2(int a)
{
int flags = 0;
}
rendering func unnecessary.

The intent is that the body of func2 actually uses the value of flags.
If I want to use a non-zero flags value (say, if I want to specify
some unusual behavior), I can call func2:
func2(42, 0x40);
If I want to use a zero flags value, the "func" wrapper gives me a
shortcut:
func(42);
which is equivalent to
func(42, 0);

There are examples of this in the standard library: printf is
effectively a wrapper for fprintf, providing a default value (stdout)
for the first parameter.
But we still have the question - other than variadic functions, how do
you not provide an argument in the calling statement? Would the
language have to be expanded to accept consecutive commas in the
argument list? Of course, the missing argument would be replaced by
the default value from the prototype.

There are several possibilities. The simplest is to require that any
optional parameters (i.e., parameters with default values) must be
declared at the end of the parameter list. Then a call can omit one
or more trailing arguments, and the corresponding parameters take
their default values.

Or, as you suggest, you could accept consecutive commas -- but I
personally dislike this, since it makes calls too difficult to read:
foo("some string", 42, , , , '$');
The '$' is, let's see, [count commas] the 6th argument, so it
corresponds to, um, [count commas in declaration] this parameter.

Yet another possibility is to add named parameter associations, so
that arguments don't have to be given in the specified order, and you
can skip parameters without using consecutive commas. (Ada does
this.)

I'm doubtful that this feature will be added to standard C any time
soon.
 
R

Rafael

Using variable number of arguments you kind of can do something close to
that - just don't feed the value and assume defaults on missing arguments.
 
D

David Thompson

There are several possibilities. The simplest is to require that any
optional parameters ... must be ... at the end of the parameter list. ...
Yes.

Or, as you suggest, you could accept consecutive commas -- but I
personally dislike this, since it makes calls too difficult to read: ...

I used fairly extensively another SIL which did it this way, and I
found that used in moderation and with some care it wasn't too bad.
But it wouldn't be my first choice.
Yet another possibility is to add named parameter associations, so
that arguments don't have to be given in the specified order, and you
can skip parameters without using consecutive commas. (Ada does
this.)
So does Fortran since 90. (And some of the new intrinsics added in F90
and later have parameter lists which are rather inconvenient if you
don't use named syntax for them.)

In C99 you can kinda sorta fake named arguments, and named optional
ones if you can take 0 as the default value, by creating a struct type
containing your parameters, and using a compound literal with
designated initializers to construct the value you pass. Or usually
better, a few types that group logically related parameters that may
be reusable for multiple routines. In my experience 0 is an acceptable
'nothing' or 'default' value for most of my defaultable parameters,
and the rest can usually be bent into shape with only a small hammer.

Ada and F90+ also have ways to create a value of their struct
equivalents from named items, and with arbitrary defaults not just 0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,259
Messages
2,571,035
Members
48,768
Latest member
first4landlord

Latest Threads

Top