Solidity Programming: Understanding Function Types and the Constructor

In this article, we discuss two different function types available in Solidity: view functions and pure functions. We'll also discuss a special type of function called the constructor.

2 months ago   •   4 min read

By Peter Foy

In this article, we'll discuss the two different function types available in Solidity: view functions and pure functions. We'll also discuss a special type of function called the constructor.

This article is based on notes from this Blockchain Developer course and is organized as follows:

  • View and Pure Functions
  • Understanding the Constructor

View and Pure Functions

To start, we'll create a new file called EampleViewPure.sol and create a contract with the same name.

We'll then create any storage variable, which will be a public unsigned integer  myStorageVariable.

If we deploy this, we have a public variable with a public getter function called myStorageVariable that will return the content of the storage variable.

We also know this getter function is a call, also known as a reading function. There are two types of reading functions:

  • View functions
  • Pure functions

Let's write out a view function to access myStorageVariable that returns our unsigned integer:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.15;

contract ExampleViewPure {

    uint public myStorageVariable;

    function getMyStorageVariable() public view returns(uint) {
        return myStorageVariable;
    }

}

If we deploy this we see the buttons are both the same color in Remix because they're both view functions:

Now let's write a pure function called getAddition to return a+b:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.15;

contract ExampleViewPure {

    uint public myStorageVariable;

    function getMyStorageVariable() public view returns(uint) {
        return myStorageVariable;
    }

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

}
The difference between a view function and a pure function is that the view function can access storage variables, but cannot write them.
A pure function can only call variables that are not storage variables or other pure functions.

In this example, we just uint a and uint b as parameters, we're not using other storage variables, view functions, or writing anything. Below you can see we can add up two integers

In this case, you do not pay anyone to mine the transaction, which is different than a writing function.

Understanding the Constructor

The final Solidity concept we'll cover in this article is the constructor.

Let's create a new file called ExampleConstructor.sol and create a contract with the same name.

We'll then create a public address variable called myAddress. If we deploy this we know it will be initialized with the default 0 address.

Now let's create two functions, one called setMyAddress and the other setMyAddressToMsgSender as we've done before:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.15;

contract ExampleConstructor {

    address public myAddress;

    function setMyAddress(address _myAddress) public {
        myAddress = _myAddress;
    }

    function setMyAddressToMsgSender() public {
        myAddress = msg.sender;
    }

}

Now we can either assign this address to something we choose or to the message sender.

If we want to assign this to our own address, i.e. the one that deployed the smart contract we could set address public myAddress = msg.sender;.

If, however, we want to assign it directly at the time of deployment to another address, we would have to actually call two transactions. This means we cannot make this a variable that is assigned during deployment.

This is where the constructor comes in.

What is a constructor?

A constructor is a special function that is called directly at the time of deployment of a smart contract. It can only be called once and not after the contract has been deployed.

The constructor function also doesn't use the function keyword, accepts any kinds of arguments, and has no visibility specifier. For example, we can set the constructor to a specific address as follows:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.15;

contract ExampleConstructor {

    address public myAddress;

    constructor(address _someAddress) {
        myAddress = _someAddress;
    }

    function setMyAddress(address _myAddress) public {
        myAddress = _myAddress;
    }

    function setMyAddressToMsgSender() public {
        myAddress = msg.sender;
    }

}

If we go to deploy this we now have an address argument that we can provide, which will set myAdress after deployment

Summary: View Functions, Pure Functions, and the Constructor

To recap, there are two types of reading functions: view functions and pure functions.

The difference between a view function and a pure function is that the view function can access storage variables, but cannot write them.

A pure function can only call variables that are not storage variables or other pure functions.

Another special type of function is the constructor, which is called directly at the time of deployment of a smart contract. It can only be called once and not after the contract has been deployed.

In the next article, we'll pull all these concepts together and create a simple blockchain messenger project.

Resources

Spread the word

Keep reading