-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path005-variables-and-scope.sol
57 lines (45 loc) · 1.47 KB
/
005-variables-and-scope.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* Learn about variables and scope
*/
contract LearnVariableAndScope {
// state variable
uint256 public data = 10;
// private variable - can't be accessed out of the contract
uint256 private dataTwo = 50;
// internal variable - can be accessed within this contract and
// contracts that inherit this contract
// private with a lesser restriction power
uint256 internal dataThree = 50;
// do not do this for variables; as this removes or contradicts
// the purpose of having a state variable
// uint256 external dataFour = 50;
function x() public returns (uint256) {
// modifying the state variable
data = 25;
return data;
}
function y() public view returns (uint256) {
// returns 10 if function x is not ran
// but returns 25 after function x runs
return data;
}
// private function, cannot be accessed out of the contract
function z() private returns (uint256) {
data = 35;
return data;
}
// this function can only be called out of the contract
// will produce a compilation error when called
// function b() external returns (uint256) {
// data = 45;
// return data;
// }
function m() public returns (uint256) {
// calls function z to execution
return z();
// test execution for function b
// return b();
}
}