Solidity: Smart Contract Languages Solidity Tutorial Series 3 – Function types The original text has been updated to read for real

Part 3 of the Solidity tutorial series – Solidity function types. For a complete list of articles in the Solidity series, see the category -Solidity.

Writing in the front

Solidity is the Ethereum Smart Contract programming language. Before reading this, you should know something about Ethereum, smart contracts, and if you don’t already, what is Ethereum

The first half of this article is translated from the official Solidity document (latest version: 0.4.20) and the second half is an in-depth analysis of functions visibility (public, External, Internal, privite) (for column subscribers only).

Function Types

Functions are also a type and are of value type. A function can be assigned to a variable of a function type. You can also pass a function as an argument. You can also return a function in a function call. There are two types of function types: internal and external functions

Internal functions can only be called within the current contract (within the current code block, including internal library functions, and inherited functions). An external function consists of an address and a function method signature that can be used as an argument or return value to an external function call.

Function types are defined as follows:

function (<parameter types>) {internal|external} [pure|constant|view|payable] [returns (<return types>)]
Copy the code

If the function does not need to return, omit returns (). The default function type is internal, so internal can be omitted. In contrast, functions in the contract themselves are public by default and internal only when used as type names.

There are two ways to access functions. One is to use the function name f directly, and the other is this.f, which is used for internal functions and the other is used for external functions.

If a function variable is not initialized, calling it directly will raise an exception. The same exception occurs if a function is called after delete.

If external function types are used outside of the Solidity context, they are treated as function types. It encodes the 20-byte address of the function, along with the 4-byte function method signature that precedes it, as type Bytes24. Public functions in the contract can be called either internal or external. The internal access form is f and the external access form is this.f

Member: property selector

Public (or external) functions have a special selector member that corresponds to an ABI function selector. ` ` ` js pragma solidity ^ 0.4.16;

contract Selector { function f() public view returns (bytes4) { return this.f.selector; }} ` ` `Copy the code

The following code shows the use of the internal function type:

pragma solidity ^0.416.;

library ArrayUtils {
  // internal functions can be used in internal library functions because
  // they will be part of the same code context
  function map(uint[] memory self, function (uint) pure returns (uint) f)
    internal
    pure
    returns (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) {
    returnx + y; }}Copy the code

The following code shows the use of the external function type:

Pragma solidity ^ 0.4.11; contract Oracle { struct Request { bytes data; function(bytes memory) external callback; } Request[] requests; event NewRequest(uint); function query(bytes data, function(bytes memory) external callback) public { requests.push(Request(data, callback)); NewRequest(requests.length - 1); } function reply(uint requestID, bytes response) public { // Here goes the check that the reply comes from a trusted source requests[requestID].callback(response); } } contract OracleUser { Oracle constant oracle = Oracle(0x1234567); // known contract function buySomething() { oracle.query("USD", this.oracleResponse); } function oracleResponse(bytes response) public { require(msg.sender == address(oracle)); // Use the data } }Copy the code

Function visibility analysis

  • Public – Arbitrary access
  • Private – Only in the current contract
  • Internal – Only current contracts and inherited contracts
  • External – Externally accessible only (internally, externally accessible only)

Public or External best practices

Please subscribe to blockchain Technology to see.

Refer to the video

We also have the most comprehensive video course on the market: In-depth Solidity, the Ethereum Smart Contract language. We are currently looking for course experiential experts, check out the link.

Reference documentation

Official Solidity documentation – Type

Dynamic And Simple Blockchain – Learn blockchain systematically and build the best blockchain technology blog.

Dynamic Planet Of My Knowledge answers blockchain technology questions. Welcome to join the discussion.

Aid To The Public account “Deep And Simple Blockchain Technology” To obtain blockchain technology information for the first time.