0
pragma solidity ^0.6.0;



contract P{
    uint no1;
    uint no2;
    uint result;

function MultEvensAddOdds(uint _no1, uint _no2) public pure returns(uint) {
    result public myresult = result;
    if(_no1 % 2 == 0 && _no2 % 2 == 0){
        uint myresult = _no1 + _no2;
    }else if(_no1 % 2 == 1 && _no2 % 2 == 1){
        uint myresult = _no1 * _no2;
    }else{
        uint myresult = _no1/_no2;
    }
}

}

I want to Multiple two even numbers, add two odd numbers, or divide both numbers if none of the previous conditions are met. However I am getting this error: ParserError: Expected ';' but got 'public' result public myresult = result; ^----^

1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    May 3, 2022 at 17:41

1 Answer 1

0

Your problem is here:

result public myresult = result;

You must to declare a new variable in this way:

[datatype] [variableName] = [value];

In your case you change this line in this way:

uint myresult = result;

And then use myresult variable to set operation results. You can see this smart contract code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract P{
    uint no1;
    uint no2;
    uint result;

    function MultEvensAddOdds(uint _no1, uint _no2) public view returns(uint) {
        uint myresult = result;
        if(_no1 % 2 == 0 && _no2 % 2 == 0){
            myresult = _no1 + _no2;
        }else if(_no1 % 2 == 1 && _no2 % 2 == 1){
            myresult = _no1 * _no2;
        }else{
            myresult = _no1 / _no2;
        }
        return myresult;
    }
}
5
  • Thanks. I guess this way "result public myresult = result;" is only applicable for structs if I am not wrong. Anyways, I am using remix IDE, I can not see the output when I input my values at the deployed contract May 3, 2022 at 15:02
  • Yes, you can use that statement that you declared in your original smart contract code for struct (but it's different for this operation the initialization). However I updated my answer, for view the result of MultEvensAddOdds function only adding the 'view' keyword into signature method and specify 'return [variable]'. May 3, 2022 at 15:12
  • I see, however, the output for whatever number I input is "0: uint256: 0". There are also tons of warnings "This declaration shadows an existing declaration" and "Unused local variable." May 3, 2022 at 15:19
  • I cannot see these warnings. Did you try to copy and paste the code in my answer into Remix IDE? Moreover, I tried your MultEvensAddOdds function in my smart contract code and works. May 3, 2022 at 15:23
  • Oh I figured out a few things, the code we both typed has a silly mistake, basically, the conditions should be the way around (i.e. * not + for the 1st condition) and my code is not working properly because I typed uint before myresult at each conditional statement (I wonder why tho). May 3, 2022 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.