473,806 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15399
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******@csclu b.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************ *********@never box.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
3535
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 by pointer in C++, is there such way in Python? Thansk in advance. J.R.
14
5280
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 ask_ok(prompt, retries=4, complaint='Yes or no, please!'): while for keyword arguments the tutorial shows: def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
7
3564
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 you do not actually need to fork Python in order to implement prototypes. It seems to me that Python metaclasses + descriptors are more than powerful enough to implementing prototypes in pure Python. I wrote a module that implements part of what...
49
2635
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 code sample where newbies expect the two function calls below to both print : def f( list= ): print list.append!(1) f() # prints
2
4597
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 definition, and then copied the definition directly as the declaration: void A::foo(int a, int b=0); // declare somewhere void A::foo(int a, int b=0) {...} // define in another file 3.3 doesn't like this, because the default is repeated twice. I...
8
6665
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
12710
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
2522
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); void funcY(int i, int j,char *name);
16
2326
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 beginning to get out of hands if I continue to put in extra functionality in the (file-writing) output-functions....
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10623
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10373
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9192
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.