Skip to content

2.8. Functions#

Syntax#


FunctionDefinition = (* function_keyword: *) FUNCTION_KEYWORD
(* name: *) FunctionName
(* parameters: *) ParametersDeclaration
(* attributes: *) FunctionAttributes
(* returns: *) ReturnsDeclaration?
(* body: *) FunctionBody;

FunctionName = (* variant: *) IDENTIFIER
| (* variant: *) FALLBACK_KEYWORD
| (* variant: *) RECEIVE_KEYWORD;

ParametersDeclaration = (* open_paren: *) OPEN_PAREN
(* parameters: *) Parameters
(* close_paren: *) CLOSE_PAREN;

Parameters = ((* item: *) Parameter ((* separator: *) COMMA (* item: *) Parameter)*)?;

Parameter = (* type_name: *) TypeName
(* storage_location: *) StorageLocation?
(* name: *) IDENTIFIER?;

FunctionAttributes = (* item: *) FunctionAttribute*;

FunctionAttribute = (* variant: *) ModifierInvocation
| (* variant: *) OverrideSpecifier (* Introduced in 0.6.0 *)
| (* variant: *) CONSTANT_KEYWORD (* Deprecated in 0.5.0 *)
| (* variant: *) EXTERNAL_KEYWORD
| (* variant: *) INTERNAL_KEYWORD
| (* variant: *) PAYABLE_KEYWORD
| (* variant: *) PRIVATE_KEYWORD
| (* variant: *) PUBLIC_KEYWORD
| (* variant: *) PURE_KEYWORD (* Introduced in 0.4.16 *)
| (* variant: *) VIEW_KEYWORD (* Introduced in 0.4.16 *)
| (* variant: *) VIRTUAL_KEYWORD; (* Introduced in 0.6.0 *)

(* Introduced in 0.6.0 *)
OverrideSpecifier = (* override_keyword: *) OVERRIDE_KEYWORD
(* overridden: *) OverridePathsDeclaration?;

(* Introduced in 0.6.0 *)
OverridePathsDeclaration = (* open_paren: *) OPEN_PAREN
(* paths: *) OverridePaths
(* close_paren: *) CLOSE_PAREN;

(* Introduced in 0.6.0 *)
OverridePaths = (* item: *) IdentifierPath ((* separator: *) COMMA (* item: *) IdentifierPath)*;

ReturnsDeclaration = (* returns_keyword: *) RETURNS_KEYWORD
(* variables: *) ParametersDeclaration;

FunctionBody = (* variant: *) Block
| (* variant: *) SEMICOLON;

(* Introduced in 0.4.22 *)
ConstructorDefinition = (* constructor_keyword: *) CONSTRUCTOR_KEYWORD
(* parameters: *) ParametersDeclaration
(* attributes: *) ConstructorAttributes
(* body: *) Block;

(* Introduced in 0.4.22 *)
ConstructorAttributes = (* item: *) ConstructorAttribute*;

(* Introduced in 0.4.22 *)
ConstructorAttribute = (* variant: *) ModifierInvocation
| (* variant: *) INTERNAL_KEYWORD
| (* variant: *) OVERRIDE_KEYWORD (* Introduced in 0.6.0 and deprecated in 0.6.7. *)
| (* variant: *) PAYABLE_KEYWORD
| (* variant: *) PUBLIC_KEYWORD
| (* variant: *) VIRTUAL_KEYWORD; (* Introduced in 0.6.0 and deprecated in 0.6.7. *)

(* Deprecated in 0.6.0 *)
UnnamedFunctionDefinition = (* function_keyword: *) FUNCTION_KEYWORD
(* parameters: *) ParametersDeclaration
(* attributes: *) UnnamedFunctionAttributes
(* body: *) FunctionBody;

(* Deprecated in 0.6.0 *)
UnnamedFunctionAttributes = (* item: *) UnnamedFunctionAttribute*;

(* Deprecated in 0.6.0 *)
UnnamedFunctionAttribute = (* variant: *) ModifierInvocation
| (* variant: *) CONSTANT_KEYWORD (* Deprecated in 0.5.0 *)
| (* variant: *) EXTERNAL_KEYWORD
| (* variant: *) INTERNAL_KEYWORD (* Deprecated in 0.5.0 *)
| (* variant: *) PAYABLE_KEYWORD
| (* variant: *) PRIVATE_KEYWORD (* Deprecated in 0.5.0 *)
| (* variant: *) PUBLIC_KEYWORD (* Deprecated in 0.5.0 *)
| (* variant: *) PURE_KEYWORD (* Introduced in 0.4.16 and deprecated in 0.6.0. *)
| (* variant: *) VIEW_KEYWORD; (* Introduced in 0.4.16 and deprecated in 0.6.0. *)

(* Introduced in 0.6.0 *)
FallbackFunctionDefinition = (* fallback_keyword: *) FALLBACK_KEYWORD
(* parameters: *) ParametersDeclaration
(* attributes: *) FallbackFunctionAttributes
(* returns: *) ReturnsDeclaration?
(* body: *) FunctionBody;

(* Introduced in 0.6.0 *)
FallbackFunctionAttributes = (* item: *) FallbackFunctionAttribute*;

(* Introduced in 0.6.0 *)
FallbackFunctionAttribute = (* variant: *) ModifierInvocation
| (* variant: *) OverrideSpecifier
| (* variant: *) EXTERNAL_KEYWORD
| (* variant: *) PAYABLE_KEYWORD
| (* variant: *) PURE_KEYWORD
| (* variant: *) VIEW_KEYWORD
| (* variant: *) VIRTUAL_KEYWORD;

(* Introduced in 0.6.0 *)
ReceiveFunctionDefinition = (* receive_keyword: *) RECEIVE_KEYWORD
(* parameters: *) ParametersDeclaration
(* attributes: *) ReceiveFunctionAttributes
(* body: *) FunctionBody;

(* Introduced in 0.6.0 *)
ReceiveFunctionAttributes = (* item: *) ReceiveFunctionAttribute*;

(* Introduced in 0.6.0 *)
ReceiveFunctionAttribute = (* variant: *) ModifierInvocation
| (* variant: *) OverrideSpecifier
| (* variant: *) EXTERNAL_KEYWORD
| (* variant: *) PAYABLE_KEYWORD
| (* variant: *) VIRTUAL_KEYWORD;

Function Definitions#

Functions are the executable units of code. Functions are usually defined inside a contract, but they can also be defined outside of contracts.

contract MyContract {
    function contractFunction() public {
        // Inside the contract
    }
}

function helperFunction() {
    // Outside the contract
}

Functions can be overloaded, where multiple functions with the same name, but with different parameters, can co-exist.

Note

This section is under construction. You are more than welcome to contribute suggestions to our GitHub repository.