Hi guys, im a first time poster and a complete beginner with c++,
I've written the following piece of code simply to familiarise myself with variables & function, and im hoping someone could take a look at it and let me know if im constructing my code in the correct way. The program works fine, but obviously I dont want to pick up any bad habits this early on!
Thanks in advance for your advice :)
There's no need to worry about efficiency unless it is critical in the application you're creating, this probably won't be true of programs you make as a beginner. The only thing I would add about the code you've posted is that you should pass strings into functions by const reference e.g. void outputname(const string& chName)
passing basic types like int and char by value is fine but when you're passing an object like a string of an object of a class you should generally pass a const reference.
I was always taught to put the main() functions first. To do that you need to make your function prototype definitions below using namespace std. Just write the type, name of the functions and arguments like normal but add a semicolon at the end.
1 2 3 4 5 6 7 8 9 10
usingnamespace std;
string charactername();
void outputname( string chName );
void outputname( string chName );
void outputinfor( int s, int p, string chName );
int main()
{
// do stuff...
}
Also, and this is just my preference, it is easier to use camelback text or to use underscores to make reading variable/function names easier. As your programs get bigger and bigger, the easier they are to read, the better.
No, it won't. But it helps if you can find the main function easily.
Thanks to modern IDEs, it's a non-issue anyway, since you can easily jump to any function in the current source file.
Will it affect the overall program in any way? (sic)
No. You're basically informing the compiler/linker that the function exists, but you haven't yet defined its body. These are called Function Prototypes. This is valid and is commonly used when working with header modules.
As for your code, I would've explicitly told the compiler the parts of the std namespace I was using. For example: