3.24 Visibility
Remember that functions in Solidity
have the notion of visibility where they could be either public
, external
, internal
or private
, this affects which users can call these functions.
So public
and external
functions are callable by anyone depending on the access control that is enforced on top of that, whereas internal
and private
can be called only from within the contracts or the derived contracts.
Until Solidity
version 0.5.0
this visibility specifier was optional and they defaulted to public
. This aspect led to vulnerabilities where the developer forgot to mention or specify the visibility in which case it became public by default and resulted in malicious users being able to call these functions and make unauthorized state changes completely unexpected by the developer or the smart partner.
So this optional specification of function visibility defaulting to public
visibility was removed as of Solidity
version 0.5.2
, so this was a big change when it came to increasing the security of smart contracts and since that version function visibility is required to be specified explicitly for every function.
Public Functions
Remember that Solidity
has the notion of visibility for functions, there are four visibility specifiers: internal
, private
, public
and external
. public
functions consume more Gas than external
functions.
The reason for this is because the arguments of public
functions need to be copied from thecall data component of the EVM to the memory component. This copying produces more bytecode for such public
functions which therefore consumes more Gas.
This copying is not required for external
functions where their arguments can be left behind in the calldata component of the EVM. This key difference leads to public
functions consuming more Gas than external
functions in Solidity
.
So if there are functions in the contract that are never called from within the contracts themselves, then such functions should be declared with external
visibility and not public
visibility, which leads to better Gas efficiency.
Last updated