Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  1. STEP 2

16: Required

Required is a keyword in Solidity that is used to make sure that functions are called with the correct amount of arguments. It will throw an error if the function is called with the wrong number of arguments.

contract ExampleContract {    
    function exampleFunction(uint a, uint b) public {        
        require(a > b);    
    }
}

In the above example, the require statement ensures that the argument a must be greater than b. If this is not the case, an error is thrown.

  • Example 1 Let's take a look at a simple example of using require to check if a certain value is greater than 0.

function checkValue(uint value) public {    
    require(value > 0);    // code executes if value is greater than 0
}

In this example, the require statement checks if the value passed to the function is greater than 0. If the value is not greater than 0, the require statement will cause the code to revert and the function will not execute.

  • Example 2 Let's look at another example of using require to check if two values are equal.

function checkValues(uint value1, uint value2) public { 
    require(value1 == value2); 
    // code executes if value1 is equal to value2
}

In this example, the require statement checks if the two values passed to the function are equal. If the values are not equal, the require statement will cause the code to revert and the function will not execute.

That's it for the lesson 16! In the next lesson, Assert

Previous15: For LoopNext17: Assert

Last updated 1 year ago