2

I am trying to compile this code:

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

contract tonsofun {
    uint public i;
    bool public b;
    address public myAddress;
    }
    function foo() external {
        uint x = 123;
        bool f = false;
    }

3 Answers 3

6

Your foo() function is outside the contract. and according to the documentation

A free function behaves like an internal function of the contract that called it.

therefore shouldnt not have the external visibility keyword.

1
  • When I remove "external" I get...SyntaxError: Free Functions cannot have visibility
    – dustycar17
    Feb 23, 2022 at 14:27
1

You said you removed the external keyword. Are you sure your code is like this?

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract TonsoFun { 
    uint public i;
    bool public b;
    address public myAddress;
}

function foo() { 
    uint x = 123;
    bool f = false;
}

I've tested this out and it's working fine.

In case you need more explanation on free functions in solidity

2
  • contract tonsofun { uint public i; bool public b; address public myAddress; ///do not put a } after myAddress; function foo() external { uint x = 123; bool f = false; i = 2; b = false; x = 44; f = true; myAddress = address (1); } }
    – dustycar17
    Feb 24, 2022 at 0:11
  • contract tonsofun { uint public i; bool public b; address public myAddress; ///do not put a } after myAddress; function foo() external { uint x = 123; bool f = false; i = 2; b = false; x = 44; f = true; myAddress = address (1); } }
    – dustycar17
    Feb 24, 2022 at 0:11
1

Richard, you were having issues with the curly brackets; you didn't place some correctly.

There should be no curly bracket after " address public myAddress; } "

Delete it. Then put another curly bracket after the enclosing curly bracket of the foo function.

This is what I am saying, everything should be like this:

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

contract tonsofun {
    uint public i;
    bool public b;
    address public myAddress;
    
    function foo() external {
        uint x = 123;
        bool f = false;
    }
}

Fixed

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.