Home > Software engineering >  Differences between these 2 functions in solidity?
Differences between these 2 functions in solidity?

Time:01-02

I don't get the difference between these 2 functions in solidity, will the same result be returned?

address oldVoter;

  modifier checkSender(address actualVoter) {
        require(oldVoter != actualVoter);  
        _; 
    }


    function changeVote(address caller)public  {
        oldVoter =  caller;
    }


    function changeVote() public checkSender(msg.sender){
        olderVoter = caller;
    }

CodePudding user response:

this function is modified with checkSender modifier:

 function changeVote() public checkSender(msg.sender){
        olderVoter = caller;
    }

since it is modified you can see this function like this:

function changeVote() public checkSender(msg.sender){
        require(oldVoter != actualVoter); 
        // "_" in modifier is placeholder. saying that place the modified function here
        olderVoter = caller;
    }

There is another function with the same name changeVote, but if you notice it does not have any modifier and does not take any argument. It is absolutely legal to have the same function name with a different number of parameters of different data types for incoming parameters.

You can have multiple definitions for the same function name in the same scope in Solidity. This is called function overloading. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. Return types are not taken into consideration for determining valid function signatures.

CodePudding user response:

You are not returning anything from your functions. changeVote(address caller) (first one) sets oldVoter to the caller input. The second one should not compile as caller is not defined it in.

  • Related