Solidity Functions that do not consume any gas

Takshil Patil
Coinmonks

--

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 when they are called externally. pure and view functions still cost gas if they are called internally from another function.

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

--

--