Is that .h file is only needed when I need other Classes or .cpp file
to use the function?
If that function is only used in one .cpp file, then I don't need to
write a .h file?
For a valid C++ program you formally do not need any .h files, not
even the ones provided by your compiler environment. You are free to
put every bit of your source code into a single, big .cpp file and it
will work. For some programming languages (e.g. Pascal without
proprietary extensions, ancient Cobol versions, old BASIC dialects)
this is the only way to organize source code.
However, separating implementation and declaration of functionality is
considered a smart thing - it allows easy reuse of code in other
modules of the same program or even share to other programs. You
should welcome the feature and make it a habit to:
1. Put only one class into any source module. Programming languages
like Java and C# strongly encourage or even enforce this for good
reason.
2. Always create a .h and a .cpp file for each class. You never know
when you'll need the same feature again and this will make it easy to
incorporate the class into another program.
3. Speaking of #2, when you design a class, try to make it potentially
useful for other programs - keep the interface lean and avoid
aggregation of too many functionalities. Use inheritance to specialize
for your current problem. If you incorporate too many other classes,
you will have to move all of them to the next project in order to use
only one function.
I'll try to elaborate on this in an example. Say, you want to write an
application about car insurances. A weak approach would be to put
everything into one class: properties of the car mixed with properties
of the insurance:
// CarInsurance.h:
class CarInsurance {
public:
std::string brand; // maker of the car
std::string model; // model of the car
int maxspeed; // maximum speed in mph
std::string company; // name of insurance company
int limit; // limit of insurance coverage per incident
/* ... */
};
This restricts the reusability of your class a lot. I consider it
better to make three classes, each with own .h and .cpp file. You will
be able to cover other applications that deal with cars *or*
insurances without the burden of the unneeded context:
// Car.h:
class Car {
public:
std::string brand; // maker of the car
std::string model; // model of the car
int maxspeed; // maximum speed in mph
/* ... */
};
// Insurance.h
class Insurance {
public:
std::string company; // name of insurance company
int limit; // limit of insurance coverage per incident
/* ... */
};
// CarInsurance.h
#include "Car.h"
#include "Insurance.h"
class CarInsurance : public Car, public Insurance {
/* ... */
};
Methods and .cpp files left out as an exercise to the reader

-
Note how the .h files make it easy to construct the derived class.
It may sound dogmatic to follow these principles even when you do not
feel you need this level of architectural cleanliness in the problem
at hand. After some time it will become the natural thing to do,
though, and you will appreciate how much easier it is to keep track of
your own programs.
best,
Michael.