Smart Contract Language Solidity Tutorial Series 3-function type

Source: Internet
Author: User

Solidity Tutorial Series III-Introduction to the Solidity function type.
For a complete list of articles in the solidity series, see Category-solidity.

Write in front

Solidity is the Ethereum Smart Contract programming language, before reading this article, you should have an understanding of ethereum, smart contracts, if you do not understand, it is recommended that you first see what Ethereum is

The first half of this article is a reference to the solidity official document (currently the latest version: 0.4.20) for translation, the latter part of function visibility (public, external, internal, privite) in-depth analysis.

function type (functions 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

The internal (internal) function can only be called within the current contract (within the current code block, including internal library functions, and inherited functions).
an external (external) function consists of an address and a function method signature, which can be used as an argument 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>)]

Omit returns (<return types>) If the function does not need to be returned
The function type is internal by default, so internal can be omitted. In contrast, the function itself is public by default in the contract, and is internal by default only when used as a type name.

There are two ways to access functions, one is to use the function name Fdirectly, and the other is this.f, which is used for intrinsic functions and the latter for external functions.

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

If external function types are used outside of the context of solidity, they are considered function types. It encodes the address of the 20-byte function, along with the 4-byte function method signature that precedes it as the bytes24 type.
The public function in the contract can be invoked in two ways, using internal and external.
Internal Access Form F, external access in the form of this.f

Member: Attribute selector

The public (or external) function has a special member selector, which corresponds to an ABI function selector.

    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 be used in internal library functions because// They'll be part of the same Code context function map (uint[] memory self, function (UINT) pure returns (UINT) f) int    Ernal 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 RET    Urns (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 the 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);  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  }}
Analysis of function visibility
    • Public-Any Access
    • Private-only within the current contract
    • Internal-only the current contract and the inherited contract
    • External-External access only (external access is only available internally)
Public or external best practices Reference Document

Solidity official documents-type

In-depth blockchain-the system learns blockchain to create the best blockchain technology blog.

Smart Contract Language Solidity Tutorial Series 3-function type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.