For what reason does Shakespeare bring Fortinbras in during Act IV, Scene 4?
Remember that Hamletactually starts out as a highly political play. Barnardo and Marcellus and Horatio are all on watch in the opening act. The reason for which Horatio explains that "...young Fortinbras, / Of unimproved mettle hot and full, / Hath in the skirts of Norway here and there / Shark'd up a list of lawless resolutes, / For food and diet to some enterprise..." (I.1.95-99). This is the same "enterprise" Hamlet references, himself, in his soliloquy at the end of Act 4, Scene 4. So from a marxist perspective, we have to keep in mind class systems, war, and politics. So that is part of the function Fortinbras' appearance serves at this moment. Shakespeare is reminding us of the political uncertainty of the moment, the instability of Denmark who has lost the king, Old Hamlet; who has Claudius on the throne, a man concerned with other things, namely the murder he has committed; and a young Prince Hamlet who has even more on his mind, but seems unconcerned with Fortinbras and that threat.
Also, there is an archetypal critique here. Shakespeare reminds us of this looming threat, this darkness rising that Joseph Campbell and Carl Jung would study in the 20th century. Part of the hero's journey is this constant threat; a hero's job is never really finished. His job is to be a hero on this archetypal journey. So there must consistently be a darkness looming. Here, it is Fortinbras and his 20,000 who are making way for Denmark.
Finally, the above answer hits it on the nose about the narrative technique of establishing a foil. In Hamlet's soliloquy, "How all occasions do inform against me" speech, he takes us rhetorically through his decision to finally act and avenge his father's death, to no longer stand idly by. And part of that rhetoric, along with comparing himself to a beast and questioning his own manhood and sense of duty, is comparing himself to Fortinbras, whom he has just heard about. Fortinbras is a young man of action and Hamlet, at this point is not; Fortinbras has lost a father whom he will honor, and Hamlet has yet to do this as well; Fortinbras demonstrates a conviction and passion and duty, and Hamlet is still wondering if he should be or not be.
He states, "Witness this army of such mass and charge / Led by a delicate and tender prince [Fortinbras], / Whose spirit with divine ambition puff'd / Makes mouths at the invisible event, / Exposing what is mortal and unsure, / To all that fortune, death, and danger dare, / Even for an egg-shell..." (IV.4.47-53).
Fortinbras will face "fortune, death, and danger" for something trivial, something Hamlet deems insignificant, the "egg-shell". And Hamlet has yet to make any decision.
Fortinbras' achievements serve as a foil to Hamlet's inactivity. Hamlet has failed to complete the revenge that he promised the Ghost in Act 1, despite a lot of time passing and the fact that he has had many chances to commit the deed already. In contrast, Fortinbras has managed to raise an army and is marching into a pointless battle, risking the lives of not only himself but the many men that he has brought with him. To Hamlet, Fortinbras' actions are based upon the defence of honour, and this is something that Hamlet haas failed to accomplish. Fortinbras' care free decision to march to battle contrasts with Hamlet's deep and lengthy thought process regarding the revenge of his father. By introducing Fortinbras at this point, Shakespeare emphasizes the effect that Hamlet's contemplative nature has had on the plot to date, and it also provides contrast for the character of Hamlet