It was built to help the kings ascend to the God of Sun
The Egyptian pyramids were constructed during the so-called Golden Age, the rule period of the fourth dynasty of the Egyptian old kingdom, and mainly served as tombs of the kings and their resting place until they moved into afterlife. For this reason, the tombs were usually equipped with all the objects that may serve the king in his afterlife, sometimes even including animate objects like live animals.
However, one astonishing fact about the pyramid is that it was purposefully built to help the late king ascend to the sun, to the God of Sun Ra, since every Egyptian king deemed himself as a deity living on earth. The pyramid is the place where this ascension takes place and the king is reborn. However, this does not answer all the possible questions about the mysterious ancient pyramids. It seems that whenever archaeologists or historians disagree upon what the actual purpose of a historical artifact was, it is ascribed automatically to religious causes.