[Ethereum] Why is there more than one payable function in a solidity contract

fallback-functionpayablesolidity

When someone transfers ether to a contract address the payable callback (without name) function is called automatically.
What I don't understand is why are there many examples with multiple payable functions ? Are all of them automatically called?
Why do they have to be payable and why don't I call them normally from the callback function?

In my examples I noticed that if I have only one payable function and it has a name, the contract can't receive ether so it is somehow not automatically called but only if the payable function has no name. Is that normal or did I do a mistake?

Best Answer

Anyone can call any public function on your contract, by sending a transaction to that contract and including "data" that names that function.

If someone sends a transaction with ether to a function, and the contract did not mark the function as payable, then the call will be rejected. If the contract did mark the function as payable, though, that ether will be deposited into the contract's control. (and whatever code is in the function will run)

The fallback function is called when you send a transaction to a contract, and don't identify any function (in other words, "data" is empty). All the payable rules in the previous paragraph apply. Nothing prevents you from sending 0 ether to the fallback function.

Related Topic