Solidity Tutorial Series The third-solidity function type introduction. It 's written in front .
Solidity is the Ethernet Square Intelligent Contract programming language, before reading this article, you should have an understanding of the ether square, the intelligent contract, if you do not understand, suggest you first look at the etheric square is what
The first half of this article is for the reference solidity official documentation (current latest version: 0.4.20) for translation, the latter half of the function visibility (public, external, internal, privite) depth analysis (for column subscribers only). functions Type (function Types)
A function is also a type and belongs to a value type.
You can assign a function to a variable of a function type. You can also pass a function as a parameter. You can also return a function in a function call.
There are two classes of function types: internal (internal) and external (external) functions
Internal (internal) functions can only be invoked within the current contract (within the current code block, including internal library functions, and inherited functions).
The external (external) function consists of two parts of the address and function method signatures, either as arguments to an external function call, or as a return value.
The function type is defined as follows:
function (<parameter types>) {internal|external} [pure|constant|view|payable] [Returns (<return types>)]
If the function does not need to be returned, omit returns (
There are two ways to access functions, one is directly using the function name F, one is THIS.F, the former for internal functions, the latter for external functions.
If a function variable is not initialized, calling it directly will produce an exception. If you delete a function and then call it, the same exception occurs.
If external function types are used outside of the solidity context, they are treated as function types. It will encode the address of the 20-byte function, along with the 4-byte function method signature that preceded it as the Bytes24 type.
The public function in the contract can be invoked using internal and external two ways.
The internal access form is F, and the external Access form is THIS.F member: attribute selector
The public (or external) function has a special member selector, which corresponds to a ABI function selector.
"" JS
pragma solidity ^0.4.16;
Contract Selector {
function f () public view Returns (BYTES4) {return
this.f.selector;
}
}
```
The following code shows the use of the internal (internal) function type:
pragma solidity ^0.4.16; Library Arrayutils {//Internal functions can is used in internal library functions because//They'll be part of T He same the code context function map (uint[] memory self, function (UINT) pure returns (UINT) f) Internal Pure R
Eturns (uint[] Memory R) {r = new uint[] (self.length);
for (UINT i = 0; i < self.length; i++) {R[i] = f (self[i));
function reduce (uint[] memory self, function (uint, UINT) pure returns (UINT) f) Internal Pure
Returns (UINT R) {r = self[0];
for (UINT i = 1; i < self.length; i++) {R = f (r, Self[i]);
} function range (UINT length) internal pure Returns (uint[) memory R) {r = new uint[] (length);
for (UINT i = 0; i < r.length; i++) {r[i] = i;
}} contract Pyramid {using arrayutils for *;
Function pyramid (UINT L) public pure Returns (UINT) {return Arrayutils.range (L). Map (square). reduce (sum); } functiOn square (UINT x) Internal pure Returns (UINT) {return x * x;
function sum (uint x, uint y) internal pure Returns (UINT) {return x + y; }
}
The following code shows the use of an external (external) function type:
pragma solidity ^0.4.11;
Contract Oracle {
struct Request {
bytes data;
function (bytes memory) external callback;
}
Request[] requests;
Event Newrequest (UINT)