• Wed. Sep 21st, 2022

Solidity Functions that do not consume any gas

ByWikafever

Aug 8, 2022 ,

Why I wrote this blog? I had an assumption that only integers (read operation) does not require gas fees, but as I explored more contracts, below are few different functions that do not consume any gas.

Generally functions with view or pure modifier do not incur any gas fees.

1. Printing an integer without reading

pure modifier means that no reading of (state) variables can be performed by the function.

//SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
contract basic1 {
function printHello() public pure returns (uint) {
return 5;
}
}

2. Printing an integer with reading

view modifier can allow only reading (state) variables

//SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
contract basic1 {
    uint val = 5;
    function printHello() public pure returns (uint) {
return val;
}
}

3. Printing a string without reading

//SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
contract basic1 {
function printHello() public pure returns (string memory) {
return "Hello World"
}
}

4. Printing a string with reading

//SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
contract basic1 {
string strdata = "Hello World";
function printHello() public view returns (string memory) {
return strdata;
}
}

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read


Solidity Functions that do not consume any gas was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.