0
0
Blockchain / Solidityprogramming~10 mins

Payable functions in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Payable functions
Call function
Is function payable?
NoReject transaction
Yes
Accept Ether sent
Execute function code
Update contract balance
Return success
When a function is called, the system checks if it is payable. If yes, it accepts Ether and runs the function; otherwise, it rejects the transaction.
Execution Sample
Blockchain / Solidity
contract Example {
  function deposit() public payable {
    // Accept Ether
  }
}
This contract has a payable function 'deposit' that can receive Ether when called.
Execution Table
StepFunction CalledIs Payable?Ether Sent (wei)ActionContract Balance (wei)
1deposit()Yes1000Accept Ether and execute1000
2deposit()Yes500Accept Ether and execute1500
3withdraw()No100Reject transaction1500
💡 At step 3, 'withdraw' is not payable, so transaction is rejected and balance stays the same.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
contract_balance0100015001500
Key Moments - 2 Insights
Why does the contract reject Ether sent to a non-payable function?
Because only payable functions can accept Ether. As shown in execution_table step 3, calling 'withdraw' which is not payable causes rejection.
What happens to the contract balance when a payable function is called with Ether?
The contract balance increases by the Ether amount sent, as seen in steps 1 and 2 where balance updates from 0 to 1000 and then to 1500.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the contract balance after step 2?
A1500 wei
B1000 wei
C500 wei
D0 wei
💡 Hint
Check the 'Contract Balance (wei)' column at step 2 in the execution_table.
At which step is a non-payable function called and what happens?
AStep 2, transaction accepted
BStep 1, transaction accepted
CStep 3, transaction rejected
DStep 3, transaction accepted
💡 Hint
Look at 'Is Payable?' and 'Action' columns in execution_table for step 3.
If the deposit function was not marked payable, what would happen when sending Ether?
AEther accepted and balance updated
BTransaction rejected
CFunction runs but no Ether accepted
DContract balance resets
💡 Hint
Refer to the concept_flow where non-payable functions reject Ether.
Concept Snapshot
Payable functions allow contracts to receive Ether.
Only functions marked 'payable' can accept Ether.
Sending Ether to non-payable functions rejects the transaction.
Contract balance updates when payable functions receive Ether.
Use 'payable' keyword in function declaration.
Full Transcript
Payable functions in blockchain contracts are special functions that can receive Ether when called. When a function is called, the system checks if it is marked payable. If it is, the Ether sent with the call is accepted and the function code runs. The contract's balance increases by the amount of Ether received. If the function is not payable, the transaction is rejected and no Ether is accepted. For example, a deposit function marked payable can receive Ether and increase the contract balance. Calling a non-payable function with Ether causes rejection. This behavior ensures contracts only accept Ether where intended.