0

I'm trying to call a function in one contract from another contract with the following simple example:

Untitled.sol:

pragma solidity ^0.4.16;

contract helloworld {
function returnint() returns (uint) {
return 15;
}
}

And the second contract:

pragma solidity ^0.4.16;

import "./Untitled.sol";

contract pullother {

function callFunctionInContractOne (address _address) returns (uint) {
    helloworld contractOne = helloworld(_address);
    contractOne.returnint();
}
}

When calling callFunctionInContractOne() in the second contract, the address of the deployed Untitled.sol is given as a parameter, then the returnint() function in the first contract is called (which should return 15). When I try this in the remix editor I get the following returned instead:

enter image description here

I am copying from calling simple function from another contract but getting a different result. I've also tried running a function on one contract from another on a private blockchain in a similar way but am unsuccessful.

Adding a return value I get the same thing:

enter image description here

2 Answers 2

2

You actually sent a TX that call that method of the first smart contract, that called the function of the second smart contract. The thing is you are on a real blockchain - it seems Ropsten to me - and not in the memory of your computer, so you cannot retrieve values from a contract like that. What remix is returning to you is the transaction receipt after the block is mint.

If you want a ÐApp to be able to retrieve values directly from a contract, create functions with constant returns. And please note soon this constant will be split in view and pure.

0

You should add a return value for callFunctionInContractOne function, as follows:

contract pullother {

    function callFunctionInContractOne (address _address) returns (uint) {
        helloworld contractOne = helloworld(_address);
        return contractOne.returnint();
    }

}

Testing that everything is ok. The result is :

enter image description here

Hope it helps~

2
  • I still seem to run into the same thing
    – ZhouW
    Commented Aug 30, 2017 at 10:36
  • add constant for callFunctionInContractOne function, try it. function callFunctionInContractOne (address _address) constant returns (uint) { helloworld contractOne = helloworld(_address); return contractOne.returnint(); } Commented Aug 30, 2017 at 11:26

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.