Simple output using an additional function

-
Last edited on

overloaded approach: (note that overloading means writing multiple complete functions with the same name, but different configurations. )

void greet()
{
cout << Greetings Earthling" << endl;
}
void greet(string s)
{
cout << Greetings " << s << endl;
}
don't forget to put BOTH prototypes above main.
void greet(string s);
void greet(); //both needed.

another way to do such a simple thing is this: (this is not overloading, but it can often be used as an alternative)
1
2
3
4
void greet(string s = "Earthling")
{
cout << Greetings " << s << endl;
} 
if you call it with greet() it will default the input
if you call it with greet(stringvar) it will override the default

Please put code in code tags <> on the side editor, it makes it readable and runable (if you post a complete example).

Last edited on
Topic archived. No new replies allowed.