DEV Community

Cover image for Hello world in Solidity
Hillary Chibuko
Hillary Chibuko

Posted on

2 1

Hello world in Solidity

solidity is the most popular language for writing smart contract for the Ethereum blockchain.today I'm going to be showing you how to print "hello word" in solidity
Firstly the solidity smart contract code starts with a pragma declaration which specifies the version of the compiler to compile our smart contract code

pragma solidity ^0.8.2

The caret symbol before the version number tells the compiler that any compiler version above 0.8.2 can be used to compile the smart contract...
A compiler of 0.9.0 would throw an error same as 0.7...n

After the Pragma declaration then we move to declaring the contract block

`contract HelloWorld {

}`

This is the block that contains all the code for our smart contract,anything outside this block should be either another smart contract definition or Pragma declaration.

Then we move to declaring a string variable to store our text.
Also it is to be noted that data types in solidity are statically typed

So declaring a variable ,the name of the variable must be preceded by the variable type
E.g
string public helloWorld;
Statement in Solidity should end with a semicolon.

and the public declaration before the variable name simply means that the variable can be accessed outside the smart contract..
Meaning any smart contract that inherits from this contract can call this variable

Now I move to declaring the constructor function that assigns value to the variable

constructor() public {
helloWorld = "hello world";
}

Now it's time to put the pieces together

`Pragma solidity ^0.8.2;
contract HelloWorld {
string public helloWorld;

Constructor () public {
    helloWorld = "hello  world";
}
Enter fullscreen mode Exit fullscreen mode

}
`

Happy ccoding🎉🎉

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay