Skip to Content

Hello World in Solidity

The simplest possible Solidity smart contract that demonstrates basic structure and a simple function.

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract HelloWorld { // State variable to store the greeting string private greeting = "Hello World!"; // Event emitted when greeting is changed event GreetingChanged(string newGreeting); // Returns the current greeting function getGreeting() public view returns (string memory) { return greeting; } // Changes the greeting function setGreeting(string memory _newGreeting) public { greeting = _newGreeting; emit GreetingChanged(_newGreeting); } }

What’s happening here?

  1. First line specifies the license (required for all Solidity files)
  2. pragma solidity ^0.8.0 indicates which Solidity version to use
  3. contract HelloWorld defines a new contract named HelloWorld
  4. We store a greeting in a private state variable
  5. An event is defined to track greeting changes
  6. Two functions are provided:
    • getGreeting() to read the current greeting
    • setGreeting() to change the greeting

This simple example demonstrates:

  • State variables
  • Functions (both view and state-changing)
  • Events
  • Basic string handling
  • Public/private visibility

Try it yourself in the Remix IDE: Open in Remix 

Last updated on