When an audience is watching any play they only see actors on a stage. The playwright has the task of introducing these people as characters with names. Notice how Hamlet keeps calling Horatio by name in his dialogue. This is solely to inform the audience that this is Horatio.
I am glad to see you well:
Horatio,--or I do forget myself.
Sir, my good friend; I'll change that name with you:
And what make you from Wittenberg, Horatio?
Notice that here Hamlet (who does not need to be called by name, since he has already been called Hamlet numerous times in the preceding scene) calls Horatio "my good friend." This is important information for the audience. Horatio will be Hamlet's only true and trustworthy friend throughout the play. Horatio exists as a character mainly to be a confidant to Hamlet. Hamlet speaks his private thoughts in numerous soliloquies, but Shakespeare may have felt he could only use so many of these artifices and would like to have at least one character to whom Hamlet could express his plans, secrets, and misgivings in confidence.
Thrift, thrift, Horatio! the funeral baked meats
Did coldly furnish forth the marriage tables.
Would I had met my dearest foe in heaven
Or ever I had seen that day, Horatio!
My father!--methinks I see my father.
In these lines he calls Horatio by name twice. This is to make sure that the audience will remember that the man they are looking at is Horatio, Hamlet's friend, a fellow student from Wittenberg.
In my mind's eye, Horatio.
Hamlet continues to call him by name. Obviously Horatio is going to be a very important character in this play. At the end he will be the only character left alive who knows all the facts--that Claudius murdered Hamlet's father to usurp the crown and Hamlet killed Claudius in revenge. Without Horatio, the play would end with a lot of dead bodies on the stage, and nobody would know know why.
Horatio will serve many purposes throughout the play. He will be a witness when Claudius reveals his guilt during the play-within-a-play. Hamlet expresses his trust and liking for Horatio at length as they are conspiring to watch Claudius's reaction, beginning with:
Horatio, thou art e'en as just a man
As e'er my conversation coped withal.
When Hamlet is captured by pirates, it is to Horatio he writes for help and Horatio who arranges for his ransom. Then when the two friends meet again, Hamlet explains what has happened since Claudius sent him to England. He discovered that the King had sent sealed orders to have the English execute him. He forged new orders and sent Rosencrantz and Guildenstern to their deaths in his stead. And so on. Horatio is characterized as an intelligent, understanding and loyal friend in whom Hamlet can place absolute trust. Having such a character makes Shakespeare's job easier, because so much important information and exposition can be conveyed to Horatio by Hamlet in their dialogue.
Hamlet could have learned about the visitations of his father's ghost without Horatio, but Shakespeare wanted to introduce Horatio early in the play and establish that he and Hamlet are not only friends and schoolmates but also very much alike in their personalities. They are intelligent, introspective, observant, responsible, ethical, educated gentlemen. Horatio and Hamlet are virtually soul mates. As readers who sympathize with the unhappy Prince, we are glad he has a friend he can trust. We like Horatio because he is a good friend to Hamlet.