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?
✗ Incorrect
Only functions marked as
payable can receive Ether.What does
msg.value represent inside a payable function?✗ Incorrect
msg.value holds the amount of Ether (in wei) sent to the function.What happens if you send Ether to a function NOT marked payable?
✗ Incorrect
The transaction will fail because the function is not payable.
Which of these is a correct way to declare a payable function in Solidity?
✗ Incorrect
The correct syntax is to add
payable after visibility like public payable.Can a constructor be payable in Solidity?
✗ Incorrect
Constructors can be marked payable to accept Ether during contract creation.
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.