AROUND 30 heads of state – from Iran's Mahmoud Ahmadinejad to Cuba's Raul Castro – attended the funeral of Hugo Chavez in a last goodbye to the charismatic but divisive Venezuelan leader who changed the face of politics in South America.

Mr Chavez died on Tuesday aged 58 after a two-year battle with cancer, devastating millions of mostly poor supporters who loved him for putting the country's vast oil wealth at their service, but giving hope to foes who saw him as a dictator.

Huge crowds of Chavistas gathered for the ceremony at a military academy where his body was lying in state.

Many were dressed in the red of the ruling Socialist Party, carrying his picture and waving Venezuelan flags.

"Chavez did not die, he multiplied." they chanted. "Chavez lives. The revolution goes on."

The late president's body is to be embalmed and shown "for eternity" at a military museum – similar to how communist leaders Lenin, Stalin and Mao were treated after their deaths.

His remains will lie in state for an extra seven days to accommodate the millions of Venezuelans who still want to pay their last respects to a man who will be remembered as one of the world's most colourful and controversial populist leaders.

"All these measures are being taken so that the people can be with their leader forever," said Mr Chavez's preferred successor and acting president Nicolas Maduro, who was sworn in as caretaker leader after the funeral yesterday.

More than two million people have so far filed past Mr Chavez's coffin behind a red rope at the grandiose military academy, many sobbing, some saluting or crossing themselves.

Among the leaders in Caracas were close allies such as Ecuadorean President Rafael Correa and Brazil's current and former leaders, Dilma Rousseff and Luiz Inacio Lula da Silva.

"Most importantly, he left undefeated," Raul Castro said, referring to Mr Chavez's four presidential election wins, among a string of other ballot victories in his 14-year rule. He added: "He was invincible. He left victorious and no one can take that away. It is fixed in history."

Celebrity mourners included Oscar-winning US actor and director Sean Penn and Venezuelan conductor Gustavo Dudamel, who arrived together wearing dark suits and sunglasses.

Belarusian President Alexander Lukashenko and Ahmadinejad were among the more controversial figures. Mr Ahmadinejad has caused a storm back home for saying Mr Chavez would be resurrected alongside Jesus Christ and a "hidden" imam who Shi'ite Muslims believe will rise up to bring world peace.

The United States did not send senior officials.

Former US Representative William Delahunt and US Representative Gregory Meeks were attending, amid speculation of a possible post-Chavez rapprochement between the two ideologically opposed governments.

Mr Chavez's death paved the way for a new vote in the Organisation of the Petroleum Exporting Countries nation that boasts the world's biggest oil reserves. But it is unclear when the election will be held. Many Venezuelans have yet to get over his death and have not thought much about the future.

"For me, Chavez was more of a father than a leader," said social worker Anibal Arciniegas, 26, adding that he arrived at midnight to take his place for the funeral. He added: "He made us visible. Before him, governments discriminated against the poor."

The constitution stipulates an election must be called within 30 days, but politicians say the electoral authorities may not be ready and there has been talk of a possible delay.

Mr Maduro, 50, a former bus driver who became Foreign Minister and then vice-president, looks certain to face opposition leader Henrique Capriles, 40, the centrist governor of Miranda state who lost to Mr Chavez in last October's election.

Opposition sources say the 30 or so political groupings making up the Democratic Unity coalition have again agreed to back Capriles, whose 44% vote share in 2012 was the best performance by any candidate against Mr Chavez.

Senior opposition figure Leopoldo Lopez cautioned that the post-Chavez era would not automatically bring a brighter future. He said: "The uncertainty goes on, as does the gross meddling by Cuba and the flagrant violation of the constitution.

"Our people continue to be overwhelmed by insecurity, inflation and food shortages.

"We call on all Venezuelans to join the struggle to build a peaceful way out of the crisis."

Two recent opinion polls gave Mr Maduro a strong lead over Mr Capriles, and Western investors and foreign diplomats are factoring in a probable win for Mr Maduro and a continuation of "Chavista" policies, at least in the short term.