1

Assume we have the following solidity contract:

contract khiar {
    function mooz(uint size) external {
        // do something with mooz!
    }

    function bademjoon(uint size) external {
        // do something with bademjoon and then call mooz
        address(this).call(abi.encodeWithSignature("mooz(uint)", size*2));
    }
}

As you can see, mooz is an external function, but we need to call it within bademjoon function in the same contract dynamically (for whatever reason). Is it a valid call or we need to add public modifier to mooz ?

1 Answer 1

1

Is it a valid call or we need to add public modifier to mooz ?

It is a valid call, but it's a strange usecase though.

You do not need to add public and in fact, you can't. It would trigger a compiler error. See the documentation about the function visibility modifers :

Public functions are part of the contract interface and can be either called internally or via message calls.

and

External functions are part of the contract interface, which means they can be called from other contracts and via transactions. An external function f cannot be called internally (i.e. f() does not work, but this.f() works).

The two are incompatible, public allows internal calls, while external doesn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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