> For the complete documentation index, see [llms.txt](https://calnix.gitbook.io/eth-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnix.gitbook.io/eth-dev/compendium/solidity-basics/payable.md).

# Payable

### A function cannot receive Ether, unless marked as `payable` .

* `address payable myAddress`
* `function myFunction() public payable{}`
* If a function/address is not marked as payable and receives Eth - it will fail.

```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.1;

contract SendMoneyExample {
    uint public balanceReceived;      

    function receiveMoney() public payable {
        balancedReceived += msg.value;          //msg: global always-existing object
    }

    function getBalance() public view returns(uint) {
        return address(this).balance
    }
}
```

`uint public balanceReceived`

* public state variable -> getter function auto-created. So we can always query its value.
* *therefore, we don't need getBalance -> this is just an example.*&#x20;

`balanceReceived += msg.value`&#x20;

* The msg-object is a global always-existing object containing a some information about the ongoing transaction.&#x20;

![msg object fields](https://1628544884-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTgomzlmn9NrxUY0OQ3cD%2Fuploads%2Fdv7lc7kOA5zXeQsW8SrV%2Fimage.png?alt=media\&token=acd8c387-97b8-40f8-a1ca-bf1ad443e414)

* The two most important properties are .value and .sender.&#x20;
  * .value contains the amount of Wei that was sent to the smart contract.&#x20;
  * .sender contains the address that called the Smart Contract.&#x20;

`address(this).balance`&#x20;

* A variable of the type address always has a property called .balance which gives you the amount of ether stored on that address.&#x20;
* It doesn't mean you can access them, it just tells you how much is stored there. Remember, it's all public information.&#x20;
* `address(this)` converts the Smart Contract instance to an address. So, this line essentially returns the amount of Ether that are stored on the Smart Contract itself.<br>
