Overview page background
HomeOverviewsLearn Solidity
What are Solidity functions?

What are Solidity functions?

Alchemy headshot

Written by Alchemy

Brady Werkheiser headshot

Reviewed by Brady Werkheiser

Published on 2022-10-043 min read

There are multiple types of Solidity functions including view functions, pure functions, special functions, and fallback functions. Functions also can be modified with a function visibility attribute of public, external, internal, and private. This article will introduce Solidity function types, syntax, how to read functions and visibility.

What are Solidity functions?

Solidity functions are self-contained modules of code that accomplish a specific task. Like other web3 programming languages, Solidity allows the developer to write modular code by using functions to eliminate the redundancy of rewriting the same piece of code. Instead, devs can call a function in the program when it’s necessary. 

How to Write and Read a Solidity Function

To write a Solidity function, developers need to structure it according to its proper syntax, and making sure to write its basic components to ensure the function can execute correctly. 

Solidity Function Syntax

Use the following syntax to create your Solidity function, as seen in the example below:

  1. Define the function with the function keyword

  2. Create a name for the function, which is unique and does not coincide with any of the reserved keywords

  3. List any parameters containing the name and data type of the parameter or include no extra parameters

  4. Create a statement block surrounded by curly brackets

function function-name(parameter-list) scope returns() {    // statements }

Note: Solidity events are declared like functions. Events and functions are both integral to high-level dApp work.

The image below shows the three main components of a Solidity function:

  1. Function name

  2. Function type

  3. Return types

An example of a function in Solidity
An example of a function in Solidity

What are the different types of Solidity functions?

There are various types of Solidity functions we’ll be covering in this section including view functions, pure functions, special functions, and fallback functions.

1. View Functions

In Solidity, view functions are read-only and cannot alter the state variables defined in a smart contract. The syntax for a view function is as follows:

function <function-name>() <access-modifier> view returns() {   // function body }

2. Pure Functions

A pure function declares that no state variable will be changed or read. Typically pure functions serve some common utility or calculation. The syntax for a pure function is as follows:

function <function-name>() <access-modifier> pure returns() {   // function body }

3. Special Functions

Solidity has a couple of special functions that you can use when developing a smart contract. Getter and receive functions are important payable functions for smart contracts in Solidity.

Getter Function

State variables defined as public have a getter function that is automatically created by the compiler. The function has the same name as the variable and has external visibility.

Receive Ether Function

A contract can have at most one receive function. A receive function cannot have arguments, is unable to return, and must have external visibility and payable state mutability.

A receive function is executed on a call to the contract that sends Ether and does not specify any function. This function is declared as follows:

receive() external payable { ... }

4. Fallback Function

In Solidity, a fallback function is an external function without a name, arguments, or return values. Fallback functions are executed when a function identifier doesn't match any of the available functions in a smart contract, or if there was no data supplied along with the function call.

What is function overloading?

Function overloading occurs when you have multiple definitions for the same function name within the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.

How does Vyper handle function overloading differently from Solidity?

Unlike Solidity, Vyper, a pythonic language based on Solidity, does not allow for function overloading. Attempting function overloading in Vyper will result in a “Duplicate function or event name” error. 

What does function visibility mean?

Specifying function visibility allows us to control which entities can call functions within the smart contract. There are three types of callers: 

  1. The main contract 

  2. A contract derived (i.e. inheriting) from the main contract 

  3. A third party

Function visibility helps you control which of the above callers can execute the function. There are four types of function visibility: 

  1. Public 

  2. External

  3. Internal

  4. Private

The accessibility of the functions decreases from External to Private: public functions are the most accessible and private functions are the least.

External vs. Public 

External and public are the two function visibilities that can be called from outside of the contract they are defined within. External means that the function can exclusively be called by other contracts or Externally Owned Accounts (EOA). Public means that the function can be called externally or from within the contract itself.

Internal vs. Private

As opposed to external and public, internal and private both disallow external parties from accessing the function. For a private function, only functions within the same contract can call it. For an internal function, functions within the same contract or functions within derived contracts can call it.

Keep Learning About Solidity Functions

This article has introduced you to functions in Solidity. To keep learning, sign up for Alchemy University's Ethereum Developer Bootcamp, to gain access to a free, 7-week asynchronous Solidity crash course. If developers are new to development in general, Alchemy University's 3-week JavaScript crash course is a great prerequisite before starting an Ethereum bootcamp.

Overview cards background graphic
Section background image

Build blockchain magic

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get your API key