#函数类型(function type)
function (<parameter types>) {internal|external(public)} [pure|constant|view|payable] [returns (<return types>)] 有内部类型(internal)与外部(external)两种类型,如不提示关键字,默认是 internal。内部函数仅当前合约可以调用,外部函数由地址和函数签名组成,它们能传递和调用后返回结果。 合约中访问函数有2种方法: 直接用函数名,如f;或者 this.f
此外,外部函数有特别的成员属性seletor,返回 ABI function selector
pragma solidity ^0.4.16;

contract Selector {
function f() public view returns (bytes4) {
return this.f.selector;
}
}

内部函数示例

pragma solidity ^0.4.16;

library ArrayUtils {
// internal functions can be used in internal library functions because
// they will be part of the same code context
function map(uint[] memory self, function (uint) pure returns (uint) f) internal pure returns (uint[] memory r) {
r = new uint[](self.length);
for (uint i = 0; i < self.length; i++) {
r[i] = f(self[i]);
}
} function reduce(uint[] memory self, function (uint, uint) pure returns (uint) f) internal pure returns (uint r) {
r = self[0];
for (uint i = 1; i < self.length; i++) {
r = f(r, self[i]);
}
} function range(uint length) internal pure returns (uint[] memory r) {
r = new uint[](length);
for (uint i = 0; i < r.length; i++) {
r[i] = i;
}
}
} contract Pyramid {
using ArrayUtils for *;
function pyramid(uint l) public pure returns (uint) {
return ArrayUtils.range(l).map(square).reduce(sum);
} function square(uint x) internal pure returns (uint) {
return x * x;
} function sum(uint x, uint y) internal pure returns (uint) {
return x + y;
}
}

外部函数示例

pragma solidity ^0.4.11;

contract Oracle {
struct Request {
bytes data;
function(bytes memory) external callback;
} Request[] requests; event NewRequest(uint); function query(bytes data, function(bytes memory) external callback) public {
requests.push(Request(data, callback));
NewRequest(requests.length - 1);
} function reply(uint requestID, bytes response) public {
// Here goes the check that the reply comes from a trusted source
requests[requestID].callback(response);
}
} contract OracleUser {
Oracle constant oracle = Oracle(0x1234567); // function buySomething() {
oracle.query("USD", this.oracleResponse);
} function oracleResponse(bytes response) public {
require(msg.sender == address(oracle));
// Use the data
}
}

solidity语言3的更多相关文章

  1. 用solidity语言开发代币智能合约

    智能合约开发是以太坊编程的核心之一,而代币是区块链应用的关键环节,下面我们来用solidity语言开发一个代币合约的实例,希望对大家有帮助. 以太坊的应用被称为去中心化应用(DApp),DApp的开发 ...

  2. 第一行代码:以太坊(2)-使用Solidity语言开发和测试智能合约

    智能合约是以太坊的核心之一,用户可以利用智能合约实现更灵活的代币以及其他DApp.不过在深入讲解如何开发智能合约之前,需要先介绍一下以太坊中用于开发智能合约的Solidity语言,以及相关的开发和测试 ...

  3. Solidity语言系列教程

    Solidity 是一门面向合约的.为实现智能合约而创建的高级编程语言.这门语言受到了 C++,Python 和 Javascript 语言的影响,设计的目的是能在 以太坊虚拟机(EVM) 上运行. ...

  4. solidity语言介绍以及开发环境准备

    solidity语言介绍以及开发环境准备   Solidity 是一门面向合约的.为实现智能合约而创建的高级编程语言.这门语言受到了 C++,Python 和 Javascript 语言的影响,设计的 ...

  5. 用C++生成solidity语言描述的buchi自动机的初级经验

    我的项目rvtool(https://github.com/Zeraka/rvtool)中增加了生成solidity语言格式的监控器的模块. solidity特殊之处在于,它是运行在以太坊虚拟机环境中 ...

  6. Solidity语言基础 和 Etherum ERC20合约基础

    1. 类型只能从第一次赋值中推断出来,因此以下代码中的循环是无限的,  小. for (var i = 0; i < 2000; i++) { ... } --- Solidity Types ...

  7. solidity语言

    IDE:Atom 插件:autocomplete-solidity 代码自动补齐   linter-solium,linter-solidity代码检查错误   language-ethereum支持 ...

  8. solidity语言14

    库(Libraries) 库类似合约,实现仅在专门地址部署一次,使用EVM的DELEGATECALL的功能重复使用的目的.意思是当库函数被调用后,代码执行在被调用的合约的环境.例如,使用this调用合 ...

  9. solidity语言13

    函数过载 合约内允许定义同名函数,但是输入参数不一致 pragma solidity ^0.4.17; contract A { function f(uint _in) public pure re ...

  10. solidity语言12

    View Functions 函数声明为视图,将无权修改状态 pragma solidity ^0.4.16; contract C { function f(uint a, uint b) publ ...

随机推荐

  1. python学习,day2:python字符串和二进制之间的互换

    在python3中,byte二进制和striing字符串之间不能直接操作,需要进行编码和解码才行.下面是个例子 msg = '我爱北京天安门' print(msg) print(msg.encode( ...

  2. JavaScript 中this 初步理解笔记

    Javascript中函数中的this通常指向的是函数的拥有者,这个拥有者就是上下文执行对象:另外一点需要注意,this只能在javascript函数内部使用.

  3. POJ_2407 Relatives 【欧拉函数裸题】

    一.题目 Given n, a positive integer, how many positive integers less than n are relatively prime to n? ...

  4. Codeforces 1136D Nastya Is Buying Lunch (贪心)

    题意: 给一个序列和一组交换序列(a,b),当且仅当a在b的前面(不允许有间隔),这两个数才能交换,问最后一个数最多能移动多少个位置. 分析: 这题是思路是十分的巧妙呀 , 用一个数组num[x]  ...

  5. Java static{}语句块详解

    [转自] http://blog.csdn.net/lubiaopan/article/details/4802430 static{}(即static块),会在类被加载的时候执行且仅会被执行一次,一 ...

  6. Notepad++编译和运行Java

    首先要让Notepad++编译和运行Java,前提是电脑里已经配置好了Java的环境(这里可以参考我博客里关于Java环境配置的那篇随笔). 在Notepad++上面的选项栏中找到 插件---> ...

  7. 理解Call、Apply、bind

    Apply.call 共同点: 为了改变函数执行时的上下文(简单说就是为了改变当前函数体内的This的指向) 不同点: 传入的参数不一样,func.apply(this,[arg1,arg2]).fu ...

  8. python 计数器Counter

    from collections import Counter colours=( ('Yasoob','Yellow',1), ('Ali','Blue',2), ('Arham','Green', ...

  9. oracle 操作实例(一)----redolog 损坏恢复

    一,实验前的准备 数据库全备保证自己没成功还能补救一下 vim full.sh export ORACLE_BASE=/u01/app/oracle export ORACLE_HOME=$ORACL ...

  10. WEB 倒计时

    <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...