Skip to content

Commit

Permalink
Fixing Variable Shadowing in Solidity: uint256 Redeclaration Error (#636
Browse files Browse the repository at this point in the history
)

The issue arises because the variable `a` is already declared as a
parameter of the `arithmeticError` function:

```solidity
function arithmeticError(uint256 a) public {
    uint256 a = a - 100; // Error: `a` is already declared as a parameter
}

When you redeclare a with uint256 a = a - 100;, the compiler throws an error because you're trying to shadow the parameter a by declaring a new variable with the same name. This is not allowed in Solidity.

To fix this, simply remove the type declaration (uint256) and use the parameter a directly:

solidity
Code kopieren

function arithmeticError(uint256 a) public {
    a = a - 100; // Correct: modifies the existing parameter `a`
}
  • Loading branch information
Guayaba221 authored Dec 27, 2024
1 parent b93cf4b commit b5a8691
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ contract TestContract is Test {
contract ErrorsTest {
function arithmeticError(uint256 a) public {
uint256 a = a - 100;
a = a - 100;
}
}
```
Expand Down

0 comments on commit b5a8691

Please sign in to comment.