You proceed from the logic of anthropocentrism - a philosophical concept according to which a person is the center of all events occurring in the Universe. The question "why" implies the presence of some meaning, an ultimate goal, a task. This is the wrong approach. The universe and life have no goals or objectives. It doesn't even make sense. The universe and life are not needed for anything. They just are. The universe has existed for billions of years before us, and, perhaps, if we follow some hypotheses, it will exist for another trillion years after us. Therefore, the correct formulation of the question would be something like this: "How did it happen that everything exists?" In fact, the answer to this question is not very simple, because we do not fully understand how the universe came to be. Nevertheless, you need to understand that if it were not for them - the Universe, life and other things - you would not be able to ask this question at all now.
Space is an eternal order, it goes beyond the framework of natural being, being essentially Nothing, everything comes out of this Nothing, and it returns there, existence grows from space, while it does not carry any function, in fact, Nothing can be something, and can not have a need, therefore I say, the cosmos as Nothing, like eternal order, beginning and end, has no function.