origin of Pagodas?

Pagodas 9those multi-story, tower-like buildings0 have always fascinated me. What is their origin? Were they originally religious temples? As housing, they would be quite impracticable. anyway, you see them in China, Japan, bali, and Vietnam…don’t know if Thailand has them. They are costly to construct, so what is their purpose? they are usually topped by a shaft with has numbers of rings-what is that for?
And finally, the name comes from a portuguese word 9meaning noisey)-how did this come about?