2

Here I take input from the user in every function to perform the calculation but what if I wanted to take the two input numbers from the user only once and then perform all the rest of functions with those 2 numbers only. Like take the input numbers and then only click the specific button for the function that comes after deploying the contract in remix. For addition, subtraction etc.

Code:

//SPDX-License-Identifier:MIT;
pragma solidity >=0.8.0 < 0.9.0;

contract SimpleCalculator{
function addTwoNumbers(uint256 number1,uint256 number2) public view returns(uint256){
    uint256 addition = number1 + number2;
    return addition;
}
function subtractTwoNumbers(int number1,int256 number2) public view returns(int256){
    int256 subtraction = number1 - number2;
    return subtraction;
}
function divideTwoNumbers(uint256 number1,uint number2) public view returns(uint256){
    uint256 division = number1/number2;
    return division;
}
function multiplyTwoNumbers(uint256 number1, uint256 number2) public view returns(uint256){
    uint256 multiplication = number1*number2;
    return multiplication;
}
}

2 Answers 2

0

I'm not really sure what you need, but I wrote this quick example of how to have a single function that orchestrates all the operations and returns one result with all the operations.

Take a look at the doAllOperations function. Basically I created an Operations struct to save all the results of all these operations and return this struct:

//SPDX-License-Identifier:MIT;
pragma solidity >=0.8.0 < 0.9.0;

struct Operations {
    uint256 sum;
    uint256 substraction;
    uint256 division;
    uint256 multiplication;
}

contract SimpleCalculator {

    function doAllOperations(uint256 number1,uint256 number2) public pure returns(Operations memory) {

        Operations memory operationsResult = Operations({
            sum: addTwoNumbers(number1, number2),
            substraction: subtractTwoNumbers(number1, number2),
            division: divideTwoNumbers(number1, number2),
            multiplication: multiplyTwoNumbers(number1, number2)
        });

        return operationsResult;

    }

    function addTwoNumbers(uint256 number1, uint256 number2) public pure returns(uint256) {
        uint256 addition = number1 + number2;
        return addition;
    }

    function subtractTwoNumbers(uint256 number1, uint256 number2) public pure returns(uint256) {
        uint256 subtraction = number1 - number2;
        return subtraction;
    }

    function divideTwoNumbers(uint256 number1, uint number2) public pure returns(uint256) {
        uint256 division = number1/number2;
        return division;
    }

    function multiplyTwoNumbers(uint256 number1, uint256 number2) public pure returns(uint256) {
        uint256 multiplication = number1*number2;
        return multiplication;
    }

}
0

Here I got you covered:

//SPDX-License-Identifier:MIT;
pragma solidity >=0.8.0 < 0.9.0;

contract SimpleCalculator{

    uint number1;
    uint number2;

    function setImputNumbers(uint firstNumber, uint secondNumber) public {
        number1 = firstNumber;
        number2 = secondNumber;
    }

    function addTwoNumbers() public view returns(uint256){
        uint256 addition = number1 + number2;
        return addition;
    }

    function subtractTwoNumbers() public view returns(int256){
        int256 subtraction = int(number1) - int(number2);
        return subtraction;
    }

    function divideTwoNumbers() public view returns(uint256){
        uint256 division = number1/number2;
        return division;
    }

    function multiplyTwoNumbers() public view returns(uint256){
        uint256 multiplication = number1*number2 ;
        return multiplication;
    }

}

It's a simple code that does exactly what you requested. So now you can play/experiment with it. I haven't optimized or covered possible mathematical edge cases that can go wrong (division by zero). But its perfectly suitable otherwise for learning solidity.

Your Answer

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

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