3

Just started learning solidity from https://solidity.readthedocs.io.

Here's the code I'm trying to understand:

enter image description here

My questions:

  1. Why does get() function has constant keyword? Is it neccessary to put constant there?
  2. Is it nesessary to put retVal in (uint retVal)? If I call get() function from other contract and want to assign return value to a variable, what should be the name of that variable? Can it be any name or just retVal?

Solidity is confusing, but very cool!

1 Answer 1

3

Q: 1. Why does get() function has constant keyword? Is it neccessary to put constant there?

Constant functions just read data from the blockchain. They don't change any data in the blockchain. As a result, you can query constant functions without having to pay any gas (fee).

From Solidity - Frequently Asked Questions:

What is the difference between a function marked constant and one that is not?

constant functions can perform some action and return a value, but cannot change state (this is not yet enforced by the compiler). In other words, a constant function cannot save or update any variables within the contract or wider blockchain. These functions are called using c.someFunction(...) from geth or any other web3.js environment.

“non-constant” functions (those lacking the constant specifier) must be called with c.someMethod.sendTransaction({from:eth.accounts[x], gas: 1000000}); That is, because they can change state, they have to have a gas payment sent along to get the work done.


Q: 2. Is it nesessary to put retVal in (uint retVal)? If I call get() function from other contract and want to assign return value to a variable, what should be the name of that variable? Can it be any name or just retVal?

retVal is just another name. You can call your return variables any name.

There are some alternative ways you can achieve the same result:

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

or

function get() constant returns (uint retVal) {
    retVal = storedData;
}

You may also want to look at Where can I find some Solidity / Smart Contract source code examples?

4
  • you're awesome! It's all so much clearer now. One quick question. Do calculations inside constant functions also cost nothing? Would I be charged any gas if I multiplied some values and tried to return the result?
    – manidos
    Jul 23, 2016 at 15:23
  • 1
    Totally free to execute statements within constant functions. No charges if you multiplied some values and return the results. The compiler should report errors where there are statements that modify the blockchain data within constant functions. Jul 23, 2016 at 15:31
  • 1
    One thing to note is that for now the constant keyword is not taken into account for functions
    – dragosb
    Jul 23, 2016 at 19:40
  • 1
    @dragosb - found a reference in updated answer above "constant functions can perform some action and return a value, but cannot change state (this is not yet enforced by the compiler)" - is this what you are referring to? Jul 24, 2016 at 1:05

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.