当前位置:网站首页>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;
    }
    

}

 Insert picture description here

 Insert picture description here

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 
    //}
    
}

 Insert picture description here

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);
    }
}

 Insert picture description here

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;
    }
    
}

 Insert picture description here

 Insert picture description here

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';
    
}

 Insert picture description here

原网站

版权声明
本文为[Rookie counter attack]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220526038887.html