473,385 Members | 1,927 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,385 software developers and data experts.

Can I call the function in this way

QQ
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short
g2p, bool s = false);
When I call it in the CC can I just use this format, where I don't pass
bool s
D(p, e, c, g1, g1p, g2, g2p);

Thanks a lot!

Jul 23 '05 #1
7 1408
QQ wrote:
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short g2p, bool s = false);
When I call it in the CC can I just use this format, where I don't pass bool s
D(p, e, c, g1, g1p, g2, g2p);


No, you will get an 'ambigous function call' error.

Jul 23 '05 #2
In article <11**********************@f14g2000cwb.googlegroups .com>,
"QQ" <ju****@yahoo.com> wrote:
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short
g2p, bool s = false);
When I call it in the CC can I just use this format, where I don't pass
bool s
D(p, e, c, g1, g1p, g2, g2p);

Thanks a lot!


Looks right.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.
Jul 23 '05 #3
QQ wrote:
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2,
short g2p, bool s = false);
When I call it in the CC can I just use this format, where I don't
pass bool s
D(p, e, c, g1, g1p, g2, g2p);

Thanks a lot!


Yes. That's why C++ allows functions to have default arguments. A
good C++ book should cover this in more detail.

Kristo

Jul 23 '05 #4

"QQ" <ju****@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );
Is this really what you have? That isn't a function definition, it's just a
function prototype. Do you really have something like this?

int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short g2p,
bool s )
{
// function code here...
}

That's a function definition. If that's what you have, then you're fine.

BUT!...if you have a function prototype here, as you've posted, and ALSO a
function prototype like this one below in the header file:
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short
g2p, bool s = false);


....then (assuming you #include the header file) the compiler won't know
which function to call.

(We aren't sure which way your code is written. That's why you've gotten
two different answers.)

-Howard

Jul 23 '05 #5
Howard wrote:

(We aren't sure which way your code is written. That's why you've gotten
two different answers.)


No, the reason is much more basic: this area is confusing. <g>

These two prototypes describe the same function:

void f(char *p, bool s = false);
void f(char *p, bool s);

In any translation unit that has the first one you can call f with one
argument (and the default value) or with two. In any translation unit
that has only the second one you can only call f with two arguments.

In the translation unit that defines f, if it #includes the header with
the first prototype (as it should), then the definition of f cannot have
the default argument. So it's reasonable to say that in that translation
unit f is defined as "void f(char *p, bool s);". Yes, it's not
technically a definition, but newsgroup readers aren't compilers, and
it's okay to speak shorthand now and then.

The second of these two prototypes is illegal:

void f(char *p, bool s = false);
void f(char *p, bool s = false);

because it redefines the default argument. That's also the reason the
actual definition can't have a default argument if the translation unit
has the first prototype: it would also redefine the default argument.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #6
QQ wrote:
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short
g2p, bool s = false);
When I call it in the CC can I just use this format, where I don't pass
bool s
D(p, e, c, g1, g1p, g2, g2p);

Thanks a lot!


If this is what you mean:

// --- stuff.h
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2,
short g2p, bool s = false);
// --- stuff.cpp
#include "stuff.h"

int D(char* p, char* e, char* c,char* g1, short g1p, char* g2,
short g2p, bool s)
{
// do something
}
// --- morestuff.cpp
#include "stuff.h"
..
..
bool ok = false;

// this should work just fine. 's' will be false when D()
// is invoked
D(p, e, c, g1, g1p, g2, g2p);
// these also work
D(p, e, c, g1, g1p, g2, g2p, true);
D(p, e, c, g1, g1p, g2, g2p, false);
D(p, e, c, g1, g1p, g2, g2p, ok);
Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #7

QQ wrote:
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );
This means you have something like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s )
{
--
}
I am asking just because in your post you have put a ;(semicolon) after
the function header. The ; should only be there for prototype and
should not be there for function definition.

I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short g2p, bool s = false);
This is the prototype of the function and you have correctly given a
default value to an argument. Remember that you can give deault values
on ly to the rightmost arguments.
So, while 1 is correct, 2nd proto is wrong:

1. int func(int p, int v=3, int u=4);
2. int func(int p, int v=3, int u);


When I call it in the CC can I just use this format, where I don't pass bool s
D(p, e, c, g1, g1p, g2, g2p);
Yes when you make a call to the function you can omit the last
argument. In this case the last argument would go as false. Incase, you
wanted to pass some other value you can always over-ride the default
value.

Thanks a lot!


Jul 23 '05 #8

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

Similar topics

3
by: Mariusz | last post by:
I want to write function to call another function which name is parameter to first function. Other parameters should be passed to called function. If I call it function('f1',10) it should call...
3
by: Vinodh Kumar P | last post by:
What is call stack? I am sorry if its perceived as an off topic.
6
by: Xing Xu | last post by:
Hi guiders, sorry , since I don't know which group suit for this question,I just post this question at these group. As we know , we can get the run-time call graph by some proved tools . now...
7
by: rahul8143 | last post by:
hello, what is difference between system call and library function call? Does library function call can have context switch to kernel mode? regards, rahul
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
5
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly...
11
by: yangsuli | last post by:
i want to creat a link when somebody click the link the php script calls a function,then display itself :) i have tried <a href=<? funtion(); echo=$_server ?>text</a> but it will call the...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.