Back to homepage

Solidity fundamentals: require conditional


The require conditional is very common in Solidity as it stops the exeution of a function if a condition is not true.

Table of contents

Require conditional

The require conditional is very used in Solidity. It makes it so that the function will throw an error and stop executing if the condition is not true. See an example below 👇

pragma solidity ^0.8.4;

contract LotteryContract {

  uint256 ticketPrice = 0.1 ether;

  function playLottery(uint256 _number) public payable returns (memory string) {
    // validate value sent, return error message
    require(msg.value >= ticketPrice, "Minimum value is 0.1 ETH!");
    // continue code...
    return 'You won!'

If you call the function playLottery() but the user didn't send enough ETH, it will throw an error with the message "Minimum value is 0.1 ETH!". However if you call it with 0.1ETH or more , it will return, the function will continue executing (in the example above it'll just return "You won!").

The require conditional is very useful for verifying certain conditions that must be true before running a function and you'll proabably use it quite often in your smart contracts 😉


If you enjoyed this article consider sharing it on social media or buying me a coffee ✌️

Buy Me A Coffee