当前位置:网站首页>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 .
边栏推荐
- 从DevOps到MLOps:IT工具怎样向AI工具进化?
- DNS series (I): why does the updated DNS record not take effect?
- LeetCode 1155. 掷骰子的N种方法 每日一题
- 测试用例管理工具推荐
- Lowcode: four ways to help transportation companies enhance supply chain management
- Nerf: the ultimate replacement for deepfake?
- How to choose the appropriate automated testing tools?
- 编程模式-表驱动编程
- How to mount the original data disk without damage after the reinstallation of proxmox ve?
- 【黄啊码】为什么我建议您选择go,而不选择php?
猜你喜欢
Direct dry goods, 100% praise
正在准备面试,分享面经
Sator launched Web3 game "satorspace" and launched hoobi
最新阿里P7技术体系,妈妈再也不用担心我找工作了
Pychart ide Download
A tour of grpc:03 - proto serialization / deserialization
Sator推出Web3游戏“Satorspace” ,并上线Huobi
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
Seaborn data visualization
PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight
随机推荐
[Fantan] how to design a test platform?
Flask搭建api服务-生成API文档
【Seaborn】组合图表:PairPlot和JointPlot
Mrs offline data analysis: process OBS data through Flink job
Solidity 开发环境搭建
LeetCode-SQL第一天
Nerf: the ultimate replacement for deepfake?
LeetCode 1986. 完成任务的最少工作时间段 每日一题
Leetcode brush questions day49
QT视频传输
How to implement safety practice in software development stage
自定义View必备知识,Android研发岗必问30+道高级面试题
What is cloud computing?
浅浅理解.net core的路由
赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性
Master this promotion path and share interview materials
如何选择合适的自动化测试工具?
LeetCode 1049. 最后一块石头的重量 II 每日一题
Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
Pycharm IDE下载