Create your account

Already have an account? Login here

Note: By joining, you will receive periodic emails from Coursetro. You can unsubscribe from these emails.

Create account

Ethereum Smart Contracts: Variables and Types Tutorial

By Gary simon - Oct 23, 2017

The following tutorial is a part of our 100% free course: Developing Ethereum Smart Contracts for Beginners

In the previous lesson (video or written), you learned what a basic blockchain is, along with what smart contracts and decentralized apps are.

In this tutorial, you're going to begin learning the absolute basics of smart contract development. So, let's get started.

If you prefer watching a video instead..

Be sure to Subscribe to the Official Coursetro Youtube Channel for more videos.

The Remix IDE

Remix is a browser-based IDE built by the Ethereum development team. You can choose to either install it locally through the repository at the Github repo, or simply visit the online version at For this course, we're going to use the online version.

The purpose of the Remix IDE is to allow you to write and deploy Solidity smart contracts. It also provides you with useful debugging features.

Visit the Remix IDE URL. You will see a similar interface.

The left column contains your Solidity files, which store your smart contracts in the .sol format. In the middle column top row resides the area in which you write your solidity code, and the bottom row contains your debugger.

The right column contains a series of tabs including Compile, Run, Settings, etc..

At this point, we only need to concern ourselves with the Run tab, where there's an "Environment" dropdown. By default, we want to leave it at the default setting of JavaScript VM, though we will be changing this later on in the course. The Javascript EVM (Ethereum Virtual Machine) means that you will run and deploy your smart contracts locally; they are not live. This is ideal when we're learning about the absolute basics, because it's very quick.

Smart Contract Basics

When you first visit the Remix IDE, it presents you with a sample smart contract. We don't want that, we're going to create our own from scratch.

Click on the (+) icon on the upper left portion of the editor to create a new smart contract. Call it "Coursetro.sol", or whatever you wish.

Start off by writing (or pasting) the following code:

pragma solidity ^0.4.18;

contract Coursetro {

The very first line defines the version of solidity you're going to use.

Next, you define the contract and its name and open it up like a JavaScript class.

On the right side of the browser under the Run tab, you will see a Create button. Click on this.

You will notice that underneath the Create button shows a new section with the name of the contract and "at 0x..." (memory):

This means that the smart contract (yes, it's not very smart at this point) lives at an address. To see this full address along with other information, in the debugger if you click on the Details button, it will provide you with more information:

Notice there's a contractAddress, this is where the smart contract actually lives. No, it's not live on the Ethereum Blockchain because right now, we're simply working within the Javascript EVM.

Also notice gas. Every time a contract is deployed and modified, nodes on the Ethereum network must verify the contract. It's referred to as being redundantly parallel, as a way to reach consensus. 

Gas is the name for the execution fee that senders of transactions (in our case, senders of a smart contract transaction) will pay for verification.

Elaborating on this further, from

"Gas is the name for the execution fee that senders of transactions need to pay for every operation made on an Ethereum blockchain. The name gas is inspired by the view that this fee acts as cryptofuel, driving the motion of smart contracts. Gas is purchased for ether from the miners that execute the code. Gas and ether are decoupled deliberately since units of gas align with computation units having a natural cost, while the price of ether generally fluctuates as a result of market forces. The two are mediated by a free market: the price of gas is actually decided by the miners, who can refuse to process a transaction with a lower gas price than their minimum limit. To get gas you simply need to add ether to your account. The Ethereum clients automatically purchase gas for your ether in the amount you specify as your maximum expenditure for the transaction."

Smart Contract Variables & Types

The most simple concept in any language is the variable. Because Solidity is statically typed (that is, the type of the variable must be defined before compile time), you must specify the type of the variable.

Let's define a string variable in our contract:

pragma solidity ^0.4.18;

contract Coursetro {
   string fName = 'Gary';

In Solidity, you define a variable by first specifying its type.

What other types are there?

  • bool
    This is a Boolean, which returns true or false.

  • int / uint
    Both int and uint represent integers, or number values. The primary difference between int and uint (Unsigned Integer), is that int can hold negative numbers as values.

  • address
    The address type represents a 20 byte value, which is meant to store an Ethereum address. Variables that are typed as address also have members, including balance and transfer.

  • bytes1 through 32
    This is a fixed-size byte array.

  • bytes
    A dynamically-sized byte array.

  • string
    A dynamically signed string.

  • mapping
    Hash tables with key types and value types. We will look at mappings more in depth later on in the course.

  • struct
    Structs allow you to define new types. We will also cover this more in depth shortly.

 Let's also define my age. No one can have a negative age, so we will use an unsigned integer for this:

pragma solidity ^0.4.18;

contract Coursetro {
   string fName = 'Gary';
   uint age = 34;

Hit Create to create the contract. At this point, if you look under details or try to drop down the contract on the right column, you will see nothing.

Where are our variables?

Public and Private

Solidity has four types of visibilities for both functions and variables:

  • Public
    This allows you to define functions or variables that can be called internally or through messages.

  • Private
    Private variables and functions are only available to the current contract and not derived contracts.

  • Internal
    Fuctions and variables that can only be accessed internally (current contract or derived).

  • External
    Functions that can be called from other contracts and transactions. They cannot be called internally, except with "this.functionName()".

Let's add the public visibility to our variables:

pragma solidity ^0.4.18;

contract Coursetro {
   string public fName = 'Gary';
   uint public age = 34;

Click Create, we can now see on the right that we have 2 blue buttons with the name of our variables and the associated values.

When you define public state variables, the EVM creates getter functions for them. So, you can actually click on these buttons and it will return the value, as if it were a function.

Smart Contract Constructor

Every smart contract has a constructor function. This constructor is called when a contract is created. Inside of it, you can define the values of variables.

Let's re-adjust our code to work with a constructor:

pragma solidity ^0.4.18;

contract Coursetro {
   string public fName;
   uint public age;
   function Coursetro() public {
       fName = 'Gary';
       age = 34;

If you hit Create, you will see the result is the same.


Constant Variables

Variables can be declared as being constant. As the name suggests, these are variables with a constant value that does not change.

Let's transform the fName variable to a constant variable:

pragma solidity ^0.4.18;

contract Coursetro {
   string constant fName = 'Gary';
   uint public age;
   function Coursetro() public {
       fName = 'Gary';
       age = 34;

In your IDE, you'll notice a red "X" next to line 9. If you hover over it, you will notice that it says, "TypeError: cannot assign to a constant variable"

Remove line 9 and the error goes away.

Setting Variables

Our smart contract at this point is pretty boring. Let's integrate a potential user interaction where we can manually define a Coursetro Instructor's name and age.

Paste the following:

pragma solidity ^0.4.18;

contract Coursetro {
   string fName;
   uint age;
   function setInstructor(string _fName, uint _age) public {
       fName = _fName;
       age = _age;
   function getInstructor() public constant returns (string, uint) {
       return (fName, age);

As you can see, we got rid of the constructor for now. 

We have two functions, setInstructor() and getInstructor().

setInstructor accepts 2 parameters, _fName and _age. Once called, we set our string fName to the returned _fName, and same with age.

Then, our getInstructor() function is defined as being constant, and it returns a string and a uint.

This is where we return the fName and age variable once it's called.

Click Create and under the red set Instructor button, type in: "Gary", 34 and click the button.

Next, click on the getInstructor() button and you will notice it now returns the inputed value! This is how you set variables from user input in a smart contract.


Hopefully you're staring to understand how things work in a basic sense with smart contracts.

In the next video, we're going to step outside of Solidity for awhile, and actually connect our smart contract with a web-based user interface where we will be able to call our 2 functions: setInstructor() and getInstructor()!

This tutorial is a part of our 100% free course: Developing Ethereum Smart Contracts for Beginners

Share this post

Say something about this awesome post!