CH2. Solidity data types: information stored in a variable, such as a specific value or a reference address of a memory location. Solidity data types can be categorized into value types and reference types.
Value types: These store data within a defined memory space. Changes to a copied value type will not alter the original value type.
| Value Type | Description | Example/Details |
|---|---|---|
| Signed Integers | Stores positive and negative whole numbers. | Size options: int8, int16, ..., int256 (increments of 8 bits, max size: 256 bits) |
| Unsigned Integers | Stores only non-negative whole numbers. | Size options: uint8, uint16, ..., uint256 (increments of 8 bits, max size: 256 bits) |
| Booleans | Represents true or false values. | bool: true or false |
| Addresses(address_ | Stores Ethereum addresses (20 bytes or 160 bits). | 1. address : basic address type |
address payable: supports transfer and send for ETH transfers |
| Fixed-size Byte Arrays | Arrays of fixed byte size. | Declared as bytes1, bytes2, ..., bytes32 (up to 32 bytes max) |
| Enums | User-defined data type to represent a set of named constants. Improves readability and maintainability for smart contract | Example: enum Status { Active, Inactive, Suspended } |Syntax
function functionName(parameter1Type parameter1Name, parameter2Type parameter2Name) visibility [modifiers] returns (returnType) {
// function body
}
In Solidity, functions can be categorized based on their interaction with the blockchain state:
| Description | Example/Details | |
|---|---|---|
| View Functions | Can read state variables defined in the smart contract but can not modify them | function getBalance() public view returns (uint) { |
| return balance; | ||
| } | ||
| Pure Functions | Can’t read nor modify state variables defined in the contract. Mostly used for calculations only based on parameters. | function add(uint256 a, uint256 b) public pure returns (uint256) { |
| return a + b; | ||
| } | ||
| Payable Functions | can recieve ether, they are essential for financial transactions. | function deposit() public payable { |
| balance += msg.value; | ||
| } |
Function Visibility
| Public | Can be called internally and externally |
|---|---|
| Private | Can only be called from within the current contract |
| Internal | Can be called internally and by derived contracts |
| External | Can only be called from outside the contract |
| Description | Example/Details | |
|---|---|---|
| Basic Return Types | Solidity functions can return simple data types like integers, booleans, addresses, and strings | function getResult() public pure returns (uint) { |
return 42; |
||
| } | ||
| Multiple Return Values | Solidity allows functions to return multiple values | function getMultipleValues() public pure returns (uint, bool, string memory) { |
return (42, true, "Hello"); |
||
| } | ||
| Named Return Variables | In Solidity, if you name the return variables in the returns clause, you can assign values to them directly within the function body without an explicit return statement. | |
| However, you can still use return (sum, product); if you prefer. | function namedReturn() public pure returns (uint product, uint sum) { |
uint a = 5;
uint b = 3;
product = a * b;
sum = a + b;
}
**No need to explicitly return since product and sum are assigned.
|
string, bytes, structs, and arrays, you need to specify the data location (memory, storage, or calldata).returns (string memory)