What is Horatio's function in the play?various ways of importance
The ultimate purpose of Horatio is made clear in Act 5. Hamlet has just been mortally wounded and Horatio is devastated at the thought of losing his good friend and says, "[he] is more antique Roman than a Dane" which implies that he would like to just lay down and die along with his friend, but Hamlet holds him off by reminding them that he must live on in order to "tell the yet unknowning world" what events transpired to bring about such tragic final results. Without Horatio to tell what happened, it would just look like Hamlet went on a rampage! But even before that, Horatio proves to be a loyal confidante to Hamlet; he is literally the only person Hamlet can trust in the entire play.
At the start of the play, Horatio is the one who has confirmation of the ghost's appearance and tells his friend what he has seen. He accompanies Hamlet out that night to help keep on eye on things when Hamlet goes to talk to the ghost. He specifically warns Hamlet that the ghost may just be a devil in disguise and he "may tempt you towards the flood my lord... or to the dreadful summit of a cliff."
In Act 3 Hamlet trusts Horatio enough to solicit his help in watching Claudius during the performance of the play. Hamlet wants to be assured that he sees proof of Claudius's guilt. Hamlet specifically compliments Horatio for his balance of "blood and judgement" in his behavior and thoughts -- not overly emotional; not overly full of thought.
In Act 5, Hamlet confides in Horatio all his thoughts and observations about life and death during the graveyard scene and later reveals all that transpired on the boat to England. Horatio is impressed with Hamlet's determination to do what needed to be done in order to save himself and send Rosencrantz and Guildenstern to their death in England. He is a good listener to Hamlet's new understanding of the role of fate in his life. Horatio expresses concerns over Hamlet's getting involved in the duel with Laertes, but still lets Hamlet do what he needs to do.
Ultimately, Horatio is the loyal friend that Hamlet so desparately needs in a play that is filled with people who only betray one another.