1
pragma solidity ^0.4.18;
contract addition {

    address creator;
    uint a;
    uint b;
    uint c;

    function addition() public 
    {
        creator = msg.sender;                                // msg is a global variable
        uint c = uint a + uint b; //here i am getting an error that expected token semicolon got identifier
    }

    function addition() constant returns (uint) 
    {
        return uint c;
    }

    /**********
     Standard kill() function to recover funds 
     **********/

    function kill()
    { 
        if (msg.sender == creator)
            suicide(creator);  // kills this contract and sends remaining funds back to creator
    }

}
3

It's better to use the SafeMath library functions, this is the addition fucntion:

function add(uint256 a, uint256 b) internal constant returns (uint256) {
        uint256 c = a + b;
        assert(c >= a);
        return c;
      }

to add:

c =a.add(b);

Note: If you don't want to use SafeMath (which you should), here is corrected syntax for your original code:

address creator;
uint a;
uint b;
uint c;

function addition() public 
{
    creator = msg.sender;                                // msg is a global variable
    uint c = a + b; //here i am getting an error that expected token semicolon got identifier
}

The difference is that you don't put the type of a variable when referring to it, only when declaring it.

link: SafeMath library

1

You only need to declare uint c the first time — after that you just refer to it as c.

function addition() public { creator = msg.sender; c = a + b; }

There were a few other errors in your code, which I fixed in the following fiddle:

https://ethfiddle.com/O1z7aGXF3Y

You can play around with it there and see that it adds the integers as expected.

  • Thanx but i have run your updated code in remix it is not returning 3. ? – Deepak Baberwal Nov 16 '17 at 8:25
  • You have to run the addition function first, which adds the two integers. Then check the getResult function – James Duffy Nov 16 '17 at 15:00
  • @DeepakBaberwal You cannot return a value from a transaction see this ethereum.stackexchange.com/questions/765/… – Ismael Nov 16 '17 at 21:31
1

This code is working for me :

pragma solidity ^0.4.0;
contract AddInteger{
  uint private c;

function addition(uint _a, uint _b) public constant returns(uint)
  {
     c = _a+_b;
     return c;
  } 
}

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.