Skip to content

Commit

Permalink
publish review 3-5
Browse files Browse the repository at this point in the history
  • Loading branch information
AmazingAng committed May 22, 2023
1 parent c7b5e26 commit 00fe009
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
10 changes: 8 additions & 2 deletions 03_Function/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ tags:
---

# WTF Solidity极简入门: 3. 函数

我最近在重新学solidity,巩固一下细节,也写一个“WTF Solidity极简入门”,供小白们使用(编程大佬可以另找教程),每周更新1-3讲。

推特:[@0xAA_Science](https://twitter.com/0xAA_Science)
Expand All @@ -18,12 +19,17 @@ tags:
-----

## 函数
Solidity官方文档将函数归类为数值类型,但由于它们之间存在显著差异,因此我们将函数单独分类。让我们先了解一下Solidity中函数的形式:

Solidity语言的函数非常灵活,可以进行各种复杂操作。在本教程中,我们将会概述函数的基础概念,并通过一些示例演示如何使用函数。

我们先看一下 Solidity 中函数的形式:

```solidity
function <function name>(<parameter types>) {internal|external|public|private} [pure|view|payable] [returns (<return types>)]
```
虽然看起来有些复杂,但我们可以从前往后逐个分析(方括号中的内容为可选关键字):

看着有一些复杂,让我们从前往后逐个解释(方括号中的是可写可不
写的关键字):

1. `function`:声明函数时的固定用法。要编写函数,就需要以 `function` 关键字开头。

Expand Down
2 changes: 1 addition & 1 deletion 05_DataStorage/DataStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ contract DataStorage {
// The data location of x is storage.
// This is the only place where the
// data location can be omitted.
uint[] x = [1,2,3];
uint[] public x = [1,2,3];

function fStorage() public{
//声明一个storage的变量xStorage,指向x。修改xStorage也会影响x
Expand Down
7 changes: 4 additions & 3 deletions 05_DataStorage/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ tags:
-----

## Solidity中的引用类型
**引用类型(Reference Type)**:包括数组(`array`,结构体`struct`和映射(`mapping`),这类变量占空间大,赋值时候直接传递地址(类似指针)。由于这类变量比较复杂,占用存储空间大,我们在使用时必须要声明数据存储的位置。
**引用类型(Reference Type)**:包括数组(`array`和结构体`struct`由于这类变量比较复杂,占用存储空间大,我们在使用时必须要声明数据存储的位置。

## 数据位置

solidity数据存储位置有三类:`storage``memory``calldata`。不同存储位置的`gas`成本不同。`storage`类型的数据存在链上,类似计算机的硬盘,消耗`gas`多;`memory``calldata`类型的临时存在内存里,消耗`gas`少。大致用法:

1. `storage`:合约里的状态变量默认都是`storage`,存储在链上。
Expand All @@ -46,7 +47,7 @@ solidity数据存储位置有三类:`storage`,`memory`和`calldata`。不同

1. `storage`(合约的状态变量)赋值给本地`storage`(函数里的)时候,会创建引用,改变新变量会影响原变量。例子:
```solidity
uint[] x = [1,2,3]; // 状态变量:数组 x
uint[] public x = [1,2,3]; // 状态变量:数组 x
function fStorage() public{
//声明一个storage的变量 xStorage,指向x。修改xStorage也会影响x
Expand All @@ -59,7 +60,7 @@ solidity数据存储位置有三类:`storage`,`memory`和`calldata`。不同

2. `storage`赋值给`memory`,会创建独立的副本,修改其中一个不会影响另一个;反之亦然。例子:
```solidity
uint[] x = [1,2,3]; // 状态变量:数组 x
uint[] public x = [1,2,3]; // 状态变量:数组 x
function fMemory() public view{
//声明一个Memory的变量xMemory,复制x。修改xMemory不会影响x
Expand Down

0 comments on commit 00fe009

Please sign in to comment.