1

Below is my smart contract code for calculator.

pragma solidity 0.4.24;

contract Calculator{

    uint result=10;

    function Calculator() public
    {

    }

    function getResult() public view returns (uint)
    {
        return result;
    }

    function addition(uint num) public
    {
        result=result+num;

    }

    function sub(uint num) public
    {
        result = result - num;
    }

    function mult(uint num) public
    {
        result =result*num;
    }

    function div(uint num) public
    {
        result = result/num;
    }

    }

Here what is the significance of view keyword in below function?

function getResult() public view returns (uint)

My observation is:

  1. If I do not specify view keyword, remix browser is not showing me results.
  2. If I specify the view keyword, I can see the result on UI.

But still I want to understand the significance of view in solidity.

1

1 Answer 1

1

A non-view function executes a transaction on the blockchain.

You can use the return-value directly only in the on-chain (i.e., by calling this function from another function in the same contract or from another function in another contract).

When you call it in the off-chain (i.e., using some web3 client), then you do not get the return-value directly, but a transaction receipt, which you can then use in order to check the status of the transaction executed.

2
  • 2
    I think you meant to say the view functions (off-chain) return values but don't update the state and the non-view functions (on-chain) update the state but don't return value (to EOA). ;-) Aug 27, 2018 at 19:27
  • @RobHitchensB9lab: I said only the second half of your statement, because I figured that he/she were having difficulties understanding why they were unable to obtain the return-value. Aug 28, 2018 at 7:08

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