ReasonJun

Solidity : Variable 본문

Blockchain/Solidity

Solidity : Variable

ReasonJun 2023. 10. 15. 00:42
728x90

Variable :

The name given to the memory space that can store variable values, or the memory space itself, is called a 'variable'.

 

State Variables : 

Declared outside of a function. Variable permanently stored in the contract storage.

 

Local Variables : 

Declared inside a function. Exists only during function execution. Not stored in blockchain contract.

 

Global Variables : (ex. Block info , msg info, tx info.)

Built-in variable. Can be called from any location. Variables that can check blockchain information.

https://www.geeksforgeeks.org/solidity-global-variables/

 

Solidity Global Variables - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

// Local Variable

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

// View function : can be declared view in which case they promise not to modify the state. 
// they can view the state variable but can't modify it

contract MyStorage {

    // Function definition showing declaration
    // Scope of Local Variable

    function getResult() public pure returns(uint){

        // Initializing Local Variable
        uint local_var1 = 1;
        uint local_var2 = 2;
        uint result = local_var1 + local_var2;

        // Access Local Variable
        return result;
    }
}

// Pure function : declares that no state variable will be changed or read.
// Global Variables

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract Mystorage {

    function getResult() public view returns(uint){
        return block.number; // Global Variables
    }
}
// State Variable

// SPDX-License-Identifier: MIT

pragma solidity >= 0.7.0 < 0.9.0;

contract SolidityTest {
    uint storedData; // State Variable
    constructor() { // Only one constructor is specified.
        storedData = 10; // Use State Variable 
    }
}
// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract fs_1 {
    uint public a = 3; // state variable : External access is possible. It changes at any time.
    
    function myfin1() external view returns(uint, uint){
        uint b = 4; // local variable => There is no need to set visibility specifiers.
        return (a, b);
    }
}
728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : constant  (0) 2023.10.15
Solidity : Data Type (1)  (0) 2023.10.15
Solidity : storage, memory, calldata, stack  (0) 2023.10.15
Solidity : Smart Contract rule  (0) 2023.10.14
Solidity : What is Solidity ?  (0) 2023.10.13
Comments