Skip to main content

Solidity Syntax - Function

A function is a block of code that performs a specific task. In Solidity, functions are defined using the function keyword followed by the function name and any parameters that the function requires. Solidity functions can be classified based on various properties, including access modifiers, input/output parameters, and function type.

Structure​

The basic syntax for defining a Solidity function is as follows:

function <function_name>(<parameter_types>) <access_modifier> <function_type> returns (<return_types>) {
// Function body
}
  • <function_name> is the name of the function.
  • <parameter_types> is a comma-separated list of the types of the function parameters.
  • <access_modifier> specifies who can call the function. This can be public, private, internal, or external.
  • <function_type> specifies the type of the function. This can be pure, constant, view, or payable.
  • <return_types> is a comma-separated list of the types that the function returns.

Access Modifiers​

Access modifiers determine who can call a function. The available access modifiers in Solidity are:

  • public: Can be called from within the contract, from other contracts, and externally.
  • private: Can only be called from within the contract.
  • internal: Can be called from within the contract and from contracts that inherit from the contract.
  • external: Can only be called externally, and not from within the contract.

Parameters​

Solidity functions can have both input and output parameters.

Input Parameters​

Input parameters are declared like variables and are passed to the function when it is called. For example:

function add(uint256 a, uint256 b) public returns (uint256) {
return a + b;

In this example, the function add takes two uint256 parameters and returns their sum.

Output Parameters​

Output parameters are declared after the returns keyword and are used to return a value from the function. For example:

function add(uint256 a, uint256 b) public returns (uint256 sum) {
sum = a + b;
}

In this example, the add function returns the sum of its input parameters.

Multiple output parameters can be returned from a function by separating them with commas. For example:

function getValues() public returns (uint256, uint256) {
return (10, 20);
}

Function Type​

Solidity functions can be classified based on the type of operation they perform. The available function types in Solidity are:

Pure Functions​

Functions can be declared pure in which case they promise not to read from or modify the state.

function f(uint a) pure returns (uint) {
return a * 42;
}

Payable Functions​

Functions that receive Ether are marked as payable function.

function deposit() payable {
// Accept Ether
}

Fallback Function​

A contract can have exactly one unnamed function. This function cannot have arguments and cannot return anything. It is executed on a call to the contract if none of the other functions match the given function identifier (or if no data was supplied at all).

function() payable {
// Do something
}

Function Modifiers​

Modifiers are used to add restrictions to a function, and they can be used to reduce code repetition. Modifiers can automatically check a condition prior to executing the function.

modifier onlyOwner {
require(msg.sender == owner);
_;
}

function close() onlyOwner {
selfdestruct(owner);
}

In the above example, the onlyOwner modifier restricts the execution of the close() function to only the owner of the contract.

View and Constant Functions​

Functions can be declared view or constant in which case they promise not to modify the state, but can read from it.

function f(uint a) view returns (uint) {
return a * b; // where b is a storage variable
}

It's important to note that the compiler doesn't enforce that a view or constant function does not modify state.

Conclusion​

In this tutorial, we covered the various aspects of Solidity functions. We learned about the syntax and structure of Solidity functions, as well as their parameters, return values, and access modifiers. We also discussed the various types of functions, such as pure, view, constant, payable, and the fallback function. Lastly, we covered the concept of function modifiers, which are used to add restrictions to a function.