Hiii....
I am stuck here..
I want a function which can return a string and also it should be able to take DEFAULT string arguments....
can anyone help me wid d syntax??

Recommended Answers

All 20 Replies

string foo(string myStr);
Was that so difficult?

its not.........

but default strings is creatin sum kind of error...
tell d syntax na...

i mean i want sumthin like

int func(int a=5, int b=10, int c=10);

so for the above one...
char* fun(char* a="awe", char* b="ahgsh");

but default strings is creatin sum kind of error...

What kind. You create functions of all types like this only. Other than this I don't think I know of any other way I've seen anyone use. Trying using std:: in front of string.

I'd personally overload the functions, rather than specifying default arguments.

EDIT:

so for the above one...
char* fun(char* a="awe", char* b="ahgsh");

Yes.

i think u dint get it,..... u r just making a normal function..
i want one with DEFAULT arguments...
even if u dont give any arguments in d call, the default one will take care of it..

char *foo(char *a="a",char *b="b", char *c="c")
{
   cout<<a<<b<<c;
   return c;
}

int main(int argc, char *argv[])
{
  foo();
  return 0;
}

As I said earlier this is fine, but I'd rather overload the foo function to accept multiple/no arguments.

10: error: expected ‘;’, ‘,’ or ‘)’ before ‘=’ token

Above code worked fine for me.

im using linux....is tht creating a problem for me??

nbaztec, this is the C forum. C++ does not work here. ;)

> I want a function which can return a string and also it should be able to take DEFAULT string arguments....
C does not support either default arguments or function overloading, so the simple answer is you cannot do it. The not so simple answer is you can probably do it with compiler extensions and/or tricks, but the end result is probably not worth the extra effort and complexity. You can read more on Stack Overflow.

Well I know that C supports none. But I wanted to help the OP rather than shoo him off to C++ forum. :)
This thread can(should) be moved right?

> This thread can(should) be moved right?
Why? It is not confirmed that the OP even *wants* C++, and errors in compiling your sample code strongly suggests that he is trying to compile as C. Ed is not convinced that "use C++" is the right answer to the question here.

ohh is it??? i dint kno tht.... thnx a lot...

Well the OP wants default arguments, which he can't get in C, you know of any work-abouts?

> Well the OP wants default arguments, which he can't get in C
It is not always practical to switch programming languages, even when the languages are highly compatible like C and C++. The OP is using C for a reason, and we should not dismiss his choice so readily. ;)

> you know of any work-abouts?
Yes. Ed posted a link with ideas on how to get default arguments in C.

ok...who is this OP and Ed???

ok...who is this OP and Ed???

LMAO :D! OP is Original Poster & Ed is Edwards narcissist alter-ego. :D

> who is this OP and Ed???
OP is you, and Ed is Edward. :)

>>OP is you, and Ed is Edward.
See I told you. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.