-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem2.sol
More file actions
34 lines (22 loc) · 1.69 KB
/
Copy pathproblem2.sol
File metadata and controls
34 lines (22 loc) · 1.69 KB
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
//problem 2
//Write a Solidity program to initialize a state variable to 150 and create a function that returns its value. Similarly, initialize a local variable to 200 and create another function that returns its value.
pragma solidity ^0.8.0;
contract challenge2 {
// This line declares the contract called "challenge2".
uint state = 150; // This line declares a state variable called "state", which is of type uint (unsigned integer) and is assigned an initial value of 150.
// State variables are stored permanently in contract storage (blockchain) and their values persist between function calls.
function returnStateVariable() public view returns(uint) {
// This line declares a function called "returnStateVariable".
// The function is marked as "public", which means it can be called from outside the contract.
// The function is also marked as "view", which means it only reads data from the contract and does not modify it.
return state; // This line returns the value of the "state" variable.
}
function returnLocalVariable() public pure returns(uint){
// This line declares a function called "returnLocalVariable".
// The function is marked as "public", which means it can be called from outside the contract.
// The function is also marked as "pure", which means it does not read or modify data from the contract.
uint b = 200; // This line declares a local variable called "b", which is of type uint and is assigned a value of 200.
// Local variables are temporary and only exist within the function in which they are declared.
return b; // This line returns the value of the "b" variable.
}
}