ETHEREUM SMART CONTRACTS DEVELOPMENT
Solidity Fundamentals: Functions
Functions: Part One
In this part we will be focusing on the following topics related Functions;
- Visibility and Getters
- Function Modifiers
- Constant and Immutable State Variables
- Function Parameters and Return Variables
- Pure, View Functions
Visibility and Getters
There are four types of visibility for Solidity functions and state variables (see: Data Location and Assignment Behaviors ): external, public, internal and private. external visibility is not possible for state variables.
external: These types of functions can only be invoked from the outside of a contract and via transactions. It is not possible to call f() external internally. When dealing with large arrays of data, these functions can be highly efficient since the data is not copied from calldata to memory(see: Data Location and Assignment Behaviors ).
public: Public functions can be called from any place — outside of a contract or internally. Getter function is generated automatically for public state variables.
internal: Those functions and state variables can only be called from within the current contract or contracts deriving from it.
private: Private functions and state variables can only be called the contract they are defined in.
Note: Everything that is inside a contract is visible to all observers external to the blockchain. Making something private only prevents other contracts from reading or modifying the information, but it will still be visible to the whole world outside of the blockchain.
With the usage of modifiers, we can check a condition prior to executing the function. They are inheritable properties of contracts and may be overridden by derived contracts, but only if they are marked virtual (wait for inheritance chapter in upcoming sections).
Note: We can apply multiple modifiers to functions by placing them in a whitespace-separated list and they are evaluated in the order presented.
Constant and Immutable State Variables
immutable and constant are keywords that can be used on state variables to restrict modifications to their state. The difference is that constant variables can never be changed after compilation, while immutable variables can be set within the constructor. It is also possible to declare constant variables in file level.
Note: The compiler does not reserve a storage slot for these variables, and every occurrence is replaced by the respective value (see: Data Location and Assignment Behaviors ).
Not all types for constants and immutables are implemented at this time. The only supported types are strings (only for constants) and value types.
Function Parameters and Return Variables
Functions can take typed parameters and compare to other languages, Solidity functions also return an arbitrary number of values as output.
Parameters are declared the same way as variables.
Note: An external function cannot accept a multi-dimensional array as an input parameter. This functionality is possible if you enable the new ABIEncoderV2 feature by adding pragma experimental ABIEncoderV2; to your source file.
An internal function can accept a multi-dimensional array without enabling the feature.
Return variables are declared with the same syntax as declaring parameters after the returns keyword and they can be used as any other local variable. Names of return variables can be omitted.
Note: You cannot return some types from non-internal functions, notably multi-dimensional dynamic arrays and structs. If you enable the new ABIEncoderV2 feature by adding pragma experimental ABIEncoderV2; to your source file then more types are available, but mapping types are still limited to inside a single contract and you cannot transfer them.
Pure, View Functions
view: Functions can be declared view in which case they don’t modify the state.
The following statements are considered modifying the state:
- Writing to state variables.
- Emitting events.
- Creating other contracts.
- Using selfdestruct.
- Sending Ether via calls.
- Calling any function not marked view or pure.
- Using low-level calls.
- Using inline assembly that contains certain opcodes.
Note: Getter methods are automatically marked view.
pure: Functions can be declared pure when they don’t read from or modify the state.
The following are considered reading from the state:
- Reading from state variables.
- Accessing address(this).balance or <address>.balance.
- Accessing any of the members of block, tx, msg (with the exception of msg.sig and msg.data).
- Calling any function not marked pure.
- Using inline assembly that contains certain opcodes.
Next we will continue Functions: Part Two — receive Ether function, fallback function, function overloading and events. Thanks for reading.
- Contracts - Solidity 0.7.4 documentation
- Solidity Fundamentals | Data Location and Assignment Behavior
Previous Parts in Order
- Layout of a Contract
- Value Types: Part One
- Value Types: Part Two
- Reference Types
- Data Location and Assignment Behaviors
- Control Structures
- Error Handling: Assert, Require, Revert and Exceptions
Feel free to ask any questions.
Stay safe, do good work, and keep in touch!
- What is a Flash loan?
- The Best Crypto Trading Bot
- 3Commas Review | Pionex Review | Coinrule review
- AAX Exchange Review | Deribit Review |FTX Crypto Exchange Review
- NGRAVE ZERO review
- Bybit Exchange Review | Bityard Review
- 3Commas vs Cryptohopper
- The Best Bitcoin Hardware wallet
- Ledger vs Ngrave
- Crypto Copy Trading Platforms
- ledger nano s vs x
- Vauld Review | YouHodler Review
- The Best Crypto Tax Software | CoinTracking Review
- Best Crypto Lending Platforms
- Ledger Nano S vs Trezor one vs Trezor T vs Ledger Nano X
- BlockFi vs Celsius vs Hodlnaut
- Bitsgap review | Quadency Review
- Ellipal Titan Review | SecuX Stone Review
- BlockFi Review | Earn up to 8.6% interests in your Crypto
- DEX Explorer and Blockchain APIs
- Crypto arbitrage guide: How to make money as a beginner
- Best Crypto Charting Tool
- What are the best books to learn about Bitcoin?