You have already seen functions, haven't you? If not, it's time to practise!



Create a new function named getName that returns the name of you SpacecryptFactory
pragma solidity ^0.4.24; contract SpacecryptFactory { uint public timeout = 60; uint public halfway = timeout / 2; bytes32 public name = 'SuperCryptor 2000'; // Get some inspiration from what is done here function getHalfway() external view returns (uint) { return halfway; } // Add getName() here }

Now that you know how to copy/paste and adapt some code, I can teach you how functions work.

What is it

Functions are a way to interact with a contract. They would be its entrypoints and can wrap complex behaviours.

Declare functions

Let's start with an example.

contract Muffin {
  uint private people = 0;

  // Eat the virtual muffin and tell you how many people tried to do it (you included)
  function eat() public returns (uint) {
    people = people + 1;
    return people;

In this contract, there is:

  • One public unsigned integer: people with value 0
  • One function: eat which returns an unsigned integer



Here is the Spacemuffin contract, the tastiest muffin ever created. You should created a public function called like that increases the number of people that are fans of the muffin
pragma solidity ^0.4.24; contract SpaceMuffin { uint public fans = 0; // Write the `like` function here // (exciting, isn't it?) }

Formal declaration

Did you notice that like has a very different signature compare to getHalfway? Let's recall

function like() public;
function getHalfway() external view returns (uint);

In the following section, you will get to learn how to write your own functions and associated programming concepts.


I told you about signature earlier but haven't defined what a signature is.

A function signature defines input and output of a given functions, along with its name, visibility and modifiers. The signature only cares about what the function is doing rather than how it does it.

Amiral tip

Putting it easy, in Solidity it often consist in replacing the content within {...} by a ;

In Solidity, all signatures can be written as follow

function <name> ([arg1, arg2, ...]) <visibility> [mutability] [returns ([ret1, ret2, ...])] ;