-
bitcoin $87959.907984 USD
1.34% -
ethereum $2920.497338 USD
3.04% -
tether $0.999775 USD
0.00% -
xrp $2.237324 USD
8.12% -
bnb $860.243768 USD
0.90% -
solana $138.089498 USD
5.43% -
usd-coin $0.999807 USD
0.01% -
tron $0.272801 USD
-1.53% -
dogecoin $0.150904 USD
2.96% -
cardano $0.421635 USD
1.97% -
hyperliquid $32.152445 USD
2.23% -
bitcoin-cash $533.301069 USD
-1.94% -
chainlink $12.953417 USD
2.68% -
unus-sed-leo $9.535951 USD
0.73% -
zcash $521.483386 USD
-2.87%
What is the difference between a payable function and a non-payable function in Solidity?
In Solidity, only functions marked `payable` can receive Ether, preventing accidental or malicious fund transfers and ensuring secure contract design.
Nov 14, 2025 at 01:59 pm
Understanding Function Modifiers in Solidity
In Solidity, functions within smart contracts can be assigned specific modifiers that dictate how they interact with Ether. These modifiers play a crucial role in determining whether a function can receive funds during execution. The distinction between payable and non-payable functions lies in their ability to accept Ether as part of a transaction.
Payable Functions: Accepting Ether Transfers
1. A payable function is explicitly marked with the payable modifier, allowing it to receive Ether when called.
- When a user sends Ether along with a transaction that invokes a payable function, the funds are transferred to the contract’s balance.
- Without the payable modifier, attempting to send Ether to a function results in a transaction failure.
- Payable functions are essential for features like crowdfunding, donations, or any mechanism where users contribute funds directly through function calls.
- Inside a payable function, developers can access the amount sent using msg.value, which holds the number of wei received.
Non-Payable Functions: Rejecting Incoming Ether
1. By default, functions in Solidity are non-payable unless specified otherwise.
- If a transaction attempts to send Ether to a non-payable function, the entire transaction is reverted to prevent accidental loss of funds.
- This behavior protects both users and contract logic from unintended Ether transfers.
- Non-payable functions are suitable for operations that only modify state or read data without requiring financial input.
- Even if a function doesn’t expect Ether, calling it with msg.value > 0 will cause a runtime exception if it's not marked payable.
Practical Implications in Smart Contract Design
1. Mislabeling a function as non-payable when it should accept funds can break core functionality, such as token purchases or staking mechanisms.
- Conversely, marking unnecessary functions as payable increases attack surface, especially if proper validation around msg.value is missing.
- Developers must carefully audit all external-facing functions to ensure correct handling of Ether flows.
- Tools like static analyzers and testing frameworks help detect incorrect use of the payable modifier before deployment.
- In complex contracts, distinguishing between fund-receiving and utility functions improves code clarity and security.
Frequently Asked Questions
Can a constructor be payable?Yes, a constructor can be marked as payable. This allows the contract to accept Ether during deployment. If the deployment transaction includes a value transfer, the constructor must be payable to avoid reverting.
What happens if I call a non-payable function with Ether?The transaction will automatically revert, and no state changes occur. Ethereum enforces this rule at the EVM level to prevent accidental Ether transfers to functions not designed to handle them.
Is there a way to make all functions in a contract payable by default?No, each function must individually be declared as payable. There is no global setting to make every function accept Ether. This design ensures intentional and secure handling of fund reception.
Can fallback functions receive Ether if they are not payable?No, even fallback functions must be marked as payable to accept Ether. A non-payable fallback function will reject any incoming Ether sent to the contract without specifying a function call.
Disclaimer:info@kdj.com
The information provided is not trading advice. kdj.com does not assume any responsibility for any investments made based on the information provided in this article. Cryptocurrencies are highly volatile and it is highly recommended that you invest with caution after thorough research!
If you believe that the content used on this website infringes your copyright, please contact us immediately (info@kdj.com) and we will delete it promptly.
- Penny Value, Precious Metals, and Coin Clubs: A Collector's Rollercoaster Ride
- 2026-02-08 10:00:02
- Dogecoin's Meme-Coin Renaissance: Lunar Dreams Collide with Crypto Realities
- 2026-02-08 10:10:02
- Bitcoin Blunder: Crypto Firm Bithumb's Massive Mistaken Transfer Sparks User Compensation and Regulatory Scrutiny
- 2026-02-08 07:35:01
- Ripple's XRP Ledger Set to Revolutionize Finance with Lending Protocols and Confidential Transactions
- 2026-02-08 07:20:02
- Bitcoin's Taker Ratio Signals Deep Bearish Sentiment Amidst Market Turmoil
- 2026-02-08 07:35:01
- KDN Fuels Klardven's DeFi Ascent: Demand Soars, FOMO Follows in a Maturing Market
- 2026-02-08 07:20:02
Related knowledge
How to Maximize Leverage Safely for Day Trading Crypto?
Feb 08,2026 at 01:19am
Understanding Leverage Mechanics in Crypto Derivatives1. Leverage multiplies both potential gains and losses by allowing traders to control larger pos...
How to Trade Ethereum Futures Before and After Major Upgrades?
Feb 08,2026 at 09:40am
Understanding Ethereum Futures Mechanics1. Ethereum futures contracts are standardized agreements to buy or sell ETH at a predetermined price and date...
How to Use "Mark Price" vs. "Last Price" to Prevent Liquidation?
Feb 07,2026 at 05:39pm
Understanding Mark Price Mechanics1. Mark price is a composite value derived from multiple spot exchange indices and funding rate adjustments, designe...
How to Calculate "Return on Equity" (ROE) in Leverage Trading?
Feb 08,2026 at 04:39am
Understanding Return on Equity in Leverage Trading1. Return on Equity (ROE) in leverage trading measures the profitability generated relative to the t...
How to Use "Post-Only" Orders to Ensure You Are a Market Maker?
Feb 08,2026 at 04:00am
Understanding Post-Only Order Mechanics1. A post-only order is a type of limit order that executes exclusively as a maker—never as a taker. 2. If the ...
How to Trade Bitcoin Quarterly Delivery Contracts for Low Fees?
Feb 08,2026 at 04:19am
Understanding Bitcoin Quarterly Delivery Contracts1. Bitcoin quarterly delivery contracts are standardized futures instruments that settle on a fixed ...
How to Maximize Leverage Safely for Day Trading Crypto?
Feb 08,2026 at 01:19am
Understanding Leverage Mechanics in Crypto Derivatives1. Leverage multiplies both potential gains and losses by allowing traders to control larger pos...
How to Trade Ethereum Futures Before and After Major Upgrades?
Feb 08,2026 at 09:40am
Understanding Ethereum Futures Mechanics1. Ethereum futures contracts are standardized agreements to buy or sell ETH at a predetermined price and date...
How to Use "Mark Price" vs. "Last Price" to Prevent Liquidation?
Feb 07,2026 at 05:39pm
Understanding Mark Price Mechanics1. Mark price is a composite value derived from multiple spot exchange indices and funding rate adjustments, designe...
How to Calculate "Return on Equity" (ROE) in Leverage Trading?
Feb 08,2026 at 04:39am
Understanding Return on Equity in Leverage Trading1. Return on Equity (ROE) in leverage trading measures the profitability generated relative to the t...
How to Use "Post-Only" Orders to Ensure You Are a Market Maker?
Feb 08,2026 at 04:00am
Understanding Post-Only Order Mechanics1. A post-only order is a type of limit order that executes exclusively as a maker—never as a taker. 2. If the ...
How to Trade Bitcoin Quarterly Delivery Contracts for Low Fees?
Feb 08,2026 at 04:19am
Understanding Bitcoin Quarterly Delivery Contracts1. Bitcoin quarterly delivery contracts are standardized futures instruments that settle on a fixed ...
See all articles














