当前位置:网站首页>Solidity from introduction to practice (IV)
Solidity from introduction to practice (IV)
2022-06-22 08:53:00 【Rookie counter attack】
Solidity From introduction to practice ( Four )
Be careful : This column mainly refers to https://www.bilibili.com/video/BV1St411a7Pk?p=11&spm_id_from=pageDriver Study notes and https://blog.csdn.net/weixin_45067603/article/details/105751748
function overloading
Function overloading means that the function names are the same , Different parameter list , That is, one of the following two conditions must be met
1. The types of parameters passed in by the function are different
2. The number of arguments passed in by the function is different
pragma solidity ^0.4.16;
contract funTest{
uint public test= 0;
function fun1(uint num1,uint num2) {
test = 10;
}
function fun1(uint num1) {
test = 20;
}
function fun2(uint a) {
test = 100;
}
function fun2(string a) {
test = 200;
}
function fun3(address a) {
test=1000;
}
function fun3(uint160 a){
test=2000;
}
function fun4(uint8 a){
test=10000;
}
function fun4(uint16 a){
test=20000;
}
function fun1test() public view returns(uint){
fun1(1,2);
return test;
}
function fun2test() public view returns(uint){
fun2('asdasd');
return test;
}
function fun3test() public view returns(uint){
fun3(0x3e805eC48BdFBc458e7446058F94a315896A1cF6);
// Use only address type , Can run and run address Overloaded function of parameter
return test;
}
// function fun3test2() public view returns(uint160){
//uint160 If it turns into uint160 Type and run , So report a mistake
//temp=uint160(0x3e805eC48BdFBc458e7446058F94a315896A1cF6);
//fun3(temp);
//return temp;
// }
//** The following error will appear **
function fun4test() public view returns(uint){
fun4(256);
return test;
}
function reset() public{
test = 0;
}
}


Function passed in parameters
There are two main methods to transfer parameters
1. You can directly pass in the value of the corresponding parameter
2. Format Function name ({ type 1:value1, type 2:value2,…, type n:valuen})
3. All parameters must be passed in when calling the function , Otherwise, an error will be reported
pragma solidity ^0.4.16;
contract funTest{
uint public num;
string public teststring;
function setvalue(uint num1,string teststring1){
num=num1;
teststring = teststring1;
}
function test1(){
setvalue(1,'a');
}
function test2(){
setvalue({
num1:1,teststring1:'a'});
}
function test3(){
setvalue({
teststring1:'a',num1:1});
}
// function wrongtest4(){
// setvalue(1); Error will be reported in compilation
//}
}

Function return value
1. And go Language is similar to ,solidity You can have multiple return values , You can name ( type 1 value1, type 2 value2,…, type n valuen)
2. Other properties are similar to those of other languages
pragma solidity ^0.4.16;
contract returnTest{
function test1() public view returns(uint8 num1,uint8 num2,string teststring){
// Direct assignment , Finally, you can see that the return value is the assigned value
num1=1;
num2=2;
teststring='hello';
}
function test2() public view returns(uint8 num1,uint8 num2,string teststring){
return (10,20,'hello2');// You can return directly
}
function test3() public view returns(uint8 num1,uint8 num2,string teststring){
num1=1;
num2=2;
teststring='hello';
return (10,20,'hello2');// If you assign a value and return , So in order to return The value of
}
function test4() public view returns(uint8 num1,uint8 num2,string teststring1,string teststring2){
teststring1='testnb1';
teststring2='testnb2';
//return (10+20,10*20,'hello2',teststring1+teststring2);//solidity I won't support it string Direct splicing of
return (10+20,10*20,teststring1,teststring2);
}
}

Scope of variable
Be careful : Do not redefine a variable inside a function that is the same as a function parameter
pragma solidity ^0.4.16;
contract areaTest{
uint public a=1;
function areatest1(uint a) public view returns(uint){
a=10;
return a;
}
function areatest2(uint a) public view returns(uint){
a=10;
// Redefinition a, The following error will appear
// for(uint a=0;a<10;a++){
// }
return a;
}
function areatest3() public returns(uint){
// This code changes the global variables a Value
a++;
return a;
}
function areatest4(uint a) public returns(uint){
// This variable is only a local variable , The function is destroyed after execution
a++;
return a;
}
function areatest5() public returns(uint){
// At this point, redefine a It is feasible. , Override global variables a Value
uint a;
a=100;
return a;
}
}


constant keyword
1. intra-function constant keyword , stay 4.x In the version, it is the same as view It is equivalent. , stay 5.0 You will not be able to use
2. Global variables 、constant attribute 、 Local variables do not have this attribute .
3. Global variable plus constant attribute , Can't be modified .
4. Current version support int,uint,string,bytes1–bytes32 Able to use constant.
pragma solidity ^0.4.16;
contract constantTest{
uint public constant a=1;
//function changetest() public{
// a=2; An attempt was made to modify , The result is a compilation error
//}
// function changetest() public{
// uint constant test=2; // Cannot declare inside a function constant Variable
// }
int public constant b=2;
bytes32 public constant c=0x232334224;
string public constant d='ttttest';
}

边栏推荐
- luogu P4557 [JSOI2018]战争
- YOLOv5报错:AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘ 的解决方案
- Give priority to static member classes
- 新型冠状病毒疫情
- 一文搞懂one-hot和embedding
- Several methods to prevent repeated form submission + actual measurement
- 08 bridging mode
- Interview shock 59: can there be multiple auto increment columns in a table?
- 09 组合模式
- Hashtable source code analysis, collections Synchronizedmap parsing
猜你喜欢
随机推荐
Application of complex science in Maker Teaching Research
12 享元模式
How to troubleshoot OOM
A simple - timed task component (quartz) -demo
电机学感应电动机重点知识总结(现有题目中反映的)
Matrix decomposition
一文搞懂one-hot和embedding
Spark Yarn内存资源计算分析(参考)--Executor Cores、Nums、Memory优化配置
How to write high performance SQL statements?
Instanceinforeplicator class of Eureka (service registration assistant)
Flask blog practice - realize article management
Flask blog practice - user management
Basic operation knowledge of DML and DQL
Bit group sort
19 备忘录模式
20 status mode
Fastcorrect: speech recognition fast error correction model RTC dev Meetup
Basic knowledge and practical application of redis
Develop steam education based on scientific skills
Spark yard memory resource calculation and analysis (Reference) -- optimized configuration of executor cores, nums and memory









