Hi @Kkrypto,
There are 4 function types (3 of which we define with specific keywords in the function header) as follows:
-
pure
(most restrictive)
Cannot change or read the contract state. -
view
Can read the contract state, but cannot change it. -
undefined (no type declared in the function header)
Can read and change the contract state (but cannot receive ether) -
payable
(least restrictive)
Can read and change the contract state, and can also receive ether.
(Note: a function doesn’t need to bepayable
to send ether — only to receive it)
It is good risk management to always restrict what a function is allowed to do, to only what it needs to do…
// LINE 7
function getNumber() public view returns(int) {
return number;
}
This is a getter. It only needs to read data stored in the contract state (the integer stored in the state variable number
) and return it to the caller. It doesn’t need to modify the contract state. So, we should define it as a view
function by adding view
to the function header.
// LINE 10
function setNumber(int _number) public {
number = _number;
}
In contrast, this is a setter. It needs to modify data stored in the contract state (assign the integer input into the function to the state variable number
). To ensure that it can modify the contract state, but cannot receive any ether, we leave its function type undefined i.e. we do not need to add anything extra to the function header.
Let me know if anything is still unclear, or if you have any further questions