Solidity Counter

whiteboard crypto logo
Published by:
Whiteboard Crypto
on

This contract is a simple counter written in Solidity. The variable “count” starts at 0 by default, and the functions “increase” and “decrease” change the number respectively.

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

contract counter{

    uint public count;

    function increase() public {
        count++;
    }
    
    function decrease() public{
        count--;
    }
}

Here is the output when we click the “increase” function twice:

Solidity counter code output

Let’s go through line by line:

The first 2 lines of code are the SPDX License Identifier, and the Solidity Version identifier.

contract counter{

The above line of code starts the counter contract, and it is named “counter”. The open curly bracket starts the contract, and there will be a closing curly bracket at the end, which ends the contract.

You can see everything inside the contract itself is indented. This is on purpose to make the code organized and easier to read.

    uint public count;

This line of code declares an unsigned integer, which is public, and named “count”. Unsigned integer, just means it can be any number from 0 to 2^256. Public means it’s access isn’t restricted (meaning anyone can view and see it)

Next, we have our first function!

function increase() public{

The first word in this line of code states that we are creating a function. The second word “increase” is the name of the function we are naming it, and is the name we need to call if we want to use this specific function in the future.

The parenthesis immediately after the word “increase” indicate that the function name is over, and are empty at the moment. If we wanted to give our function some information, we would fill the space between these parenthesis with information, however this function requires no information, so it is blank.

Finally, the word public is the access modifier of this function. Public means anyone can use this function, including you, me, and other contracts. We then end this line with another opening curly bracket to indicate the start of our function.

count++;

This line of code is what is actually performing what we want it to do. The word “count” is followed by two plus’ and then a semicolon. The two plusses indicate that we want to add one to the value of “count”. This means if the old value of “count” is 5, we wish to update it to 6.

“Count++” is a simpler way of writing “count = count + 1”, which means add one to the value of what “count” currently is. Of course, we also end the line with a semicolon to let the compiler know we are done with this line of code.

After that, we end the function.

}

This concludes our first function named “increase”, which takes the value of the state variable “count”, and adds 1 to it.

The next line of code is very similar to the last function. In fact, the only changes we make in this function include changing the name to “decrease” and altering what it does.

function decrease() public{
    count--;
}

In this case, “count–” does exactly what you think it would do… it subtracts one from the current value of “count”. This function is also public, requires no inputs, and is surrounded by curly brackets.

Finally, since we only want to create 1 state variable, and 2 functions, we can end our entire contract with a closing curly bracket.

whiteboard crypto logo

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.