0

I have a function which returns an uint, but actually gets an object - how is that converted.

e.g.

f

   "0x91f35a1267f2c8d763f7a3fb35ffac845fcbb1417a64b030ed79cbaa4e29694f"

f is defined as the return to the following function:

function testReturn() returns (uint) {
    studentAge=90;
    return 89;
 }

There must be a way to convert the return from this function.

The contract is defined as:

contract CollegeAdmin {
    uint8 public studentAge=21;
    uint16 public studentIncome=15000;
    uint8 public studentScore=4;

    uint8 public studentAttendance=96;

    function testReturn() returns (uint) {
        studentAge=90;
        return 89;
    }
}

Hence f is defined using:

f = con.testReturn()

Also, con is defined:

con = eth.contract(abi).at(addr)

abi - abi for contract addr - contract address

2 Answers 2

1

I tested by adding constant and that worked. I am unsure what it returned before, but with constant added it worked -

   function testReturn() constant returns (uint) {
       studentAge=90;
       return 89;
   }
4
  • Constants methods can not modify state. SO the studentAge will not be updated to 90. Feb 26, 2018 at 11:10
  • That is useful to know about the state variable. I am only testing what functions can do. Constant is needed to return the integer apparently. Feb 26, 2018 at 11:18
  • Yes. In case you have to modify state as well as return values. You have to Solidity events. :) Feb 26, 2018 at 11:23
  • The issue is all about constant. It seems that is needed to do a call otherwise sendTransaction applies. Feb 26, 2018 at 12:03
0

Non-contract functions can not return values. Use Events instead.

Or mark your function constant (or view) (if the function doesn't modify the state).

function testReturn() constant returns (uint) {
    studentAge=90;
    return 89;
 }

The following statements are considered modifying the state:

  • Writing to state variables.
  • Emitting events.
  • Creating other contracts.
  • Using selfdestruct.
  • Sending Ether via calls.
  • Calling any function not marked view or pure.
  • Using low-level calls.
  • Using inline assembly that contains certain opcodes.

If you are not reading any state variables better make function pure.

function testReturn() pure returns (uint) {
        return 89;
     }

Read more about view functions and pure functions.

7
  • The func is coded in a smart contract, and deployed into the ethereum blockchain and then referred via geth and the con object, the question I asked related to converting the returned object into a number. It is not related to view and pure, or events. Feb 26, 2018 at 10:36
  • The smart-contract function can not return a value if this is not constant. It only returns transaction hash. The return type can use while while making calls from some other function. Feb 26, 2018 at 10:39
  • Better if you post the code you are using along with the object you are getting. Feb 26, 2018 at 10:40
  • I am only asking how to convert objects in geth. Feb 26, 2018 at 10:41
  • What object did you want to convert? You are calling a non-constant function and this will not return you 89 (as you may be expecting). You need to use Events. See: returning value from non-constant functions Feb 26, 2018 at 10:44

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.