2
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
    }

}
Ismael
  • 30,570
  • 21
  • 53
  • 96
Deepak Baberwal
  • 389
  • 1
  • 5
  • 5

3 Answers3

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

natewelch_
  • 12,021
  • 1
  • 29
  • 43
Mounir Atouil
  • 369
  • 2
  • 4
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.

James Duffy
  • 321
  • 2
  • 5
  • Thanx but i have run your updated code in remix it is not returning 3. ? – Deepak Baberwal Nov 16 '17 at 08: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 https://ethereum.stackexchange.com/questions/765/what-is-the-difference-between-a-transaction-and-a-call – 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;
  } 
}
niksmac
  • 9,673
  • 2
  • 40
  • 72
sruthi23
  • 361
  • 4
  • 15