2

I am new to solidity. I want to print a value of w and h inside rectangle function. how can I do that?

contract ShapeCalculator{
    function rectangle(uint w, uint h) returns (uint s, uint p) {
        //print value of w and h
        s = w * h;
        p = 2 * (w + h);
    }
} 
3

You can emit events.

pragma solidity ^0.4.21;

contract ShapeCalculator{
    function rectangle(uint256 w, uint256 h) public returns (uint256 s, uint256 p) {
        //print value of w and h
        emit Width(w);
        emit Height(h);

        s = w * h;
        p = 2 * (w + h);
    }

    event Width(uint256 w);
    event Height(uint256 h);
} 

You will see these events generated when the transaction is mined

[
    {
        "topic": "0xd9fe7dbbe8bd8e307a7710abb04dfbe406ccdc67a6aff083673fc25c8d9bcd47",
        "event": "Width",
        "args": [
            "1"
        ]
    },
    {
        "topic": "0xff7a085d76b69acdcd09df67f894435c0a628ed4da9d2180e85f4a5b4dbad030",
        "event": "Height",
        "args": [
            "5"
        ]
    }
]

Contract gist https://gist.github.com/cleanunicorn/500e53b86825c391b9d7947cf0c24d00

  • 1
    Can you please provide me example in online editor? – Jay Pastagia Apr 12 '18 at 14:36
  • 1
    Added example, fixed code and a gist with the tested contract – cleanunicorn Apr 12 '18 at 15:06

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.