1

Can someone explain to me why external is used in below code snippet? A general clarification of how the visibility modifiers work in practice would be great, I've always understood that external meant that the function can only be called from outside the contract, yet this particular piece is a part of the contract, so wouldn't internal or private be the right one to use?

function finalizePresale() external onlyOwner {
    require (!isFinalized); 
    isFinalized = true;                          
    token.transferOwnership(owner);                            
  } 

1 Answer 1

0

If its private finalizePresale would not be called outside the contract and if its internal they would not be called apart from the contract which created the function.

If you look at the function there is also a modifier which has OnlyOwner,I guess the intention is to expose the function outside the contract and make it accessible to only owner of contract for which external suits better.

5
  • Why would you want to expose the function to outside the contract if it's only the owner of the contract that has access anyway?
    – 6egic
    Oct 9, 2017 at 12:48
  • It's the only way you would be able to call this function. External means the function can only be called from outside the contract, for example from the web front-end. Being the contract owner doesn't mean you have access unless you explicitly code it that way. For example, there could be a button on the website that upon click finalizes the presale, and the only way for it to work is if the one clicking the button is the owner of the contract. Once you deploy the contract, being the owner doesn't mean anything unless you code the functions. Oct 9, 2017 at 13:22
  • @ABGhostCoder : Assume a case where you have function that collects all ether and sends it to owner address, you never want that function to be called by all but only by owner of the contract ? If you don't expose such function without OnlyOwner modifier,then there would be no way out.
    – Rangesh
    Oct 9, 2017 at 14:58
  • made sense, I tried making the function internal and then I couldn't call it from outside anymore. So external is used for one to be able to call it from outside, how do you choose between public/external and internal/private?
    – 6egic
    Oct 9, 2017 at 22:50
  • You can find the documentation here solidity.readthedocs.io/en/develop/… .
    – Rangesh
    Oct 10, 2017 at 4:13

Your Answer

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

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