当前位置:网站首页>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
view
The declared function can only read the State , You can't change the status . - pure: use
pure
The declared function can neither read nor modify the state . - payable: use
payable
The 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
receive
Function orpayable
Contract 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 .
边栏推荐
- Localstorage and sessionstorage
- 如何在软件研发阶段落地安全实践
- 电脑无法加域,ping域名显示为公网IP,这是什么问题?怎么解决?
- [source code interpretation] | source code interpretation of livelistenerbus
- Solidity函数学习
- 服务器彻底坏了,无法修复,如何利用备份无损恢复成虚拟机?
- 【Seaborn】组合图表:PairPlot和JointPlot
- Flash build API Service - generate API documents
- 《产品经理必读:五种经典的创新思维模型》的读后感
- [fan Tan] after the arrival of Web3.0, where should testers go? (ten predictions and suggestions)
猜你喜欢
QML初学
Mrs offline data analysis: process OBS data through Flink job
Sort out several important Android knowledge and advanced Android development interview questions
Module VI
QT picture background color pixel processing method
PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight
SlashData开发者工具榜首等你而定!!!
Skimage learning (3) -- adapt the gray filter to RGB images, separate colors by immunohistochemical staining, and filter the maximum value of the region
Sator推出Web3游戏“Satorspace” ,并上线Huobi
麒麟信安云平台全新升级!
随机推荐
[Seaborn] combination chart: facetgrid, jointgrid, pairgrid
DevOps 的运营和商业利益指南
LeetCode 1696. 跳跃游戏 VI 每日一题
Solidity 开发环境搭建
The top of slashdata developer tool is up to you!!!
LeetCode 403. 青蛙过河 每日一题
LeetCode 1696. Jumping game VI daily question
Seaborn数据可视化
专精特新软件开发类企业实力指数发布,麒麟信安荣誉登榜
Pycharm IDE下载
LeetCode 1031. 两个非重叠子数组的最大和 每日一题
LeetCode 1186. 删除一次得到子数组最大和 每日一题
Smart logistics platform: make overseas warehouses smarter
[Fantan] how to design a test platform?
【黄啊码】为什么我建议您选择go,而不选择php?
rpcms获取指定分类下的文章的方法
DNS 系列(一):为什么更新了 DNS 记录不生效?
LeetCode 1774. The dessert cost closest to the target price is one question per day
Repair method of firewall system crash and file loss, material cost 0 yuan
centos7安装mysql笔记