Anubhav Singh
Anubhav's Blog

Anubhav's Blog

Functions in Solidity

Functions in Solidity

Anubhav Singh's photo
Anubhav Singh
·Sep 16, 2022·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Parameters in functions:
  • Returning Values from function:
  • Visibility of Functions:
  • State Mutability:
  • Payable Functions:
  • Function Modifiers:
  • That's it!

You just started learning solidity and the functions look a bit overwhelming in solidity? Don't worry I'll explain everything about the functions, how to use them, visibility, and mutability in this post.

If you are here then I assume you already have learned any of the high-level programming languages such as C++, Java, JavaScript or any one of the object-oriented programming languages.

Functions work the same way in all other languages but are a bit different in syntax. In solidity, functions have some additional keywords which we will understand more in this post.

Syntax of function:
function function_name(parameters) visibility mutability returns(return_type) { ... }

Functions in solidity start with the function keyword, but some functions don't start with the function keyword such as receive() and fallback() functions.

Parameters in functions:

functions in solidity accept a list of parameters, which can also be empty.

function sum() { // no parameter
  uint256 a = stateVar1 + stateVar2;
}

function sum(uint256 _a, uint256 _b) { // list of parameters
  stateVariableC = _a + _b;
}

Returning Values from function:

Functions in solidity can return any value which has to be defined in the function itself, which can be uint, int, string, bool or bytes.

function sum() view returns(uint256) {
  return (a + b);
}

There are two ways that a function can return values.

Named returns In this, the return type with the name of the returning variable is defined in the function. (No need to return keyword)

function sum (uint256 _num1, uint256 _num2) 
public 
pure 
returns (uint256 _num3) {
  _num3 = _num1 + _num2;
}

Unnamed returns In this return type, the return keyword is used and the values can be returned using the returns keyword.

function sum (uint256 _num1, uint256 _num2) 
public 
pure 
returns (uint256) {
  return (_num1 + _num2);
}

Visibility of Functions:

Visibility of the functions is defined as who and from where a function can be called, there are four types of visibility for functions, public, external, internal and private. State variables cannot be declared as external, but can be declared as public, internal or private.

public: Public function can be called from any contract and account.
external: External functions can only be called from outside the smart contract.
internal: Internal functions can be called inside the contract that defines it and all other smart contracts that inherit from it.
private: Private functions can only be called within the contract that defines the function.

public functions are the most permissive functions whereas private functions are the most restrictive ones.

State Mutability:

view: Functions that are declared with the view keyword can only read the state but cannot modify any state variables.

contract ViewKeyword {
uint256 public num;
function square() view returns (uint256) {
    return (num * num); // reading from state but not changing it
    }
}

pure: Functions that are declared with pure keyword neither can read state variables nor can read variables from the blockchain.

contract PureKeyword {
function square(uint256 _num) pure returns (uint256) {
    return (_num * _num); // neither reading state nor modifying
    }
}

Payable Functions:

Functions declared payable can receive ether into the contract.

contract Payable {
  function deposit() public payable { }
  // call the deposit() function along with some ether
  // it will automatically update the balance of the contract 
}

Function Modifiers:

Modifiers are code that can run before and/or after a function call. Modifiers can be used to restrict access, validate inputs and guard against reentrancy attacks.

contract Modifiers {
  address public owner;
  constructor {
    owner = msg.sender;
  }

  modifier onlyOwner () {
    require(msg.sender == owner, "Not Owner");
    _;
  }

  function changeOwner (address _newOwner) public onlyOwner {
    // onlyOwner here works as a middleware, the code inside
    // changeOwner function will only execute when the code inside
    // onlyOwner modifier is executed successfully
    owner = _newOwner;
  }
}


That's it!

That was pretty much all the basics of how to use functions in solidity. I hope this article was a good read for you. Do share it with your friends and other peers.
Thank you!!

Find me on Twitter

 
Share this