473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Default arguments and prototypes

If I feed this to g++:
--------
int foo(int i=42);

int foo(int i=42)
{
return i;
}
--------
It says (with -W -Wall -ansi -pedantic):
--------
foo.C: In function `int foo(int = 42)':
foo.C:4: warning: default argument given for parameter 1 of `int foo(int = 42)'
foo.C:1: warning: after previous specification in `int foo(int = 42)'
--------

Does this warning indicate any actual problems, or is it just pointing
out that with the default argument given in the prototype it's not needed
in the function definition as well? (I can see how this would lead to
a minor maintenance problem with the values getting out of sync.)

Are there any good reasons not to use this:
--------
int foo(int i=42);

int foo(int i)
{
return i;
}
--------
instead?

(And, while I've got your attention, am I correct in thinking that if
only the prototype is given in another translation unit, that prototype
is required to specify the correct default value of the argument?)
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I disagree. The best indicator of comp.lang.c activities is an
industrial-strength thermometer.
--Richard Heathfield in comp.lang.c
Jul 19 '05 #1
5 15363
WW
Dave Vandervies wrote:
If I feed this to g++:
--------
int foo(int i=42);

int foo(int i=42)
{
return i;
}
--------
It says (with -W -Wall -ansi -pedantic):
--------
foo.C: In function `int foo(int = 42)':
foo.C:4: warning: default argument given for parameter 1 of `int
foo(int = 42)' foo.C:1: warning: after previous specification in `int
foo(int = 42)' --------

Does this warning indicate any actual problems, or is it just pointing
out that with the default argument given in the prototype it's not
needed in the function definition as well?
AFAIK it should not be there at all.
Are there any good reasons not to use this:
--------
int foo(int i=42);

int foo(int i)
{
return i;
}
--------
instead?


If I am not mistaking this is the form to be used if the declaration and the
definition of the function is separate.

--
WW aka Attila
Jul 19 '05 #2
"Dave Vandervies" <dj******@csclub.uwaterloo.ca> wrote...
If I feed this to g++:
--------
int foo(int i=42);

int foo(int i=42)
Drop the default argument value from the line above
{
return i;
}
--------
It says (with -W -Wall -ansi -pedantic):
--------
foo.C: In function `int foo(int = 42)':
foo.C:4: warning: default argument given for parameter 1 of `int foo(int = 42)' foo.C:1: warning: after previous specification in `int foo(int = 42)'
--------

Does this warning indicate any actual problems, or is it just pointing
out that with the default argument given in the prototype it's not needed
in the function definition as well? (I can see how this would lead to
a minor maintenance problem with the values getting out of sync.)
According to the Standard, once a default argument value has been
given, no other default argument is allowed to be specified.
Are there any good reasons not to use this:
--------
int foo(int i=42);

int foo(int i)
{
return i;
}
--------
instead?
I guess I don't understand the question. What do you mean "reasons
not to use this"? "This" is the only way the code is going to be
accepted. A good enough reason for you?
(And, while I've got your attention, am I correct in thinking that if
only the prototype is given in another translation unit, that prototype
is required to specify the correct default value of the argument?)


No, you're not. Every declaration in its own translation unit is
allowed to have its own default argument value.

Victor
Jul 19 '05 #3
WW
Victor Bazarov wrote:
[SNIPPO GROSSO]
No, you're not. Every declaration in its own translation unit is
allowed to have its own default argument value.


That must be fun to debug! :-)

--
WW aka Attila
Jul 19 '05 #4
Victor Bazarov wrote:

According to the Standard, once a default argument value has been
given, no other default argument is allowed to be specified.

<snip>
(And, while I've got your attention, am I correct in thinking that if
only the prototype is given in another translation unit, that prototype
is required to specify the correct default value of the argument?)

No, you're not. Every declaration in its own translation unit is
allowed to have its own default argument value.


OK, I have a question along these same lines (since we're on the topic).
Is the following allowed?

void f(int, int=3);
void f(int=2, int); // add another default?

I was reading about this in the standard yesterday, and couldn't decide
whether this would be allowed. I know this is not:

void f(int, int=3);
void f(int=2, int=3); // ERROR: can't give a new default (even if
// the value is the same) in the same scope

Nor is this:

void f(int, int=3);

{
void f(int=2, int); // ERROR: Does not inherit default from
// enclosing scope, so this violates the
// rule than only trailing args have
// default values.
}

Right?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5
"Kevin Goodsell" <us*********************@neverbox.com> wrote...
Victor Bazarov wrote:

According to the Standard, once a default argument value has been
given, no other default argument is allowed to be specified.


<snip>
(And, while I've got your attention, am I correct in thinking that if
only the prototype is given in another translation unit, that prototype
is required to specify the correct default value of the argument?)

No, you're not. Every declaration in its own translation unit is
allowed to have its own default argument value.


OK, I have a question along these same lines (since we're on the topic).
Is the following allowed?

void f(int, int=3);
void f(int=2, int); // add another default?

I was reading about this in the standard yesterday, and couldn't decide
whether this would be allowed. I know this is not:

void f(int, int=3);
void f(int=2, int=3); // ERROR: can't give a new default (even if
// the value is the same) in the same scope

Nor is this:

void f(int, int=3);

{
void f(int=2, int); // ERROR: Does not inherit default from
// enclosing scope, so this violates the
// rule than only trailing args have
// default values.
}

Right?


Seems like it. Paragraph 4 of subclause 8.3.6 states that "In
a given function declaration, all parameters subsequent to
a parameter with a default argument shall have
default arguments supplied in this or previous declarations.".

In your example, the second parameter in the second declaration
of 'f' gets its default argument value from a previous declaration.

Comeau accepts it.

Victor

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
14
by: Edward Diener | last post by:
In the tutorial on functions there are sections on default arguments and keyword arguments, yet I don't see the syntactic difference between them. For default arguments the tutorial shows: def...
7
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that...
49
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon...
2
by: Paul Davis | last post by:
I've just converted from gcc2.96 to gcc3.3. One of the things that 3.3 complained about was my use of functions which had default arguments. Previously, I put the defaults in my function...
8
by: Agent Mulder | last post by:
Hi group, I want to know why this doesn't work void f(int a=0,int b=1,int c=2){} int main() { f(); //OK f(1); //OK f(1,2); //OK f(1,2,3); //OK
12
by: earl | last post by:
class temp { public: temp(); foo(char, char, char*); private: char matrix; }; temp::foo(char p, char o, char m = matrix )
3
by: prasanthag | last post by:
Hi, I am a newbie to this group. I have a problem in handling the variable arguments passed to a function. My requirement is like this. I have 2 functions say, void funcX(int i, int j);...
16
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.