Back to homepage

Solidity fundamentals: all about functions

Published

A detailed guide about functions in Solidity explaining how to declare them, return statements, visibility, and types (view and pure)


Function Declarations

A function declaration in solidity is pretty similar to ther languages:

// function functionName(type param1, type param2) [visibility] [returns(type)]{  }

function sumNumbers(uint a, uint b) public returns (uint) {
  return a + b;
}

This is a function named sumNumbers that receives 2 parameters of type uint and returns a uint as well. Note that we're specifying the function visibility as public.

There are a few things to keep in mind:

  • Solidity is a tpyed language so we have to indicate the types for the parameters received and for whatever is returned
  • It's a common pattern to start function parameter's names with _
  • Visibility can be public (default), private, internal orexternal (more about this in the next article)
  • Solidty functions can return one, none or multiply variables (more details below)

Function parameters: by value and by reference

In Solidity there are two ways in which you can pass an parameter to a function:

  • Passing parameters by value: in this case, the compiler creates a new copy of the parameter's value and passes it to your function. This allows your function to modify the value without worrying that the value of the initial parameter gets changed.

  • Passing parameters by reference: in this case the function receives a reference to the original variable so if the function changes the value of the variable it receives, the value of the original variable gets changed. This is the case for arrays, structs, mappings, and strings and we can store these variables in memory or in storage

function dealCards(string memory _recipient, uint _amount) public {
  // function code goes here
}

As you can see in the example above, the parameter _recipient is a string so it is passed by reference. Using memory we're indicating that the value will not be written to the blockchain. The _amount parameter is of type uint and so it'll be passed by value to the function.

Return Values

To return a value from a function, the declaration looks like this:

string msg = "Solidity is awesome!";

function sayMessage() public returns (string memory) {
  return msg;
}

In Solidity, the function declaration contains the type of the return value (in this case string).

One big difference with other languages is that functions in Solidity can return multiple variables. For that, we have to indicate all the returned variables and its types in the function declaration:


  function multipleReturns(uint _a, uint _b, string memory _msg) public returns (uint _sum, uint _multiply) {
    uint sum = _a + _b;
    uint multiply = _a * _b;
    return sum, multiply;
  }
  uint result;
  // We can just leave the other fields blank:
    (,result) = multipleReturns();

Notice that if we just want to receive one of the returned values of a function, we can do so as detailed in the example above in which we'll just store the second uint returned.

Solidity view functions

Solidity view functions do not actually change state — e.g. it doesn't change any values or write anything to the contract's state. These are commonly used as getter functions.

pragma solidity >=0.6.0

contract MyContract {
  uint age = 33;
  string message = 'Solidity is awesome!'

  function sendMessage() public view returns (string memory) {
    return message;
  }
}

The Solidity compiler will give us a warning if we do any of the following inside a pure function:

  • Modify state variables
  • Emit events
  • Create other contracts
  • Call other functions not declared as view or pure (see below)
  • Send Ether

Solidity pure functions

Solidity pure functions are the ones that do not access any state variables, not even to read values. For example:

pragma solidity >=0.6.0

contract MyContract {
  uint age = 33;

  function divide(uint a, uint b) private pure returns (uint) {
    return a / b;
  }
}

This function doesn't even read from the state of the app — its return value depends only on the parameters that it receives.

The Solidity compiler will give us a warning if we do any of the following inside a pure function:

  • Read a state variable
  • Call other functions not declared as pure
  • Accessing address(this).balance or
    .balance.

TAGS


If you enjoyed this article consider sharing it on social media or buying me a coffee ✌️

Buy Me A Coffee