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
}
}
Asked
Active
Viewed 7,251 times
2
Ismael
- 30,570
- 21
- 53
- 96
Deepak Baberwal
- 389
- 1
- 5
- 5
3 Answers
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
additionfunction first, which adds the two integers. Then check thegetResultfunction – James Duffy Nov 16 '17 at 15:00