I am fairly new to Solidity and I wanted to understand the Push function better. I am using solidity ver 0.5.12 I wanted to push the Owner address into an array as follows

        address Owner;
        constructor() public
             Owner = msg.sender;       
        address[] playersAddressList;

Remix is giving me an error stating browser/Lottery.sol:53:32: ParserError: Expected identifier but got '(' playersAddressList.push(Owner); ^

However, when I am trying to push the Owner into an array inside the following function there is no error

  function addPlayer(string memory a, uint b) public payable

        address creator = msg.sender;
        balance = balance + msg.value;
        require (msg.value >= 1 ether);
        person memory newPerson;

        newPerson.name = a;
        newPerson.age = b;

        players[creator] = newPerson;

I wanted to understand why I can push the Owner's address to an array inside the function but not outside it?

Thanks Rwiju

1 Answer 1


Other than declaring state variables and initializing them to values, you cannot have code executed outside of functions.

In fact, even the initializing them to values part is actually "moved silently" into the constructor function by the compiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.