What is the purpose of the WithdrawalContract
function in this case? If becomeRichest
doing exactly same but with a certain condition and adding a pending withdrawal to the pendingWithdrawals
.
1 Answer
It's the constructor function. It only executes once, i.e. at the creation/ deployment time of the contract.
Here in this case, at the creation/ deployment of the contract, it sets the richest
to be the one who is deploying the contract and the mostSent
to be the value sent at the time of deploying.
The name of the contract is used as the constructor function only in earlier releases. As here name of contract is WithdrawalContract
and the constructor is WithdrawalContract()
as the version says solidity ^0.4.11
. Now - the latest is solidity ^.0.4.24
- this is being deprecated and uses the constructor()
function.
You can read the documentation here for the latest version.
When a contract is created, its constructor (a function declared with the constructor keyword) is executed once.
And in the documentation here for the version 0.4.11
describes your case,
Contracts can be created “from outside” or from Solidity contracts. When a contract is created, its constructor (a function with the same name as the contract) is executed once.
This change has been introduced in the version 0.4.22
- as per the release notes here,
Constructors should now be defined using
constructor(uint arg1, uint arg2) { ... }
to make them stand out and avoid bugs when contracts are renamed but not their constructors.
-
Is it automatically calls? Why? It's not a constructor. Correct me if I'm wrong, please. Jul 29, 2018 at 9:52
-
yes, it get automatically called when the contract is deployed and it is the constructor. Did you read my quotes from the documentations? Jul 29, 2018 at 9:55
-
1Oh, sorry. I didn't note that the contract has the same name as a function. Jul 29, 2018 at 10:38