# Why do we need to specify constant in the return value [duplicate]

This question already has an answer here:

Referring to the constant below:

  function totalSupply() constant returns (uint256 totalSupply);


## marked as duplicate by Ismael, jeff, niksmac, Badr Bellaj♦, Tjaden Hess♦Jul 9 '17 at 18:21

• – eth Jul 9 '17 at 2:47

From Solidity docs:

Functions can be declared constant in which case they promise not to modify the state.

If a function doesn't modify the state of blockchain (simply getters), you should add constant to function definition.

The main reason to do so is to indicate web3js (or any other json-rpc client) to use eth_call function instead of eth_sendTransaction.

The constant modifier needs to be added manually by a programmer and compiler won't enforce adding it even if the state is not being modified (read details in docs ).

Remember that you are not charged for calling constant methods but only from off-chain context (like using web3js client). If you call them inside any other contract method you will still be charged.

Why do we need to specify constant in the return value

Just to be pedantic: the return value isn't being declared as constant, the function is, as the document snippet in the other answer has pointed out.

Adding constant you're telling to the compiler that this function is going to perform only read actions (calls instead of transactions), and calls have no gas costs. So if you have to set up a getter function just add the property constant.

function getBalance() constant returns(uint) {
return balance;
}