I have a growing public array

struct MatchInfo {
    uint256 matchId;  
    bool isMatchDone; 

MatchInfo[] public matchInfo;

and I want to return an array of uint256 which will loop through matchInfo and find where isMatchdone == false

function getUnfinishedMatches() public constant returns (uint256[]) {
  uint256[] memory unFinishedMatches = new uint[](getMatchCount());

  for (uint i = 0; i <= matchInfo.length; i++) {
    if (matchInfo.isMatchdone == false) {
      unFinishedMatches[i] = matchInfo.matchId;

  return unFinishedMatches;

I saw somewhere in the post that constant returns will not use up gas. Is it true? if it is not true, is the above function a bad way of approach/practice to return an array in solidity? If so, what are the alternatives?

1 Answer 1


It really depends. There are two ways of calling functions from a smart contract

  1. Call them from an Ethereum transaction. This will call the function on-chain and cost you gas from the execution.

  2. Call them read-only without creating a transaction. This costs no gas. This can be done because, since it's a constant function and doesn't modify the state, it can be called without creating an on-chain transaction. This wont work for functions that modify the state because the state wont persist since there is no transaction. This is usually done in web3 by doing call() on the function.

  • Thanks for your answer. So if there is constant modifier, it would not cost any gas because it does not create a transaction. I see. If I take out constant like function sellProduct(string _name, string _description, uint256 _price) public {} then it would create transaction and cost gas right?
    – bbusdriver
    Jan 29, 2018 at 21:03
  • Correct. Note that you can still create a transaction to a constant function if you aren't doing it correctly in Web3. A function named sellProduct probably wouldn't make sense to be constant as the name makes it sound like it would need to modify the state and thus not be constant.
    – natewelch_
    Jan 29, 2018 at 21:13
  • I see. so I still need to use constant correctly. Thanks.
    – bbusdriver
    Jan 29, 2018 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.