We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies.

We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies. Less

We use cookies and other tracking technologies... More

Login or register
to apply for this job!

Login or register to start contributing with an article!

Login or register
to see more jobs from this company!

Login or register
to boost this post!

Show some love to the author of this blog by giving their post some rocket fuel 🚀.

Login or register to search for your ideal job!

Login or register to start working on this issue!

Engineers who find a new job through Blockchain Works average a 15% increase in salary 🚀

Blog hero image

A few Solidity basics: contracts, state variables and functions

Madison Kanna 29 October, 2018 (1 min read)

Solidity is a contact-oriented programming language made for the Ethereum network. You can use Remix as a browser-based IDE for solidity.

Before you start writing any Solidity, you need to tell the compiler what version of Solidity it’s going to use. An example:

pragma solidity ^0.4.19;

Contracts in Solidity are similar to classes in object-oriented programming languages. A contract is the fundamental building block of an Ethereum app. Variables and functions–all of them–belong to a contract. You can create a contract like this:

contract FirstContract { }

Here, the name of the contract is FirstContract, and the body of the contract (what it’s going to do or what code it will run) is everything between the curly braces. Nothing is in the body of the contract right now, but this reserved word of contract marks it as a contract in the code, much in the same way the word object or function would in JavaScript.

State variables are written to the blockchain. They’re stored in contract storage. Every contract has its own storage. With JavaScript variables, they’re stored in memory. Yet with state variables, they’re stored on the blockchain. Pretty darn cool. We can create a string variable, name it firstVariable, and place it inside our first contract:

contract FirstContract {
string firstVariable; }

You can also use a data type called uint. It’s an unsiged integer. Solidity has the idea of signed and unsigned variables. Signed variables can have a positive value and a negative value, while an unsigned integer must only have a positive value. It can’t be negative.

contract FirstContract { uint firstUint; }

In Solidity we have also functions, which are blocks of code to be executed. You can write a function declaration in Solidity like this:

function firstFunction(string firstParam, string secondParam) {
}

A function declaration tells the compiler about a function’s name and its parameters and so on.

It’s said that Solidity was created to be similar to JavaScript, and it’s felt familiar so far. While learning my first programming language it felt excruciatingly hard, but learning a second one that’s similar isn’t quite as difficult (so far). I wish I would’ve spent less time a few years ago looking at Quora answers for the question “What language should I learn first?” Now, I would just dive into one and realize that several programming basic ideas are the same in every language.

Originally published on www.madisonkanna.com