473,509 Members | 2,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function prototype with optional parameters?

Hi Group,

sorry, its again me with a dumb question.

If i have a function with the optional parameter "mode" like this:

void myfunc(char *blah, int mode = NULL)
{
if(mode == NULL)
do_something();
else
do_something_else();
}

and i want to have a prototype for this function in my header-file and so i
write:

void myfunc(char *blah, int mode = NULL);

I get a compile error: redefenition of mode

If i write:

void myfunc(char *blah, int mode);

and i use the function with a function call like this:

myfunc("Hello World");

I got a compile error: function does not take 1 Parameter.

But i want to have the mode-param. optional. So you can call the function
with two parameters or with one and using the default value for "mode".

How have i to do this to keep the compiler (and me :-)) ) happy?

Someone out there who is willing to enlighten me a little bit? Thanks a
loooot!
CU
Joerg Toellner
Jul 19 '05 #1
4 60699
"Joerg Toellner" <to******@oss-gmbh.de> wrote in message
news:bf*************@news.t-online.com...
If i have a function with the optional parameter "mode" like this:

void myfunc(char *blah, int mode = NULL)
{
if(mode == NULL)
do_something();
else
do_something_else();
}

and i want to have a prototype for this function in my header-file and so i write:

void myfunc(char *blah, int mode = NULL);

I get a compile error: redefenition of mode


Right, because the default value for the optional argument shall onl
appear once. If you have a function prototype, the default argument goes
there, and only there. Like this:

void myfunc(char *blah, int mode = NULL);

void myfunc(char *blah, int mode)
{
if(mode == NULL)
do_something();
else
do_something_else();
}

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2
>
If i have a function with the optional parameter "mode" like this:

void myfunc(char *blah, int mode = NULL)
{
if(mode == NULL)
do_something();
else
do_something_else();
}

and i want to have a prototype for this function in my header-file and so i
write:

void myfunc(char *blah, int mode = NULL);


Define the default param value in the header:

..hpp: void myfunc(char *blah, int mode = NULL);

but NOT in your source file:

..cpp: void myfunc(char *blah, int mode) { /* WHATEVER */ }
Take no offence, but by the look of your questions, you ought to get
a decent C++ book. Everything you asked so far should be in the first
chapters.

regards,

Jan

--
Jan Rendek
INRIA Lorraine
r e n d e k @ l o r i a . f r

Jul 19 '05 #3
Hi Jakob,

AAAAAAAH! I seeeee!
*SomeoneSwitchesABigLightBulbOverMyHeadToOnState*

Thanks a lot, that makes sense!
Why haven't not thought about that by myself? *ShameRedChangingFaceColor*

Thx. again...and have a nice day!
CU
Joerg Toellner

"Jakob Bieling" <ne*****@gmy.net> schrieb im Newsbeitrag
news:bf*************@news.t-online.com...
Right, because the default value for the optional argument shall onl
appear once. If you have a function prototype, the default argument goes
there, and only there. Like this:

void myfunc(char *blah, int mode = NULL);

void myfunc(char *blah, int mode)
{
if(mode == NULL)
do_something();
else
do_something_else();
}

hth
--
jb

Jul 19 '05 #4
Hi Peter,

Wow! What an Idea with the function overloading. I know function overloading
and used it where i have different Inputs for the same function. But never
thought of this usage for defaulting parameters. But it make sense.

I'll look, if i can get the recommended book.

TYVM
Joerg Toellner

"Peter van Merkerk" <me*****@deadspam.com> schrieb im Newsbeitrag
news:bf************@ID-133164.news.uni-berlin.de...
See above. Alternatively you could create an overloaded variant of the
function.

--------foo.h-----------------
void myfunc(char *blah);
void myfunc(char *blah, int mode);

--------foo.cpp-----------------
void myfunc(char *blah)
{
do_something();
}

void myfunc(char *blah, int mode)
{
do_something_else();
}

Item 24 of the book "Effective C++" from Scott Meyers (highly
recommended) discusses how to choose beteen function overloading and
parameter defaulting.

Jul 19 '05 #5

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

Similar topics

21
3807
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
6
7958
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
11
1909
by: Neo | last post by:
Why the following code is compilable? The function abc() doesn't take any arguments, still i can call the function with arbitraty number of arguments. Even compiler doesn't show any warning. What...
5
7235
by: Kobu | last post by:
Does anyone know how old style function definitions differ in "behaviour" to new style function definitions? I heard the old style function definitions caused integeral promotion and floating...
3
3628
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
20
2330
by: Christian Christmann | last post by:
Hi, in a benchmark I've found an uncommon use of a function. This is the simplified form: 1 int foo( int f ) 2 { 3 return f; 4 } 5
8
5508
by: Jon Harrop | last post by:
I am trying to learn C# and .NET programming in general but I am finding it very hard going. To start with, I'd like to translate some trivial functions from other languages that I am familiar with...
2
6143
by: WGW | last post by:
Hello all, I need another set of eyes cause it just isn't working, no matter how identical I make it. The initRotator function works when called by itself, but when adding another function, only the...
7
7039
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
7234
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,...
0
7136
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...
0
7344
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
7412
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
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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 ...
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.