There are 4 visibility types in Solidity
public
- Publicly accessible function (Dapp users can call)private
- Can be called only inside the contractinternal
- Only this contract and it’s childrens can call itexternal
- Only consumers can call. No other contract functions can call it.
function getMyName() public returns(string) {
return myName;
}
We have got an error
:::warning TypeError: Data location must be “memory“ for return parameter in function
:::
Because we are not mutating the contract’s stored myName
, we’ll return the copy of it from memory
, which is where it’s stored by default.
Another warning
:::warning Warning: Function state mutability can be restricted to view
:::
We get this warning because our function is not mutating the state, it’s only reading from it. So we can use view
modifier.
// version of solidity greater than 5
pragma solidity ^0.5.10;
// write a contract using contract keyword
contract HelloWorld {
// data type string
string myName = 'Ryan';
// write a function that returns name
function getMyName() public view returns(string memory) {
return myName;
}
}