Smart Contract 101: What is the Structure of a Contract?
A better understanding of the Blockchain Structure could help us learn how to create smart contracts efficiently, here is a simple overview as part of our Learn series.
Let us study the following smart contract
pragma solidity >=0.4.16 <0.8.0; contract FirstContract{ string _name; function setName(string name) public{ _name = name; } function getName() constant public returns(string){ return _name; } }
The First Line pragma solidity >=0.4.22 <0.6.0; is telling the smart contract compiler which version of solidity compiler your code is using.
At line 2, FirstContract could be replaced by any names, this is the place for you to define the name of your contract.
Inside the curly brackets, there is a string variable called _name, this will occupy space and stored in the blockchain.
Also, there are two functions: setName() and getName().
setName takes a string input name. Its type of visibility is public, which means it can be called both internally and externally. Inside the function, the parameter name will be replacing the existing value stored by _name. As this function changed data in blockchain, it will cost ether.
Another function getName,
Image source: Shutterstock