Solidity Secrets: The Coding Language Changing the Internet (Part 1)

Solidity Secrets: The Coding Language Changing the Internet (Part 1)

What is Solidity?

Solidity is a programming language for creating smart contracts on the Ethereum network. Smart contracts are code-based contracts that execute themselves. Solidity is built for the Ethereum Virtual Machine (EVM) and is used to develop decentralized applications (DApps), such as token contracts and DeFi protocols.

Solidity Sample Program

pragma solidity >=0.5.0 <0.9.0;

contract Identity

{

string name;

uint age;

constructor() public

{

name="Harshita";

age=21;

}

function getName()view public returns(string memory)

{

return name;

}

function getAge()view public returns(uint)

{

return age;

}

function setAge() public

{

age=age+2;

}

}

State variables in Solidity are like containers that hold information within a smart contract. They are declared at the beginning of the contract and store data that persists between function calls.For example, if you want to create a simple contract that stores a number and a message, you would declare two state variables:

pragma solidity ^0.8.0;

contract MyContract {

uint256 public myNumber; // State variable to store a number

string public myMessage; // State variable to store a message

constructor() {

myNumber = 123; // Set initial value for myNumber

myMessage = "Hello, world!"; // Set initial value for myMessage

}

function updateValues(uint256 _newNumber, string memory _newMessage) public {

myNumber = _newNumber; // Update myNumber with the new number

myMessage = _newMessage; // Update myMessage with the new message

}

}

In this example, myNumber and myMessage are state variables that store a number and a message, respectively. The updateValues function allows you to update the values of these state variables.

Local variables in Solidity are variables that are declared within a function and exist only for the duration of that function's execution. They are used to store temporary data that is needed for calculations or other operations within the function.Here's an example that demonstrates the use of local variables in a Solidity function:

pragma solidity ^0.8.0;

contract MathContract {

function calculate(uint256 _a, uint256 _b) public pure returns (uint256 sum, uint256 product) {

// Declare local variables

uint256 result;

// Calculate the sum of _a and _b

sum = _a + _b;

// Calculate the product of _a and _b

product = _a * _b;

// Calculate the result as the sum of _a, _b, and the product

result = sum + product;

// Return the sum, product, and result

return (sum, product);

}

}

In this example, result is a local variable that is used to store the result of adding sum and product. Local variables are declared inside the function body and are only accessible within that function. Once the function execution is completed, local variables are destroyed, and their values are no longer accessible.

In Solidity, functions are like actions that a smart contract can perform. They can take inputs, process them, and provide outputs. Think of them as the verbs in a contract's language.

For example, let's say we have a simple contract that stores a person's name and age:

pragma solidity ^0.8.0;

contract Person {

string public name;

uint256 public age;

function setName(string memory _name) public {

name = _name;

}

function setAge(uint256 _age) public {

age = _age;

}

function getDetails() public view returns (string memory, uint256) {

return (name, age);

}

}

In this contract, we have three functions:

1. setName: This function takes a string input and sets the name state variable to that value.

2. setAge: This function takes a uint256 input and sets the age state variable to that value.

3. getDetails: This function is a view function, meaning it doesn't modify the contract's state. It simply returns the name and age of the person.

These functions allow us to interact with the contract, setting and getting the values of name and age.

Here's a comparison of view and pure functions in Solidity in tabular form:

Aspectview Functionpure Function
State AccessCan read the contract's state variables.Cannot read or modify the contract's state.
State ModifyCannot modify the contract's state variables.Cannot modify the contract's state variables.
External DataCan read external data (e.g., other contracts' state variables).Cannot read external data.
ExampleReading the contract's balance or status.Performing mathematical calculations without using state data.

Both view and pure functions are important in Solidity, as they help ensure the immutability and predictability of smart contracts by clearly defining which functions can modify states and which cannot.

Example: pure Function

pragma solidity ^0.8.0;

contract MathContract {
    function multiply(uint256 _a, uint256 _b) public pure returns (uint256) {
        return _a * _b;
    }
}

Example: view Function

pragma solidity ^0.8.0;

contract BalanceChecker {
    function checkBalance(address _account) public view returns (uint256) {
        return _account.balance;
    }
}