Solidity-021 StorageTomemoryValueTypeAssignment

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

// Define a contract named StorageTomemoryValueTypeAssignment

contract StorageTomemoryValueTypeAssignment {

    // Declare a state variable 'stateVar' of type uint (unsigned integer)

    // and initialize it with the value 20.

    // This variable is stored on the blockchain and its state persists

    // across function calls.

    uint stateVar = 20;

    // Define a function 'getUInt' that is public and returns an unsigned integer.

    // This function demonstrates how assignment works between storage and memory variables.

    function getUInt() public returns (uint) {

        // Declare a local variable 'localVar' of type uint and initialize it with 40.

        // This variable is stored in memory, meaning its value is only temporary and

        // will be lost once the function execution completes.

        uint localVar = 40;

        // Assign the value of 'stateVar' (which is 20) to 'localVar'.

        // After this line, 'localVar' will hold the value 20.

        localVar = stateVar;

        // Update the state variable 'stateVar' to a new value, 50.

        // This change is written to the blockchain and persists beyond this function call.

        stateVar = 50;

        // Return the value of 'localVar'.

        // Despite the change in 'stateVar' above, 'localVar' retains its value of 20,

        // demonstrating that value types are copied when assigned from storage to memory.

        return localVar; // returns 20

    }

}

//Deploy screenshot:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值