Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the type of Solidity function

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the type of Solidity function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Function type (Function Types)

Function is also a type and belongs to the value type. You can assign a function 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 functions: internal (internal) and external (external) functions

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

The function type is defined as follows:

Function () {internal | external} [pure | constant | view | payable] [returns ()]

If the function does not need to be returned, omit the returns () function type defaults to internal, so internal can be omitted. On the contrary, the function itself in the contract defaults to public, and only to internal when used as a type name.

There are two ways to access a function, one is directly using the function name f, and the other is this.f, the former for internal functions and the latter for external functions.

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

If external function types are used outside the context of Solidity, they are considered function types. It is encoded as the address of the 20-byte function, along with the 4-byte function method signature that precedes it as the bytes24 type. The function of public in the contract can be called in both internal and external. Internal access form is f, external access form is this.f

Members: attribute selector

The public (or external) function has a special member selector, which corresponds to an 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 internal (internal) function types:

Pragma solidity ^ 0.4.16 uint 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; iLibrary +) {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; iTunes +) {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.resume; iDepression +) {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 external (external) function types:

Pragma solidity ^ 0.4.11 X 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}} function visibility analysis

Public-any acc

Private-only within the current contract

Internal-only current contracts and inherited contracts

External-external access only (internal and external access only)

This is the end of the content of "what is the type of Solidity function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report