forked from HadiRawal/1.Solidity_Basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2. Variables & Functions.sol
53 lines (40 loc) · 2.18 KB
/
2. Variables & Functions.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
//TYPES OF Variables:
// SOLIDITY is typed Lanaguage = we need to inform the type of variable we use unlike JavaScript.
int a = 1; // =>0 = signed integer
uint aa = 2; // it can be negative = unsigned integer
string = b "hello"; // = string
bool c = false; // = boolean (true or false)
address d = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; // =Ethereum Address, it is specific for SOLIDITY for transactions
a = "Hello" // will not work, because we didn't assign the variable type
pragma solidity 0.7.5;
contract HelloWorld {
//State Variable:
string message; // State variables = preserve the state of the entire contract
//Constructor :
constructor (string memory _message) {
message = _message;
}
// constructor runs only ones, when the contract is deployed, this keeps the contract more dynamic
// constructor is used in order to initialize the contract with intial values by the contract creator
//TYPES OF Functions :
function hello(string memory _message) public {
/*string _message: Local variable = we can set variable here within the function, then it is a local one,
readable only within this function.*/
message = _message;
}
// 1. (undefined) (function) : it can change the State variables but it cant print the message i.e. no returns
function hello1() public view returns(string memory) {
return message;
}
// 2. view (function) : it can read state variables but can't change them. onlz viewing (reading)
function hello2() public pure returns(string memory) {
return "HelloWorld from Pure Function";
}
/* 3. pure (function) : this function is not interacting with anything out of this function.
also it cant read the state variables. */
//Special Variables:
//these variables are defined automatically in Solidity.
msg.sender //this is the Ethereum address for the sender of transaction who called this contract.
// msg.sender ==0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
msg.value // this will contain the value of the transaction (ether)
}