Skip to content

Commit

Permalink
update readme: 4. Function Output
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenBingWei1201 committed Sep 25, 2024
1 parent b1dea51 commit 958af56
Showing 1 changed file with 66 additions and 9 deletions.
75 changes: 66 additions & 9 deletions ChenBingWei1201.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ timezone: Asia/Shanghai
<!-- Content_START -->

### 2024.09.23
<details>
<summary>1. HelloWeb3</summary>

#### WTF is Solidity?
- Solidity is a programming language used for creating smart contracts on the Ethereum Virtual Machine (EVM).
Expand Down Expand Up @@ -55,10 +57,12 @@ versions: 0.4.22 -> constructor, 0.8.0 -> safeMath

#### Summary
In the first day, I learned what is `Solidity`, `Remix IDE`, and completed our first Solidity program - `HelloWeb3`.
</details>

### 2024.09.24
<details>
<summary>2. Variable Types</summary>

#### Variable Types
Solidity is statically-type language, which means **the type of each variable needs to be specified in code at compile time**.

1. **Value Type**: This include boolean, integer, etc. These variables directly pass values when assigned.
Expand Down Expand Up @@ -94,10 +98,12 @@ Solidity is statically-type language, which means **the type of each variable ne
| Type | Example |
| ------------- | ------------- |
| Mapping | `mapping(address=>uint256)`, `mapping(address addr=>uint)`, `mapping(address addr=>uint balance)` |
</details>

### 2024.09.25

#### Function
<details>
<summary>3. Function</summary>

Here's the format of a function in Solidity:
```solidity
Expand Down Expand Up @@ -145,20 +151,20 @@ We define a state variable `number = 5`
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract FunctionTypes{
uint256 public number = 5;
uint256 public number = 5;
```
Define an `add()` function, add 1 to `number` on every call.
```solidity
// default
function add() external{
number = number + 1;
number = number + 1;
}
```
If `add()` contains `pure` keyword, i.e. `function add() pure external`, it will result in an error. Because `pure` cannot read state variable in contract nor write. So what can `pure` do ? That is, you can **pass a parameter `_number` to function, let function `returns _number + 1`**.
```solidity
// pure
function addPure(uint256 _number) external pure returns(uint256 new_number){
new_number = _number+1;
new_number = _number+1;
}
```
If `add()` contains `view`, i.e. `function add() view external`, it will also result in error. Because `view` can read, but cannot write state variable. We can modify the function as follows:
Expand All @@ -173,12 +179,12 @@ If `add()` contains `view`, i.e. `function add() view external`, it will also re
```solidity
// internal
function minus() internal {
number = number - 1;
number = number - 1;
}
// external
function minusCall() external {
minus();
minus();
}
```
Here we defined an `internal minus()` function, `number` will decrease 1 each time function is called. Since `internal` function can only be called within the contract itself. Therefore, we need to define an `external minusCall()` function to call `minus()` internally.
Expand All @@ -187,11 +193,62 @@ Here we defined an `internal minus()` function, `number` will decrease 1 each ti
```solidity
// payable: money (ETH) can be sent to the contract via this function
function minusPayable() external payable returns(uint256 balance) {
minus();
balance = address(this).balance;
minus();
balance = address(this).balance;
}
```
We defined an `external payable minusPayable()` function, which calls `minus()` and return `ETH` balance of the current contract (`this` keyword can let us query current contract address). Since the function is `payable`, we can send 1 `ETH` to the contract when calling `minusPayable()`.
</details>

### 2024.09.26
<details>
<summary>4. Function Output</summary>

#### Return values (`return` and `returns`)
There are two keywords related to function output: `return` and `returns`:
- `returns` is added after the function name to declare variable type and variable name;
- `return` is used in the function body and returns desired variables.
```solidity
// returning multiple variables
function returnMultiple() public pure returns(uint256, bool, uint256[3] memory){
return(1, true, [uint256(1),2,5]);
}
```

#### Named returns
We can indicate the name of the return variables in `returns` so that solidity automatically initializes these variables, and automatically returns the values of these functions without adding the `return` keyword.
```solidity
// named returns
function returnNamed() public pure returns(uint256 _number, bool _bool, uint256[3] memory _array){
_number = 2;
_bool = false;
_array = [uint256(3),2,1];
}
```
We only need to assign values to the variable `_number`, `_bool` and `_array` in the function body, and they will automatically return because the return variable type and variable name with `returns` `(uint256 _number, bool _bool, uint256[3] memory _array)` have been declared.

Of course, you can also return variables with return keyword in named returns:
```solidity
// Named return, still support return
function returnNamed2() public pure returns(uint256 _number, bool _bool, uint256[3] memory _array){
return(1, true, [uint256(1),2,5]);
}
```

#### Destructuring assignments
Solidity internally allows tuple types, i.e. a list of objects of potentially different types whose number is a constant at compile-time. The tuples can be used to return multiple values at the same time.
- Variables declared with type and assigned from the returned tuple, not all elements have to be specified (but the number must match):
```solidity
uint256 _number;
bool _bool;
uint256[3] memory _array;
(_number, _bool, _array) = returnNamed();
```
- Assign part of return values: Components can be left out. In the following code, we only assign the return value `_bool2`, but not `_ number` and `_array`:
```solidity
(, _bool2, ) = returnNamed();
```
</details>

###

Expand Down

0 comments on commit 958af56

Please sign in to comment.