Hey folks, in this article we're gonna explore the differences between require, revert, and assert functions in Solidity! If you aren't aware about Solidity, then you can get a brief idea about it by clicking here.
What is require function?
The require function is used to verify some conditions before any statement is executed. If the given condition tends to be false, then it would give an error and the execution would stop.
In short, require statements declare the prerequisites for running the function, which has to be satisfied to work.
During the event, the execution is terminated due to a false condition, the unused gas is returned to the caller and the state is reversed to the original state.
require(num == 23, "The number entered is incorrect!");
Imply conditions before execution.
Authenticate user responses.
What is revert function?
The revert function does not evaluate any condition and does not depend on any state or statement.
It is used for handling more complex operations.
When a revert statement is called the unused gas is returned and the state reverts to its original state.
revert("There's an error!");
Unused gas is returned.
Capable of handling complicated operations.
What is assert function?
The assert function is used to check those statements which can never be false.
It plays a vital role in preventing impossible scenarios. The output is declared in boolean form.
In contrast to require and revert functions, assert doesn't return any unused gas instead it consumes the gas to reverse the program to its original state.
assert(value > 345);
Prevents impossible scenarios.
Used for cases where statements can never be false.
I hope that you must have found this article quite helpful. If yes, then do give a read to some of my other articles!
Who knows you might become a great programmer 🤔!