0
0
Blockchain / Solidityprogramming~5 mins

Payable functions in Blockchain / Solidity - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a payable function in blockchain smart contracts?
A payable function is a special type of function in smart contracts that can receive cryptocurrency (like Ether) when called. It allows the contract to accept and handle payments.
Click to reveal answer
beginner
Why do we need to mark a function as payable?
Marking a function as payable tells the blockchain that this function can accept money. Without it, sending money to the function will cause the transaction to fail.
Click to reveal answer
beginner
What happens if you send Ether to a non-payable function?
The transaction will be rejected and the Ether will not be transferred. The contract will not accept the payment because the function is not marked as payable.
Click to reveal answer
intermediate
How can you access the amount of Ether sent to a payable function?
Inside a payable function, you can use msg.value to get the amount of Ether (in wei) sent with the transaction.
Click to reveal answer
beginner
Show a simple example of a payable function in Solidity.
Example:<br><pre>function deposit() public payable {
    // Function can receive Ether
    balance[msg.sender] += msg.value;
}</pre>This function accepts Ether and updates the sender's balance.
Click to reveal answer
What keyword must a Solidity function have to accept Ether?
Apayable
Bexternal
Cview
Dpure
What does msg.value represent inside a payable function?
AThe block timestamp
BThe sender's address
CThe gas price
DThe amount of Ether sent with the transaction
What happens if you send Ether to a function NOT marked payable?
ATransaction fails and reverts
BEther is accepted anyway
CFunction runs but ignores Ether
DEther is sent back automatically
Which of these is a correct way to declare a payable function in Solidity?
Afunction buy() public {}
Bfunction buy() public payable {}
Cfunction buy() payable {}
Dfunction buy() external view payable {}
Can a constructor be payable in Solidity?
ANo, constructors cannot be payable
BOnly if marked external
CYes, constructors can be payable
DOnly in older Solidity versions
Explain what a payable function is and why it is important in smart contracts.
Think about how contracts get money from users.
You got /3 concepts.
    Describe how to access the amount of Ether sent to a payable function and give a simple example.
    Look inside the function for the special variable.
    You got /3 concepts.