Solidity Functions
A function is basically a group of code that can be reused anywhere in the program. In Solidity, a function is a set of instructions that can be executed and called by other parts of the contract or by external contracts or accounts. Functions in Solidity are similar to functions in other programming languages; they can take inputs, perform computations, and return outputs.
Here is an example of a simple function in Solidity.
In this example, the setNumber
function takes an input parameter _number
of type uint
and sets the value of the public state variable myNumber to that input value. The function is marked as public
, which means that it can be called by any external account or contract.
Function2.sol
is another basic example of writing a function. Here, the function adds two numbers. It will take two inputs, and it’s going to return the sum.
Function visibility
A variable, function, or contract's visibility describes how far it may be accessed from beyond the section of code where it was defined. Depending on whatever parts of the software system require access, the scope of visibility can be changed.
According to Solidity documentation, Solidity knows two kinds of function calls: external ones that do create an actual EVM message call and internal ones that do not. Furthermore, internal functions can be made inaccessible to derived contracts. This gives rise to four types of visibility for functions. They are; external
, public
, internal
, and, private
.
external
External functions are part of the contract interface, which means they can be called from other contracts and via transactions. An external function f cannot be called internally (i.e. f() does not work, but this.f() works). Functions marked as external
can only be called from outside the contract. They cannot be called by other functions within the same contract or by derived contracts.
public
Public functions are part of the contract interface and can be either called internally or via message calls. Functions marked as "public" can be called from anywhere, both within and outside the contract. They are part of the contract's external interface and can be called by other contracts or by accounts outside the blockchain.
internal
Internal functions can only be accessed from within the current contract or contracts derived from it. They cannot be accessed externally. Since they are not exposed to the outside through the contract’s ABI, they can take parameters of internal types like mappings or storage references.
private
Private functions are like internal ones, but they are not visible in derived contracts.
Image source: freecodecamp
Example of function visibility and how it is used in code is shown below
Pure and view function
In Solidity, functions can be marked as "view
" or "pure
" to indicate that they do not modify the state of the contract. A view
function can be called internally by other functions or externally by external accounts or contracts, and it does not require any gas to execute when called externally. View
functions can read data from the blockchain, while pure
functions do not read anything from the blockchain. Functions can be declared pure
in which case they promise not to read from or modify the state. The view
function does not modify or write anything to the blockchain. It reads some kind of data from the blockchain.
This code shows the pure and view functions.
Function modifier
A modifier is a snippet of code that can run automatically before or after you run the main function if the modifier is applied to the function. Modifiers can be used to change the behavior of functions in a declarative way. For example, you can use a modifier to automatically check a condition prior to executing the function. They provide a way to add additional functionality or restrictions to functions without duplicating code.
Modifiers are inheritable properties of contracts and may be overridden by derived contracts, but only if they are marked virtual.
Function modifiers can override each other. This works in the same way as function overriding (except that there is no overloading for modifiers). The virtual keyword must be used on the overridden modifier, and the override keyword must be used in the overriding modifier. Modifiers can be inherited from parent contracts too. It is generally used as a way to avoid repeating your code, by extracting common functionality and putting it in a modifier that can be reused throughout the codebase.
Modifiers are defined using the modifier keyword, followed by a name and a block of code. They can be applied to functions by using the modifierName syntax. When a function with a modifier is called, the code inside the modifier is executed first, and if it passes the conditions, the function body is executed. If the modifier code fails the conditions, an exception is thrown, and the function is not executed.
Functions are very important in smart contracts. Follow me to see the next article on this 100 days of solidity.
In case you missed solidity data types, click here.
Also, click here to learn more about variables and control structures in solidity.
Click here to see the Github repo for this 100 days of solidity challenge.
Click here for guidelines for becoming a Solidity developer.
To learn more about blockchain, click here.
To learn more about Web3, click here.
Follow me for more knowledge on Solidity, Ethereum, and blockchain.