Logo
Section 1 / 7

函数概述

学习如何在 Solidity 中声明和调用函数。

导入

函数概述

学习如何在 Solidity 中声明和调用函数。

如何在 Solidity 中声明函数

1// SPDX-License-Identifier: GPL-3.0 2 3pragma solidity >=0.7.0 <0.9.0; 4 5contract TestContract { 6 function testFunction(int num1, int num2) public pure returns (int num3) { 7 num3 = num1 + num2; 8 } 9}

在 Solidity 中声明函数的另一种方式

1// SPDX-License-Identifier: GPL-3.0 2 3pragma solidity >=0.7.0 <0.9.0; 4 5contract TestContract { 6 function testFunction(int num1, int num2) external pure returns (int) { 7 int num3 = num1 + num2; 8 return num3; 9 } 10}

演示 view 函数的示例代码

1// SPDX-License-Identifier: GPL-3.0 2 3pragma solidity >=0.7.0 <0.9.0; 4 5contract TestContract { 6 int public testNumber = 20; 7 8 function testFunction(int num1, int num2) external view returns (int) { 9 int num3 = num1 + num2 + testNumber; 10 return num3; 11 } 12}

总结

函数修饰符