当前位置:网站首页>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 .
边栏推荐
- What is cloud computing?
- LeetCode 1031. Maximum sum of two non overlapping subarrays
- A tour of grpc:03 - proto serialization / deserialization
- Jenkins发布uniapp开发的H5遇到的问题
- How to mount the original data disk without damage after the reinstallation of proxmox ve?
- SlashData开发者工具榜首等你而定!!!
- 如何在博客中添加Aplayer音乐播放器
- LeetCode 1477. Find two subarrays with sum as the target value and no overlap
- 【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程
- Skimage learning (3) -- adapt the gray filter to RGB images, separate colors by immunohistochemical staining, and filter the maximum value of the region
猜你喜欢
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
直接上干货,100%好评
QML初学
Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
Skimage learning (2) -- RGB to grayscale, RGB to HSV, histogram matching
Shallow understanding Net core routing
Pycharm IDE下载
如何选择合适的自动化测试工具?
Sator a lancé le jeu web 3 "satorspace" et a lancé huobi
随机推荐
The top of slashdata developer tool is up to you!!!
Rpcms method of obtaining articles under the specified classification
智慧物流平台:让海外仓更聪明
Flask build API service SQL configuration file
PLC:自动纠正数据集噪声,来洗洗数据集吧 | ICLR 2021 Spotlight
【Seaborn】组合图表:PairPlot和JointPlot
QT video transmission
科普达人丨一文弄懂什么是云计算?
最新阿里P7技术体系,妈妈再也不用担心我找工作了
QT视频传输
麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性
Pycharm IDE下载
LeetCode 1626. 无矛盾的最佳球队 每日一题
Sator推出Web3遊戲“Satorspace” ,並上線Huobi
Flask搭建api服务-生成API文档
99% of users often make mistakes in power Bi cloud reports
Arduino 控制的双足机器人
【图像传感器】相关双采样CDS
LeetCode 1696. Jumping game VI daily question
Sator launched Web3 game "satorspace" and launched hoobi