# 2. Solidity Basics

```sol
// SPDX-License-Identifier: MIT

// Specify solidity version here
// ^ means any version greater than specified one will work
// >=0.8.7 <0.9.0
pragma solidity ^0.8.7;

// EVM, Ethereum Virtual Machine
// We can deploy solidity code to all evm compatible blockchains
// Avalanche, Fantom, Polygon
```

```sol
contract SimpleStorage {
    // DATA TYPES
    // boolean, uint, int, string, address, bytes

    // This gets initialized to zero!
    uint256 public favNumber;

    // FUNCTIONS
    function store(uint256 _favNumber) public {
        favNumber = _favNumber;
    }
```

```sol
    // VISIBILITY SPECIFIERS
    // 1. public   : visible externally and internally, creates a getter ftn for storage/state vars
    // 2. private  : only visible in current contract, not visible in derived contracts
    // 3. external : only for ftns | only accessible externally not within contracts | can only be called internally via `this.ftn`
    // 4. internal : only visible internally or derived contracts. They cannot be accessed externally. (default visibility)
```

```sol
    // FUNCTION KEYWORDS WHICH DONT SPEND GAS
    // 1. view : ensure they read something from the blockchain state and do not modify
    // 2. pure : ensure they they disallow you to read or modify from blockchains state
    // Note that if a gas calling ftn calls view or pure ftn - then it will cost gas

    function getOnePlusOne() public pure returns(uint256) {
        return(1+1);
    }
```

```sol
    // STRUCTS
    // People public person = People({favNum: 2, name: "Joe"});

    struct People {
        uint256 favNum;
        string name;
    }

    // ARRAYS
    // 1. dynamic array
    // 2. fixed size array
    People[] public people;

    function addPerson(string memory _name, uint256 _favNumber) public {
        // People memory newPerson = People({favNum: _favNumber, name: _name});
        // people.push(newPerson);

        // People memory newPerson = People(_favNumber, _name);
        // people.push(newPerson);

        people.push(People(_favNumber, _name));
    }
```

```sol
    // Places you can store and access data
    // 1. calldata : variable only exists temporarily inside ftn | can be modified
    // 2. memory   : variable only exists temporarily inside ftn | can't be modified
    // 3. storage  : also exists outside function scope | default type | permenant vars that can be modified | can't be put as ftn params
    // These are only required for array, struct or mapping types
    // Others
    // 4. code
    // 5. logs
    // 6. stack
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.nomanaziz.me/development/blockchain/freecodecamp-course/2.-solidity-basics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
