mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Documentation.
This commit is contained in:
@@ -6,6 +6,34 @@
|
||||
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 and Return Variables
|
||||
|
||||
Reference in New Issue
Block a user