0

One of the tutorials in crypto zombies mentions about external function.

contract KittyInterface {
  function getKitty(uint256 _id) external view returns (
    bool isGestating,
    bool isReady,
    uint256 cooldownIndex,
    uint256 nextActionAt,
    uint256 siringWithId,
    uint256 birthTime,
    uint256 matronId,
    uint256 sireId,
    uint256 generation,
    uint256 genes
  );
}

contract ZombieFeeding is ZombieFactory {

  KittyInterface kittyContract;

  function setKittyContractAddress(address _address) external onlyOwner {
    kittyContract = KittyInterface(_address);
  }

Here, setKittyContractAddress is used to change the address in case of emergency. Why does it have to be declared as external?

because it sounds like you have to deploy another contract just to change kitty contract address.

ex:

contract ZombieFeedingInterface {
  function setKittyContractAddress(address _address) external;
}

contract Test {
    address zfAddress = 0x......;
    ZombieFeeding zombieFeeding = ZombieFeedingInterface(zfAddress);

    function changeKittyContractAddress(address _address) private onlyOwner {
        zombieFeeding.setKittyContractAddress(_address);
    }
}

and also create a separate html page (UI) to pass new address to this function.

Isn't setKittyContractAddress better to be declared as internal or private, so the owner doesn't have to create another contract and thus can handle it easily with the existing contract?

What's the benefit of external in this case?

0

Functions that are marked private or internal can only be called from another function in the contract itself (or a derived contract in the case of internal).

If you want to be able to call the function yourself, you need to mark it public or external. Yes, you can then call it from another contract, but more typically you'll call it directly by sending a transaction.

https://solidity.readthedocs.io/en/v0.4.24/contracts.html#visibility-and-getters may help.

| improve this answer | |
  • Thanks for your reply. May I ask you why setKittyContractAddress is declared as external when you can declare it as public and call it without creating another contract. Since it is external creation of a separate contract is inevitable to call the function. This is the part where I am quite confused. – bbusdriver Jul 28 '18 at 7:27
  • 1
    public is a superset of external that also asked internal calls. I can't figure out where you got the idea that you would need to create a separate contract, so I'm not sure how to convince you otherwise. – user19510 Jul 28 '18 at 9:31
  • Sorry, that should read "also supports internal calls." – user19510 Jul 28 '18 at 17:44

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.