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?¶
- First line specifies the license (required for all Solidity files)
pragma solidity ^0.8.0
indicates which Solidity version to usecontract HelloWorld
defines a new contract named HelloWorld- We store a greeting in a private state variable
- An event is defined to track greeting changes
- Two functions are provided:
getGreeting()
to read the current greetingsetGreeting()
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