To give a little more context a constant declaration indicates that the function will not change the state of the contract (although currently this is not enforced by the compiler).
When generating the compiled binaries, declaring a function constant
is reflected on the ABI. The ABI is then interpreted by web3 to figure out whether it should send a sendTransaction()
or a call()
message to the Ethereum node. Since calls are only executed locally, they're effectively free.
See this snippet from the web3.js library:
/**
* Should be called to execute function
*
* @method execute
*/
SolidityFunction.prototype.execute = function () {
var transaction = !this._constant;
// send transaction
if (transaction) {
return this.sendTransaction.apply(this, Array.prototype.slice.call(arguments));
}
// call
return this.call.apply(this, Array.prototype.slice.call(arguments));
};
Calling a constant function from another contract incurs the same cost as any other regular function.