Skip to content

Commit 209aab2

Browse files
committed
Step 6 - Functions
1 parent 9f03be8 commit 209aab2

File tree

8 files changed

+142
-0
lines changed

8 files changed

+142
-0
lines changed

06_functions_2/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Functions
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
pragma solidity ^0.4.23;
2+
contract Contract1 {
3+
4+
int public age;
5+
6+
function setAge(int a) public constant {
7+
// Changing state variable in constant function is not allowed
8+
// This will raise warning but no error, this is not
9+
// strictly enforced yet
10+
age = a;
11+
}
12+
13+
14+
function doSomeWork() public constant returns (int) {
15+
// No warning or error works fine
16+
// Accessing state variable in local and returning from
17+
// function
18+
int b = age;
19+
return b;
20+
}
21+
22+
23+
function doSomeWork2() public constant returns (int) {
24+
// No warning or error works fine
25+
// Returning state variable from function
26+
return age;
27+
}
28+
29+
function doSomeWork3() public view returns (int) {
30+
// Not allowed to change state variables, but shows only warning
31+
age = 10;
32+
return age;
33+
}
34+
35+
// pure function
36+
function doSomeWork4(int b) public pure returns (int) {
37+
// Compilation error, on both changing state variable or access
38+
// state variable
39+
40+
// access or assignment not allowed
41+
//age = b; // not allowed, uncoment and try
42+
//b = age; // not allowed, uncoment and try
43+
return b;
44+
}
45+
46+
// Returning multiple values from function
47+
function doSomeWork6(int c) public returns (int,string) {
48+
return (5,"hello");
49+
}
50+
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pragma solidity ^0.4.23;
2+
3+
contract Migrations {
4+
address public owner;
5+
uint public last_completed_migration;
6+
7+
8+
constructor() public {
9+
owner = msg.sender;
10+
}
11+
12+
modifier restricted() {
13+
if (msg.sender == owner) _;
14+
}
15+
16+
function setCompleted(uint completed) public restricted {
17+
last_completed_migration = completed;
18+
}
19+
20+
function upgrade(address new_address) public restricted {
21+
Migrations upgraded = Migrations(new_address);
22+
upgraded.setCompleted(last_completed_migration);
23+
}
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var Migrations = artifacts.require("./Migrations.sol");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(Migrations);
5+
};

06_functions_2/migrations/2_custom.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var contract1 = artifacts.require("./Contract1.sol");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(contract1);
5+
};

06_functions_2/test/TestContract1.sol

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
pragma solidity ^0.4.19;
2+
import "truffle/Assert.sol";
3+
import "truffle/DeployedAddresses.sol";
4+
import "../contracts/Contract1.sol";
5+
6+
contract TestContract1 {
7+
8+
function testConstantFunction() public {
9+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
10+
meta.setAge(5);
11+
}
12+
13+
function testConstantFunction2() public {
14+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
15+
16+
Assert.equal(meta.doSomeWork(),5,"Testing constant function, value should be 5");
17+
Assert.equal(meta.doSomeWork2(),5,"Testing constant function, value should be 5");
18+
}
19+
20+
function testViewFunction() public {
21+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
22+
23+
Assert.equal(meta.doSomeWork3(),10,"Testing view function, value should be 5");
24+
}
25+
26+
function testPureFunction() public {
27+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
28+
29+
Assert.equal(meta.doSomeWork4(3),3,"Testing pure function, value should be 5");
30+
}
31+
32+
function testMultipleReturnFunction() public {
33+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
34+
// both of the below lines will work
35+
//var (a, b) = meta.doSomeWork6(3);
36+
//(int a, int b) = (5,6);
37+
(int a, string memory b)= meta.doSomeWork6(3);
38+
Assert.equal(a,5,"Testing multiple return funciton, A should be 5");
39+
Assert.equal(b,"hello","Testing multiple return funciton, B should be hello");
40+
}
41+
}

06_functions_2/truffle-config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
// See <http://truffleframework.com/docs/advanced/configuration>
3+
// to customize your Truffle configuration!
4+
};

06_functions_2/truffle.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
// See <http://truffleframework.com/docs/advanced/configuration>
3+
// to customize your Truffle configuration!
4+
networks: {
5+
development: {
6+
host: "127.0.0.1",
7+
port: 8545,
8+
network_id: "*" // Match any network id
9+
}
10+
}
11+
};

0 commit comments

Comments
 (0)