Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.17;
- // specify the version of Solidity that our code is written with
- contract Inbox {
- // define a new contract (almost identical to a class) that will
- // have some number of methods and variables
- string public message;
- // - storage variables:
- // declares all of the instance variables (and their types)
- // that will exist in this contract
- // - public defines who have access to that variable
- // - message is the name of the variable --> the value of the variable will be
- // stored in the ethereum blockchain for ethernity
- // IN CONTRAST WITH STORAGE VARIABLES WE HAVE LOCAL VARIABLES WHICH ARE NEVER STORED IN THE BLOCKCHAIN
- function Inbox(string initialMessage) public {
- // this function is called constructor function since it has the same
- // name as the contract.
- // A constructor function is automatically called one time when the
- // contract is created. Invoked automatically when the contract is deployed
- message = initialMessage;
- }
- function setMessage(string newMessage) public {
- // this functions with a different name from the contract's one can be called
- // once the contract has been deployed on the blockchain
- message = newMessage;
- // we are modifying the message variable here so we can't use either constant and view in the function type
- }
- // this function is unuseful since the message variable is public so solidity will
- // create a function for use to return the message variable's value
- function getMessage() public view returns (string) {
- // public and view are the function type declaration
- // most common function types --> [public, private, view, constant, pure, payable]
- return message;
- // we are just accessing data not modifying. This is why we use the view function type
- // so the compiler knows we only access data.
- // - returns (with return's type) can only be specified with functions that have
- // view and constant aas function type.
- }
- }
Add Comment
Please, Sign In to add comment