can i write function definitions in header files? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can i write function definitions in header files?

i just cut all my functions in main.c and pasted them in a header file to make it look cleaner and included that in main.c, now its not compiling what should i do instead

13th Oct 2019, 4:05 PM
Arun Sharma
Arun Sharma - avatar
3 Answers
+ 2
You can do that, but you will need another source file too. For example: func.h void printHello(); func.cpp void printHello() { cout << "Hello!\n"; } And then in main.cpp #include "func.h"; ... printHello(); ... Whenever I have functions that I don't want filling up my main file, I'll put them in separate files like that and I usually call them Utility.h and Utility.cpp. I'll also wrap the file in a namespace so I know where they came from. func.h namespace Utility { void printHello(); } func.cpp void Utility::printHello() { };
13th Oct 2019, 4:33 PM
Zeke Williams
Zeke Williams - avatar
+ 2
Yes the have to
13th Oct 2019, 5:17 PM
AZTECCO
AZTECCO - avatar
+ 1
does the .h and .cpp files need to have the same name
13th Oct 2019, 4:42 PM
Arun Sharma
Arun Sharma - avatar