Loading...

Solidity Functions – Complete Guide

Solidity Functions – Complete Guide

Hello Developers ?,
Hope you all are doing well, in this article we will discuss the functions in solidity, even if the concept of function is the same as in other programming languages, the working of functions in solidity differs due to the fundamental architecture of blockchain.

Let’s see what are functions and how they work in solidity.

What are Functions?

Functions are nothing but a block of code or a bunch of statements written together to perform a particular task so that you can perform the same task for different input parameters without repeating the same lines of code.

for example, let’s say you want to add two numbers, so instead of writing code for adding two numbers every time, you can write a function that takes the number as input and sums up the numbers.

How do functions work in Solidity?

Like every other programming language first, we have to define a function, that can be defined either inside or outside of contracts. functions defined outside the contract are called “free functions”.

Now let’s see how we can create a function in solidity . . .

To define a function inside a contract we start with the keyword function followed by the name of the function and then mention the type of input parameters wrapped inside ( ), visibility or scope of the function, mutability, return values (variables), and the function body.

To understand it better way let’s take an example of a function that adds two numbers . . .

function add(uint x , uint y) public pure returns(uint){ return x + y; }
Code language: JavaScript (javascript)

The above function takes two numbers as input as we have mentioned the parameter type as uint, then we declared the function as public which means the function will be accessible from everywhere, and then marked the function as pure which means the function does not modify any state of blockchain and do not even read the state of the blockchain.
In solidity when a function return values we have to specify the list of values returned by the function as output with the “returns” keyword and the type of output value wrapped inside ( ).

Now let’s talk more about the function visibility

Solidity function visibility specifiers

The visibility of the function decides who can access the function, based on this there are four visibility specifiers.

private: accessible only inside the current contract

internal: accessible inside the current contract and in child contracts

external: can be accessed only outside the contract

public: these are accessible from everywhere, from inside and outside of a contract, also public functions create getter functions for state variables

Solidity function state mutability

functions in solidity have certain behavior which can be determined by the state mutability of that function, which tells us how they interact with data stored on the blockchain.

when needed if you do not mark function mutability, the solidity compiler will through a warning.

Functions in solidity can be declared as view, pure or payable.

  • View: functions that read the state (data stored on blockchain) but do not modify the state are declared as view
  • Pure: these are neither read nor modify the state and are declared as pure functions
  • Payable: functions declared as payable allow the function to send and receive the ether, if a it is not marked as payable it will reject the ether sent to it.

Resources

Where can you learn more about solidity functions?

The best way to learn solidity concepts is to follow the official documentation of solidity. The official documentation is always are up to date resource to check out any concept.

Besides official documentation, you can also check out the Master Solidity course by Codedamn in which functions are covered in depth.

Also, you can use Codedamn’s Solidity Playground to practice solidity code which is free to use.

Conclusion 

Even if the concept of function is the same as in other programming languages the functions in solidity work differently as they deal with the data stored on the blockchain and we need to pay a cost to operate on data stored on a blockchain in terms of gas. So it is very important that you write reliable and optimized functions which cost less gas and are safe.

Sharing is caring

Did you like what Shashank Wangkar wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far