I'm trying to insert an address into an address type dynamic array. It works well when I do the operation inside a function. There are no errors and the elements are properly pushed into the array.
pragma solidity ^0.5.2;
contract Random {
address[] public participants;
function addParticipant() public {
participants.push(0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c);
participants.push(0x583031D1113aD414F02576BD6afaBfb302140225);
participants.push(0xdD870fA1b7C4700F2BD7f44238821C26f7392148);
}
}
But when I try to do it without a function it shows an error.
Error: Expected identifier but got '('
pragma solidity ^0.5.2;
contract Random {
address[] public participants;
// function addParticipant() public {
participants.push(0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c);
participants.push(0x583031D1113aD414F02576BD6afaBfb302140225);
participants.push(0xdD870fA1b7C4700F2BD7f44238821C26f7392148);
// }
}
Please advise.