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?