A quick reference for all your function declaring needs.
external
, public
, internal
, and private
. These labels represent a further division of the public and private labels you might use in another language.
external
visibility are only callable from other contracts and cannot be called within their own contract. You may see older references stating you should use external
over public
because it forces the function to use calldata
. This is no longer correct, because both function visibilities can now use calldata
and memory
for parameters. However, using calldata
for either will cost less gas.
private
and internal
operate nearly identically. Beyond writing hygienic code, these have a very important effect. Because they are not a part of the contract’s ABI, you can use mapping
s and storage
variable references as parameters.
The difference is that private
functions can’t be called from derived contracts. You’ll learn more about that when we cover inheritance.
Some developers prepend an underscore to private
and internal
functions.
pure
functions promise to neither read nor write state. They’re usually used for helper functions that support other functionality.
pure
functions can be called from outside the blockchain without using gas, if they are also public
or external
.
view
functions access state, but don’t modify it. You’ve used these for tasks such as returning all the values in an array.
view
functions can be called from outside the blockchain without using gas, if they are also public
or external
.
view
or pure
can modify state and the compiler will generate a warning if they do not.