...

/

Solidity Functions: Declaration

Solidity Functions: Declaration

Learn about declaring functions in Solidity.

A function is a block of code that takes an optional input (called its arguments), performs some operations with it, and returns an optional output.

The execution of Solidity code must be initiated by an EOA calling a function in a contract. These interactions are known as external function calls, and they create an actual EVM message call. Functions can, in turn, call other functions, which is known as an internal function call, but the initial function call has to originate from an EOA.

A function is declared with the function keyword and has the following format:

function functionName (<optional list of arguments>) <visibility> <optional special type>
returns(<optional list of return values>){
<local variables declarations>
<operations>
}

Function arguments

If a function requires some input data, its type and name must be listed in brackets upon declaring the function. If the parameters of a function correspond to some values assigned to a state variable (say a uint number), a common naming practice is to give them the same name preceded by an underscore (uint _number).

For instance, in the following contract, we define a setter function for a ...