Original title: " A minimalist introduction to Solidity: 3. Function type "
Original source: 0xAA
I am re-learning solidity recently, to consolidate the details, and also write a "Solidity Minimalist Introduction" for beginners to use (Programmers can find another tutorial), 1-3 lectures are updated every week.
All codes are open source on github: github.com/AmazingAng/WTFSolidity
In the solidity official document, functions are classified as numerical types, but I think the difference is very big, so they are classified into a separate category. Let's first look at the form of the function in solidity:

It looks a little complicated, let's look at it one by one from the front to the back (the keywords in the square brackets are the keywords that can be written or not):
2. (): The parameters of the function are written in parentheses, that is, the variable type and name to be input to the function.
3. {internal|external|public|private} : function visibility specifier, a total of 4 types. If the function type is not marked, the default is internal.
- public: Visible both internally and externally, and automatically generate a getter function for the stoage variable .
- private: Can only be accessed from within this contract, and cannot be used by inherited contracts.
- external: can only be accessed from outside the contract (but can be called with this.f(), f is the function name)
- internal: can only be accessed from within the contract, Inherited contracts can be used.
4. [pure|view|payable]: A keyword that determines function permissions/functions. payable is easy to understand, with its function, you can transfer ETH to the contract when it is running. See the next section for an introduction to pure and view.
5. [returns ()]: The variable type and name returned by the function.
When I first started learning solidity, I never understood the keywords of pure and view, because there are no similar keywords in other languages. Solidity added these two keywords, I think it is because of gas fee. The state variables of the contract are stored on the chain, and the gas fee is very expensive. If these variables are not rewritten, there is no need to pay gas. There is no need to pay gas for calling pure and view functions.
I drew a Mario illustration to help everyone understand. In the illustration, I compare the state variables in the contract (stored on the chain) to Princess Bitchi, and the three different roles represent different keywords.

WTH is pure and view in solidity?
pure, Chinese means "pure", understood as "pure cattle and horses" in solidity. Functions containing the pure keyword cannot read or write state variables stored on the chain. Just like mobs, you can't see or touch Princess Bichi.
view, "look", is understood as "spectator" in solidity. Functions containing the view keyword can read but also cannot write state variables. Similar to Mario, you can see the bitch, but after all, you are a spectator and cannot enter the bridal chamber.
Do not write pure or view, the function can either read or write state variables. Similar to the boss in Mario, he can do whatever he wants with Princess Bichi.
We define a state variable _number = 5 in the contract.

Define an add() function, each time it is called, output _number + 1.

If add() contains the pure keyword, such as function add() pure external, an error will be reported. Because pure (pure cow and horse) is not worthy of reading the state variables in the contract, let alone rewriting. What can the pure function do? For example, you can pass a parameter _number to the function and have it return _number+1.

If add() contains view, such as function add() view external, an error will also be reported. Because the view can read, but not rewrite the state variables. The equation can be rewritten slightly so that instead of overwriting _number, it returns a new variable.


We define an internal minus () function, each call makes the number variable decrement by 1. Since it is internal, it can only be called inside the contract. Let's define an external minusCall() function and call minus(). This way, one can indirectly call the internal's minus() by calling minusCall().

< br>
We define an external payable minusPayable() function, call minus() indirectly, and return the ETH balance in the contract (this keyword can make We refer to the contract address). When we call minusPayable(), we transfer 1 ETH to the contract.

We can see from the returned information that the balance of the contract is 1 ETH.

In the third lecture, we introduced the function types in solidity, What is more difficult to understand is pure and view, which have not appeared in other languages. Solidity has two keywords, pure and view, mainly to save gas fee and control function permissions. Both of these equations do not consume gas. In the next lecture we will introduce the two types of references and maps, and introduce more complex functions.
Original link blockquote>
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia
