2

The ERC20 token standard specifies balanceof() to return a constant.

function balanceOf(address _owner) constant returns (uint256 balance)

But the balance amount might change or am I getting something wrong here?

4

The constant keyword does not specify what the function returns. A function marked as constant like this one tells you that the function will return a value and will not modify any state variables. A large use for the constant/view keywords is to declare a function that can be called without sending a transaction, i.e. a free "query" of the blockchain.

http://solidity.readthedocs.io/en/develop/contracts.html#view-functions

| improve this answer | |
  • Why would it be wrong not to use constant in balanceof()? The function itself doesn't change any state, it only returns return balances[_owner]; – baermathias Nov 10 '17 at 14:00
  • It wouldn't be wrong. You're right to have constant there. – Malone Nov 10 '17 at 23:06
  • 1
    What is "constant" is the function, which in Solidity refers to a function that doesn't modify the state of the blockchain, just reads values. Not to be confused with a constant variable, whose value can't be changed once declared. – pabloruiz55 Nov 10 '17 at 23:09
1

constant on functions is an alias to view, so when executed you can view the contents that function returns

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.