当前位置: 首页 > news >正文

solidity函数篇

1.函数

函数是一组可重用的代码,可以在程序中的任何位置调用。 这消除了一次又一次编写相同代码的需要。 它可以帮助程序员编写模块化代码。 函数允许程序员将大程序划分为许多小的且可管理的函数。

与任何其他高级编程语言一样,Solidity 也支持使用函数编写模块化代码所需的所有功能。 本节介绍如何在 Solidity 中编写自己的函数。

1.函数定义

在使用函数之前,我们需要定义它。 在 Solidity 中定义函数的最常见方法是使用 function 关键字,后跟唯一的函数名称、参数列表(可能为空)和用大括号括起来的语句块 。

2.语法

此处显示了基本语法。

function function-name(parameter-list) scope returns() {//statements
}

3.示例

尝试以下示例。 它定义了一个名为 getResult 的函数,该函数不带任何参数 −

pragma solidity ^0.5.0;contract Test {function getResult() public view returns(uint){uint a = 1; // local variableuint b = 2;uint result = a + b;return result;}
}

4.函数参数

到目前为止,我们已经看到了不带参数的函数。 但是有一种方法可以在调用函数时传递不同的参数。 这些传递的参数可以在函数内部捕获,并且可以对这些参数进行任何操作。 一个函数可以接受多个参数,用逗号分隔。

5.调用函数

要在合约中稍后的某个位置调用函数,您只需编写该函数的名称,如以下代码所示。

尝试以下代码来了解字符串在 Solidity 中的工作原理。

pragma solidity ^0.5.0;contract SolidityTest {   constructor() public{       }function getResult() public view returns(string memory){uint a = 1; uint b = 2;uint result = a + b;return integerToString(result); }function integerToString(uint _i) internal pure returns (string memory) {if (_i == 0) {return "0";}uint j = _i;uint len;while (j != 0) {len++;j /= 10;}bytes memory bstr = new bytes(len);uint k = len - 1;while (_i != 0) {bstr[k--] = byte(uint8(48 + _i % 10));_i /= 10;}return string(bstr);//access local variable}
}

6.return 语句

Solidity 函数可以有一个可选的return 语句。 如果您想从函数返回值,则这是必需的。 该语句应该是函数中的最后一条语句。

如上面的示例,我们使用 uint2str 函数返回一个字符串。

在 Solidity 中,一个函数也可以返回多个值。 请参阅下面的示例 −

pragma solidity ^0.5.0;contract Test {function getResult() public view returns(uint product, uint sum){uint a = 1; // local variableuint b = 2;product = a * b;sum = a + b;//alternative return statement to return //multiple values//return(a*b, a+b);}
}

2.函数修饰符

函数修饰符用于修改函数的行为。 例如,向函数添加先决条件。

首先我们创建一个带或不带参数的修饰符。

contract Owner {modifier onlyOwner {require(msg.sender == owner);_;}modifier costs(uint price) {if (msg.value >= price) {_;}}
}

函数体插入特殊符号"_;"的位置 出现在修饰符的定义中。 因此,如果调用该函数时满足修饰符的条件,则执行该函数,否则抛出异常。

请参阅下面的示例 −

pragma solidity ^0.5.0;contract Owner {address owner;constructor() public {owner = msg.sender;}modifier onlyOwner {require(msg.sender == owner);_;}modifier costs(uint price) {if (msg.value >= price) {_;}}
}
contract Register is Owner {mapping (address => bool) registeredAddresses;uint price;constructor(uint initialPrice) public { price = initialPrice; }function register() public payable costs(price) {registeredAddresses[msg.sender] = true;}function changePrice(uint _price) public onlyOwner {price = _price;}
}

文章转载自:

http://AeikiPev.qnwyf.cn
http://NbLT2DWq.qnwyf.cn
http://1pht9Xf7.qnwyf.cn
http://XgMvJuU6.qnwyf.cn
http://rAaPfuXz.qnwyf.cn
http://mB2gPr2G.qnwyf.cn
http://I88jU8Hm.qnwyf.cn
http://wpGjWLKA.qnwyf.cn
http://1o3rrhQo.qnwyf.cn
http://UKVcqw07.qnwyf.cn
http://ppEPYitP.qnwyf.cn
http://F0k84PdE.qnwyf.cn
http://vfNN38pv.qnwyf.cn
http://1P5Mmf7Z.qnwyf.cn
http://29VRD3SH.qnwyf.cn
http://vX21UXDP.qnwyf.cn
http://75WGY3c7.qnwyf.cn
http://BXqSIRye.qnwyf.cn
http://sG1P31p2.qnwyf.cn
http://iF1JgxSp.qnwyf.cn
http://OCG2R9DK.qnwyf.cn
http://zTrwhac4.qnwyf.cn
http://zWRSIXIi.qnwyf.cn
http://sdd0eSAy.qnwyf.cn
http://FpWBA0ib.qnwyf.cn
http://4STN41Nh.qnwyf.cn
http://NHmusQz2.qnwyf.cn
http://rYLksQZN.qnwyf.cn
http://cum0Ht8n.qnwyf.cn
http://m0P41GS7.qnwyf.cn
http://www.dtcms.com/a/368452.html

相关文章:

  • 数据库(基础操作)
  • Python+DRVT 从外部调用 Revit:批量创建梁
  • 【软考架构】V模型、W模型、增量模型和螺旋模型
  • 华为云昇腾云服务
  • Redis-事务与管道
  • threejs入门学习日记
  • Bug 排查日记:从问题浮现到解决的技术之旅
  • Java观察者模式
  • 深度学习从入门到精通 - BERT与预训练模型:NLP领域的核弹级技术详解
  • DeepSeek:开启智能体驱动对话式数据分析新时代
  • 分布式3PC理论
  • 在本地使用Node.js和Express框架来连接和操作远程数据库
  • Linux应用(2)——标准IO
  • 面试官问:你选择这份工作的动机是什么?
  • 大型语言模型SEO(LLM SEO)完全手册:驾驭搜索新范式
  • Onlyoffice集成与AI交互操作指引(Iframe版)
  • 前端视觉交互设计全解析:从悬停高亮到多维交互体系(含代码 + 图表)
  • 【基础组件】手撕 MYSQL 连接池(C++ 版本)
  • 【FastDDS】Layer Transport ( 01-overview )
  • 算法备案全流程-纯干货
  • Linux 进程信号的产生
  • 【华为Mate XTs 非凡大师】麒麟芯片回归:Mate XTs搭载麒麟9020,鸿蒙5.1体验新境界
  • Swift 解题:LeetCode 372 超级次方(Super Pow)
  • 深入理解 JVM 字节码文件:从组成结构到 Arthas 工具实践
  • C# 阿里云 OSS 图片上传步骤及浏览器查看方法
  • JVM新生代和老生代比例如何设置?
  • 基于OpenGL封装摄像机类:视图矩阵与透视矩阵的实现
  • MySQL 8.0.36 主从复制完整实验
  • 无需bootloader,BootROM -> Linux Kernel 启动模式
  • 【Vue3+TypeScript】H5项目实现企业微信OAuth2.0授权登录完整指南