why wont it let it be string mainName inside of int main I mean it seems like im just defining it more than i need to but it refuses to take it like that. Since it takes string mainName everywhere else it just seems weird.
@m4ster r0shi - thank you r0shi, sorry im so stupid. Even your second explanation i had to re-read twice. But I see what you are saying that basically instead of name i should have been calling mainName however, this relates to my above question too, why does it matter what my SomeType is if c++ doesnt even want me to put in a type?
Oh, I see what you mean. I don't know the particular reasons but the function doesn't really need to know the type (at least not in the parameter field), just the variable name. If you think of/read up on function overloading, you will see that you simply name the parameter and the function used is determined by the variable type even though the function names are the same. A quick example using your code:
#include <iostream>
#include <string>
usingnamespace std;
string getusername();
void username(string mainName);
void username(int mainName); //same function name yet different parameter type
int main()
{
string mainName;
int overload; //what I'm using as an example
mainName=getusername();
overload = 5; //giving it a value
username(mainName);
username(overload); //same function name but using a number?! GASP
cin.ignore();
cin.ignore();
}
string getusername()
{
string name;
cout<<"Please enter your name: ";
cin>>name;
return name;
}
void username(string mainName)
{
cout<<"Thank you, "<<mainName;
}
void username(int mainName)
{
cout << "\nYou entered a number yet used the same function name\n"; //this will print out after
//the other username func. finishes
}
So simply designate the variable name and leave out the type.
When you write a function, you must provide a declaration and a definition for it. In these, you must specify the type of the arguments and a name for every argument (though that last one can be omitted in declarations). When you call your function, you just have to pass something that agrees with your function's parameter list.
#include <iostream>
usingnamespace std;
//function declaration
void foo(int x);
//this would also work
//void foo(int);
int main()
{
int y;
y=5;
//function call
//y is of type int,
//we are ok
foo(y);
cin.get();
return 0;
}
//function definition
//we need names for the paremeters
//here. how else could we refer to
//them inside the function body?
void foo(int z)
{
cout << "your variable's";
cout << " value is: ";
cout << z << endl;
}
vlad61 wrote:
it seems like im just defining it more than i need to but it refuses to take it like that
thank you so much guys! this was very informative. BTW m4ster r0shi I tried "over-defining" it like you said and it actually just skipped that whole line. any ideas?
EDIT: but it DID compile fine, just skipped the name echo process.
EDIT: OH and ive been curious. what is foo?? why was it used so many times for examples? I was thinking maybe it stands for function but its strange.
I tried "over-defining" it like you said and it actually just skipped that whole line.
It didn't "skip" it. A function declaration isn't a function call. Don't expect it to print anything. A function declaration is used to inform the compiler about the name of the function and its return and parameter types. Without it, the compiler can't know if an identifier (a word) is a function or not. Take a look at this:
This won't compile. The compiler complains about this-> foo(y);, because it doesn't know that 'foo' is a function (because there's no declaration of 'foo'). Uncommenting either the first or the second (or both) declaration(s) will do the trick. The rule is that you must always declare a function before you call it.
Something else. You can use the definition itself as a declaration, but you have to put it before every function call. Example:
#include <iostream>
usingnamespace std;
void foo(int a)
{
if (a<=0) return;
cout << "foo: ";
cout << a << endl;
bar(a-1); //compiler says: "huh? bar? what bar?"
}
void bar(int b)
{
if (b<=0) return;
cout << "bar: ";
cout << b << endl;
foo(b-1); //ok, foo was declared above
}
int main()
{
int x=5;
foo(x); //ok, foo was declared above
cout << endl;
bar(x); //ok, bar was declared above
cin.get();
return 0;
}
You can easily get around this by putting this -> int bar(int); above foo's declaration-definition of course, but this is ugly. It's better to have function declarations above main and function definitions below it.