当前位置:网站首页>Solid function learning
Solid function learning
2022-07-07 17:24:00 【Hua Weiyun】
Catalog
Return value ( Or return variable )
Supported return parameters and types
State variability (mutability)
Today I'd like to share with you Solidity Learning functions in
function
Solidity The function in is :
They can be written inside the contract , It can also be a free function ( Written outside the contract ).
Return value ( Or return variable )
Function can return any number of values as output . There are two ways to return variables from functions :
1. Use the return variable name :
2. Directly in return Provide the return value in the statement :
Use the second method , You can omit the name of the return variable , Instead, specify only its type .
Supported return parameters and types
To call the smart contract function , We need to use ABI Specification to specify the function to be called and code the parameters , These parameters will be included in the data field of the transaction and sent to the Ethereum network for execution .
ABI Encoding is also used for events and return types , More details can be found in .
Be careful : ( If you're using Solidity edition :0.7.5 Can be used v2 edition . For lower than 0.7.5 Version of , We need to use the experimental version :pragma experimental ABIEncoderV2;).
Use Solidity 0.7.5 An example of this version .
visibility (Visibility)
There are four kinds of visibility of functions :
- Private( private ): The most restrictive , Function can only be called inside the defined smart contract .
- Internal( Inside ): This function can be called inside the defined smart contract , You can also call this function from the inheritance contract. .
- External( external ): Can only be called from outside the smart contract . ( If you want to call it from an intelligent contract. , Must be used
this.) - Public( Open ): It can be called from anywhere . ( Most relaxed )
State variability (mutability)
- view: use
viewThe declared function can only read the State , You can't change the status . - pure: use
pureThe declared function can neither read nor modify the state . - payable: use
payableThe declared function can accept the Ethernet currency sent to the contract , If not specified , This function will automatically reject all ethers sent to it .
Transactions and calls
use view and pure The function defined by the keyword will not change the state of Ethereum blockchain , This means that when you call these functions , You will not send any transactions to the blockchain , Because transactions are defined as status bar transitions from one state to another . It's just , The node you connect executes the function code locally by checking its own blockchain version , And return the result , There is no need to broadcast any transactions to the Ethereum network .
Special functions
In this section , We will see some special functions that can be used .
Getter function
Defined as public The state variable of has getter function , This function is automatically created by the compiler . This function has the same name as the variable , And it has external visibility .
Receive ether function
A contract can have at most one receive function . This function cannot have arguments , Cannot return any parameters , And it has to have receive Visibility and payable State variability .
When sent to contract Ether And no function is specified to call (calldata It's empty ) When the . This is a function executed on an ordinary Ethereum transfer ( for example , adopt .send() or .transfer() Transfer accounts ).
The function is declared as follows :
Fallback function
A contract can have at most one fallback function ( Fallback function ). This function cannot have arguments , Cannot return any parameters , And it has to have external visibility . If None of the other functions match the given function signature , perhaps No data was provided at all and no receive function , Execute this function when calling the contract .
You can declare a function like this :
If one is not implemented
receiveFunction orpayableContract transfer of fallback function , The contract will throw an exception , Ethereum will be refunded .
Can be in Remix Try it by yourself , Create one without receive or payable fallback The contract of , And send some ether coins to them . Click on **Transact( transaction )** after , You should see a message similar to the following .
Function modifiers
When you want to check some conditions before executing a function , You can use decorators . for example , If you want to check whether the sender is the owner of the contract , You can write the following :
Use decorators , We can separate the code , So that we can reuse it with other functions , We just need to declare modifiers , As shown below :
Then add the modifier name to the function :
By specifying multiple modifiers in a space separated list , You can apply them to a function , And apply it in the order given .
边栏推荐
- 测试用例管理工具推荐
- Notes on installing MySQL in centos7
- Lowcode: four ways to help transportation companies enhance supply chain management
- 【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程
- Direct dry goods, 100% praise
- MySQL implements the query of merging two fields into one field
- 电脑无法加域,ping域名显示为公网IP,这是什么问题?怎么解决?
- Siggraph 2022 best technical paper award comes out! Chen Baoquan team of Peking University was nominated for honorary nomination
- 【Seaborn】组合图表:PairPlot和JointPlot
- On Apache Doris Fe processing query SQL source code analysis
猜你喜欢

User defined view essential knowledge, Android R & D post must ask 30+ advanced interview questions

skimage学习(1)

《产品经理必读:五种经典的创新思维模型》的读后感

赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性

Sort out several important Android knowledge and advanced Android development interview questions

Leetcode brush questions day49

最新阿里P7技术体系,妈妈再也不用担心我找工作了

Test case management tool recommendation

Sator推出Web3游戏“Satorspace” ,并上线Huobi

浅浅理解.net core的路由
随机推荐
正在准备面试,分享面经
Arduino 控制的双足机器人
[fan Tan] after the arrival of Web3.0, where should testers go? (ten predictions and suggestions)
Smart logistics platform: make overseas warehouses smarter
Flask搭建api服务-SQL配置文件
编程模式-表驱动编程
[source code interpretation] | source code interpretation of livelistenerbus
Flash build API service
LeetCode 1654. The minimum number of jumps to get home one question per day
Seaborn data visualization
Matplotlib绘图界面设置
LeetCode刷题day49
QT视频传输
Biped robot controlled by Arduino
NeRF:DeepFake的最终替代者?
[Huang ah code] Why do I suggest you choose go instead of PHP?
DevOps 的运营和商业利益指南
LeetCode 312. 戳气球 每日一题
Localstorage and sessionstorage
LeetCode 152. 乘积最大子数组 每日一题
