1

I had been reading about the state of a smart contract. Here

It also gives a small example of a contract.

contract SimpleStorage {
    uint storedData;

    function set(uint x) {
        storedData = x;
    }

    function get() constant returns (uint) {
        return storedData;
    }
}

Here we have a variable storedData and if we want to set it, a transaction with x is send and the state of the contract changes. As far as I understood it correct it is not necessary to send a transaction to read storedData. I can perform a local call.

Now what about the following contract:

contract SimpleStorage {

    function set(uint x) {
        uint storedData = x;
        return storedData;
    }
}

storedData is now within the function and saves the content but will this change the state of the contract? Do I have to send a transaction to call this function or is a local call enough?

From my programming knowledge I would say not but I am not sure when it comes to smart contracts :)

Thank you

2

The difference between the first example and the second is that in the first, storedData is located in the contract's storage, while in the second the uint is a memory variable. Storage persists across transactions, memory does not. Thus, you are correct.

  • Could I call the second example locally or does it also have to be a transaction to invoke the function? – Donut Jun 13 '17 at 16:19
  • @Donut It needs to be transacted for the network to verify the change is allowed. – 0xcaff Jun 13 '17 at 22:52
  • That's not true, if you send it as a transaction it will not return anything useful. Calling it locally will just return what you input. Nothing in the second example is stateful anyway – Tjaden Hess Jun 14 '17 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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