My teacher has gone over functions where he will use void, struct, string, float, etc. |
A struct is just a user-defined type that you'll create yourself, basically. It's just a class with public members by default. You know how you make something and use it, like:
1 2
|
int i = 0;
cout << i << endl;
| |
int is a TYPE, and struct allows you to use your own user TYPE.
Float is just floating point numbers. You're probably more familiar with "double." It too is just a type.
String is also a type, and it's used for strings (hence the name). You use that for words/text:
1 2 3
|
string s1 = "Hello";
string s2 = "world!";
cout << s1 + ' ' + s2 << endl;
| |
void is a "type" in the sense that it doesn't return anything. It's quite useful, especially for functions where you want to do something to something else, but (obviously) have no need or desire for a return type. You could use a void function, using the variables s1 and s2 above, in a manner such as:
1 2 3 4 5 6 7 8 9 10
|
void print(string x, string y)
{
cout << x + ' ' + y << endl
}
int main(){
//define s1 and s2 -- see above
print(s1, s2);
return 0;
}
| |
So hopefully now you see that I could create any number of strings and concatenate them (put them together two at a time -- "stitch" them together, if you will) by simply passing two strings to the function print(), rather than typing cout instructions every time I wanted to do it. No return value is needed for this, so we use void.
Hope that helped.