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.

marked as duplicate by Achala Dissanayake, Richard Horrocks, eth Aug 28 '18 at 5:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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
    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). ;-) – Rob Hitchens - B9lab Aug 27 '18 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. – goodvibration Aug 28 '18 at 7:08

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