Solidity Fundamentals: Functions
Tags: smartcontracts, programming, solidity, blockchain
Published: 2021-01-31 07:22:37

---
title: Solidity Fundamentals: Functions
published: true
date: 2021-01-29 11:28:53 UTC
tags: smartcontracts,programming,solidity,blockchain
canonical_url: https://medium.com/coinmonks/solidity-fundamentals-functions-a7e3d38c1fe5
---

#### ETHEREUM SMART CONTRACTS DEVELOPMENT

#### Functions: Part Two

![](https://cdn-images-1.medium.com/max/1024/1*2KLI3_rd9mzYwpBOG2ccAw.jpeg)

Hello folks! This part will be the final part of **Solidity Fundamentals**. We will continue to work on **functions** and examine the following subjects;

- receive Ether Function
- fallback Function
- Function Overloading
- Events

**receive Ether Function**

receive function declared using receive() external payable { … } without _function_ keyword that can’t have arguments, can’t return anything, and must have external visibility and payable state keyword. One contract can have at most one receive function. It triggers on plain _Ether_ transfers via the usage of send() or transfer(). If contract doesn’t have this function but has payable fallback function, this fallback function will be called on a plain _Ether_ transfer. When neither receive nor payable fallback function exists, contract can’t receive _Ether_ through regular transactions and throws an exception.

> **Warning:** Contracts that receive Ether directly (without a function call, i.e. using send or transfer) but do not define a receive Ether function or a payable fallback function throw an exception, sending back the Ether (this was different before Solidity v0.4.0). So if you want your contract to receive Ether, you have to implement a receive Ether function (using payable fallback functions for receiving Ether is not recommended, since it would not fail on interface confusions).

{% gist https://gist.github.com/FerdiKurt/9e778c396fe5a1bee539ab1d0a8b1673 %}

**fallback function**

A contract can have at most one fallback function, declared using fallback () external [payable] without _function keyword._ It can’t have arguments, can’t return anything, and must have external visibility. It executes when the following situation occurs;

- a function that does not exist in contract,
- Plain Ether sent to the contract without any msg.data provided and there is no receive() ether function. In this scenario, fallback function also must be marked aspayable.

> **Warning** : A payable fallback function is also executed for plain Ether transfers, if no [receive Ether function](https://docs.soliditylang.org/en/v0.7.5/contracts.html#receive-ether-function) is present. It is recommended to always define a receive Ether function as well, if you define a payable fallback function to distinguish Ether transfers from interface confusions.

{% gist https://gist.github.com/FerdiKurt/008839490da9b68bcc4b9391cac63b55 %}

**Function Overloading**

_Overloading_ is a process when a contract have multiple functions of the same name but with different parameter types.

{% gist https://gist.github.com/FerdiKurt/e1c8cc10e05c7a9ee5a03c7f32604242 %}

> **Warning:** Overloaded functions are also present in the external interface. It is an error if two externally visible functions differ by their Solidity types but not by their external types.

{% gist https://gist.github.com/FerdiKurt/7479d6a93ed7c9d3fa29e3b5a552109d %}

**Events**

**_Events_** in Solidity are the same as events in other programming languages and generally used to inform the calling application about the current state of the contract with the help of the logging functionality of EVM. When you call them, they cause the arguments to be stored in transaction’s log — a special data structure in the blockchain. Logs have two portions for keeping parameters: **_data_** and  **_topics_**.

There are two types of Solidity event parameters: **_indexed_** and **_not indexed_** _._ We can mark up to three parameters as indexed which adds them to a special data structure known as **_topics_** that allow us to search for events, for example filtering a sequence of blocks for certain events or even filter events by the contract address that emitted it. All parameters without the **indexed** are data part of the log.

> **Note:** The hash of the signature of the event is one of the **topics** , except if you declared the event with the anonymous specifier. This means that it is not possible to filter for specific anonymous events by name, you can only filter by the contract address. The advantage of anonymous events is that they are cheaper to deploy and call.

We can use events and listen to them for updating our off-chain applications’ states and also as a cheaper form of storage (_more on later in_ **_Advanced Solidity_** ).

{% gist https://gist.github.com/FerdiKurt/c9f9e3dcbe8c7b46ee6f2edd41198896 %}

_See below events for more details after related functions’ executions._

EscrowPlanCreated event emitted with execution of function addEscrowPlan.

![](https://cdn-images-1.medium.com/max/827/1*petAxMEQogP8-g1YY4Y-xQ.png)

With execution of depositEther function, EtherDeposited is emitted.

![](https://cdn-images-1.medium.com/max/806/1*r_8SpbluRn4jcc5WIWJ3TA.png)

Finally withdrawEther resulted with emitting event EtherWithdrawed.

![](https://cdn-images-1.medium.com/max/817/1*Av_n1A9FPIpIhttbUtT73A.png)

Congratulations! This was the end of the **Solidity Fundamentals**. Thanks for joining me along this journey. Much appreciated.

**References**

- [Contracts - Solidity 0.7.5 documentation](https://docs.soliditylang.org/en/v0.7.5/contracts.html#functions)
- [Solidity Fundamentals | Data Location and Assignment Behavior](https://medium.com/coinmonks/solidity-fundamentals-a71bf54c0b98)
- [Solidity by Example](https://solidity-by-example.org/)

### All Parts in Order

- [Layout of a Contract](https://ferdikurt.medium.com/solidity-fundamentals-88ceb4873064)
- [Value Types: Part One](https://ferdikurt.medium.com/solidity-fundamentals-ca824a3ac023)
- [Value Types: Part Two](https://ferdikurt.medium.com/solidity-fundamentals-c94460e3be3d)
- [Operators](https://medium.com/coinmonks/solidity-fundamentals-1fb0e6b3b607)
- [Reference Types](https://medium.com/coinmonks/solidity-fundamentals-e4e4660e16c8)
- [Data Location and Assignment Behaviors](https://medium.com/coinmonks/solidity-fundamentals-a71bf54c0b98)
- [Control Structures](https://medium.com/coinmonks/solidity-fundamentals-1dc2af4b453b)
- [Error Handling: Assert, Require, Revert and Exceptions](https://medium.com/coinmonks/solidity-fundamentals-a95bb6c8ba2a)
- [Functions: Part One](https://medium.com/coinmonks/solidity-fundamentals-d2216be1c2c3)
- [Functions: Part Two](https://ferdikurt.medium.com/solidity-fundamentals-functions-a7e3d38c1fe5)

Feel free to ask any questions.

**Stay safe, do good work, and keep in touch!**

[Ferdi Kurt](https://www.linkedin.com/in/ferdi-kurt-8b910b164/)

* * *