Documentation.

This commit is contained in:
chriseth 2020-06-16 18:20:37 +02:00
parent e9f91edc4b
commit cf5aa450bd
6 changed files with 41 additions and 5 deletions

View File

@ -1,6 +1,7 @@
### 0.7.1 (unreleased) ### 0.7.1 (unreleased)
Language Features: Language Features:
* Allow function definitions outside of contracts, behaving much like internal library functions.
Compiler Features: Compiler Features:

View File

@ -8,7 +8,7 @@
grammar Solidity; grammar Solidity;
sourceUnit sourceUnit
: (pragmaDirective | importDirective | structDefinition | enumDefinition | contractDefinition)* EOF ; : (pragmaDirective | importDirective | structDefinition | enumDefinition | functionDefinition | contractDefinition)* EOF ;
pragmaDirective pragmaDirective
: 'pragma' pragmaName ( ~';' )* ';' ; : 'pragma' pragmaName ( ~';' )* ';' ;

View File

@ -18,7 +18,7 @@ if they are marked ``virtual``. For details, please see
:: ::
// SPDX-License-Identifier: GPL-3.0 // SPDX-License-Identifier: GPL-3.0
pragma solidity >0.6.99 <0.8.0; pragma solidity >0.7.0 <0.8.0;
contract owned { contract owned {
constructor() { owner = msg.sender; } constructor() { owner = msg.sender; }

View File

@ -6,6 +6,34 @@
Functions Functions
********* *********
Functions can be defined inside and outside of contracts.
Functions outside of a contract, also called "free functions", always have implicit ``internal``
:ref:`visibility<visibility-and-getters>`. Their code is included in all contracts
that call them, similar to internal library functions.
::
// SPDX-License-Identifier: GPL-3.0
pragma solidity >0.7.0 <0.8.0;
function sum(uint[] memory _arr) pure returns (uint s) {
for (uint i = 0; i < _arr.length; i++)
s += _arr[i];
}
contract ArrayExample {
bool found;
function f(uint[] memory _arr) public {
// This calls the free function internally.
// The compiler will add its code to the contract.
uint s = sum(_arr);
require(s >= 10);
found = true;
}
}
.. _function-parameters-return-variables: .. _function-parameters-return-variables:
Function Parameters and Return Variables Function Parameters and Return Variables

View File

@ -5,7 +5,7 @@ Layout of a Solidity Source File
Source files can contain an arbitrary number of Source files can contain an arbitrary number of
:ref:`contract definitions<contract_structure>`, import_ directives, :ref:`contract definitions<contract_structure>`, import_ directives,
:ref:`pragma directives<pragma>` and :ref:`pragma directives<pragma>` and
:ref:`struct<structs>` and :ref:`enum<enums>` definitions. :ref:`struct<structs>`, :ref:`enum<enums>` and :ref:`function<functions>` definitions.
.. index:: ! license, spdx .. index:: ! license, spdx

View File

@ -43,12 +43,14 @@ visibility.
Functions Functions
========= =========
Functions are the executable units of code within a contract. Functions are the executable units of code. Functions are usually
defined inside a contract, but they can also be defined outside of
contracts.
:: ::
// SPDX-License-Identifier: GPL-3.0 // SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.8.0; pragma solidity >0.7.0 <0.8.0;
contract SimpleAuction { contract SimpleAuction {
function bid() public payable { // Function function bid() public payable { // Function
@ -56,6 +58,11 @@ Functions are the executable units of code within a contract.
} }
} }
// Helper function defined outside of a contract
function helper(uint x) pure returns (uint) {
return x * 2;
}
:ref:`function-calls` can happen internally or externally :ref:`function-calls` can happen internally or externally
and have different levels of :ref:`visibility<visibility-and-getters>` and have different levels of :ref:`visibility<visibility-and-getters>`
towards other contracts. :ref:`Functions<functions>` accept :ref:`parameters and return variables<function-parameters-return-variables>` to pass parameters towards other contracts. :ref:`Functions<functions>` accept :ref:`parameters and return variables<function-parameters-return-variables>` to pass parameters