In earlier versions, we would apply the constant
modifier to indicate that a function doesn't change the storage state in any way. For example:
pragma solidity 0.4.16;
contract UseConstant {
string greeting;
function UseConstant() public {
greeting = "Hello";
}
function SayHello() public constant returns(string says) {
return greeting;
}
}
constant
indicates that network verification won't be necessary. Callers receive return
values (quickly, from local storage and processing) instead of transaction hashes.
Starting with solc 0.4.17, constant
is depricated in favor of two new and more specific modifiers.
View This is generally the replacement for constant
. It indicates that the function will not alter the storage state in any way.
Pure This is even more restrictive, indicating that it won't even read the storage state.
A pure
function might look something like this very contrived example:
function returnTrue() public pure returns(bool response) {
return true;
}
In Remix, you'll get a warning when you use the old constant
modifier. It will examine the function code and indicate the maximum restrictiveness you can safely apply.
Hope it helps.
UPDATE
This is a popular Q&A so I decided to elaborate on it in a more detailed Medium post: https://blog.b9lab.com/calls-vs-transactions-in-ethereum-smart-contracts-62d6b17d0bc2