Skip to content

Commit

Permalink
Document constant functions
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Oct 19, 2016
1 parent f0f2d5a commit 1b37137
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
27 changes: 24 additions & 3 deletions docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,9 @@ change by overriding).

.. index:: ! constant

**********
Constants
**********
************************
Constant State Variables
************************

State variables can be declared as constant (this is not yet implemented
for array and struct types and not possible for mapping types).
Expand All @@ -442,6 +442,27 @@ for these variables and every occurrence is replaced by their constant value.

The value expression can only contain integer arithmetics.

******************
Constant Functions
******************

Functions can be declared constant. These functions promise not to modify the state.

::

pragma solidity ^0.4.0;

contract C {
function f(uint a, uint b) constant returns (uint) {
return a * (b + 42);
}
}

.. note::
Accessor methods are marked constant.

.. warning::
The compiler does not enforce yet that a constant method is not modifying state.

.. index:: ! fallback function, function;fallback

Expand Down
14 changes: 0 additions & 14 deletions docs/frequently-asked-questions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,6 @@ Its type is the type of the contract. Since any contract type basically inherits
``address`` type, ``this`` is always convertible to ``address`` and in this case contains
its own address.

What is the difference between a function marked ``constant`` and one that is not?
==================================================================================

``constant`` functions can perform some action and return a value, but cannot
change state (this is not yet enforced by the compiler). In other words, a
constant function cannot save or update any variables within the contract or wider
blockchain. These functions are called using ``c.someFunction(...)`` from
geth or any other web3.js environment.

"non-constant" functions (those lacking the ``constant`` specifier) must be called
with ``c.someMethod.sendTransaction({from:eth.accounts[x], gas: 1000000});``
That is, because they can change state, they have to have a gas
payment sent along to get the work done.

Get a contract to return its funds to you (not using ``selfdestruct(...)``).
============================================================================

Expand Down

0 comments on commit 1b37137

Please sign in to comment.