Contracts, Functions, State Variables, Getters & Setters

I had the same big problem finding details in a console like “kryptoholik.” Your image has hidden the mouse action. Button Create is now Deploy. I think you should correct the video. :stuck_out_tongue_winking_eye:

Nice easy first step… like sticking your toe in the water to see how cold it is, instead of diving straight in… here’s my code.
pragma solidity ^0.4.0;

contract Person {
    
    string name = "";
    uint age = 0;
    
    function getAge() view returns (uint){
        return age;
    }
    
    function setAge(uint _age){
        age = _age;
    }
    
    function getName() view returns (string){
        return name;
    }
    
    function setName(string _name){
        name = _name;
    }
}
1 Like
// set the version of solidity you're using
pragma solidity ^0.4.0;

contract Info{
	
	// define and instantiate vars
	string name = "0";
	uint age = 0;

	// NAME FUNCTIONS

	// setter
	// define the function and it's params
	function setName(string _inputName){
		// set the input param to name;
		name = _inputName;
	}

	// getter
	function getName() view returns (string){
		return name;
	}

	// AGE FUNCTIONS

	// setter
	function setAge(uint _inputAge){
		// set the input param to age;
		age = _inputAge;
	}

	// getter
	function getAge() view returns (uint){
		return age;
	}
}
2 Likes

This is my code:

pragma solidity ^0.4.0;

contract Person {
    uint age = 0;
   string name = "";

    function getAge() view returns(uint){
       return age;
    }

    function setAge( uint _age) {
        age = _age;
    }

    function getName() view returns(string){
        return name;
    }

    function setName(string _name){
        name = _name;
    }
 }

It works fine.

1 Like

Assignment solution:

    pragma solidity ^0.4.0;

    contract Assignment{
    uint private age;
    string private name;
    
    function setAge(uint _age) public{
        age=_age;
    }

    function getAge() public view returns(uint){
        return age;
    }

    function setName(string _name) public{
        name=_name;
    }
    
    function getName() public view returns(string){
        return name;
    }
}

@filip
Could you please elaborate more on the Ethereum architecture and EVM ?

thanks

pragma solidity ^0.4.0;

contract Person {
string name;
int age;

function getName() view returns(string){
    return name;
}
    
function getAge() view returns(int){
    return age;
}
        
function setName (string _output){
    name = _output;
    
}
    function setAge(int _age){
    age = _age;
    
}

}

Hello Everyone, i wrote the code but getter is not working well. Getter function returning “0” for age and nothing for string name. Appreciate your reply.

Hi,

Could you share your code here so I can take a look?

Here my code:

pragma solidity ^0.4.0;

contract Person {
    
    uint age;
    string name;
    
    function getAge() view returns (uint) {
        return age;
    }
    
    function setAge(uint _age) {
        age = _age;
    }
    
    function getName() view returns (string) {
        return name;
    }
    
    function setName(string _name) {
        name = _name;
    }
}

My solution for the first assignment, plain and simple:

pragma solidity ^0.4.0;

contract Person {
    string name;
    uint age;
    
    function setName(string _name) { name = _name; }
    function getName() view returns (string) { return name; }
    
    function setAge(uint _age) { age = _age; }
    function getAge() view returns (uint) { return age; }
}

I’m used to write getters and setters as one-liners from C++ (which I wouldn’t do for functions with more content, of course), but that’s a matter of taste I guess.

However, even this short piece of code leads me to the first question: 256 bit (=32 bytes) as default data type for integers seems quite large to me, does it pay off to explicitely user smaller types with the goal of increasing performance or shrinking storage (and with that, gas) cost? For example, for storing a human age we shouldn’t need more than 16 bit realistically (by now even 8 bit, but hey, let’s assume that rejuvenation research takes off within the next years and there are some hundred years possible), hence we could use uint16 as data type. However, on C/C++ it’s often preferred to use just plaint ints as this selects the platforms register size which is often faster and even leads to shorter code as there are not additional instructions needed to e.g. mask away the upper bits. How does this play out for Ethereum blockchain? I could imagine that using less storage is preferred.

Another question: can Remix IDE also display the generated EVM instructions from the contract or do I need another tool for that? Where can I get the contract in binary format?

Best regards,
Sebastian

Good question. Since solidity works with 32 byte words (256 bits), the most effective way is to use 256 bit integers. It’s actually more effective and consumes less gas than using 8 bit integers.

I believe you can find the EVM instructions if you click on “Details” in the compile tab in remix.

pragma solidity ^0.4.0;

contract Assignment {
uint age ;
string name ;
function setAge(uint AGE) {
age = AGE;
}
function setName(string NAME) {
name = NAME ;
}
function getAge() view returns(uint) {
return age;
}
function getName() view returns(string) {
return name;
}
}

I am doing something wrong when i change my contract and update it. I close the old contract , then i create again but the function bark still printing the same old thing and not update it. help?

fixed : i check auto compile and now i am ok.

pragma solidity ^0.4.0;

contract nameAndAge {

string name = "";
uint age = 0;

function getName() view returns (string){
    return name;
}

function setName(string _name ){
    name = _name;
}

function getAge() view returns (uint){
    return age;
}

function setAge(uint _age){
    age = _age;
}

}

Filip is it not necessary to initialize the value of the state variables when we create them?

Yes you can initialize them when you create them as well.

My solution to the assignment.

pragma solidity ^0.4.0;

contract Identity {
    string name;
    uint8 age;
    
    function setName(string n) public returns (bool) {
        name = n;
        return true;
    }
    
    function setAge(uint8 a) public returns (bool) {
        age = a;
        return true;
    }
    
    function getName() public view returns (string) {
        return name;
    }
    
    function getAge() public view returns (uint8) {
        return age;
    }
}
1 Like

Hey all you good looking people!!! Wass cooookin!! So pumped, got my first smart contract assignment working.


pragma solidity ^0.4.0;

contract nameAge {

string myName ="";
int myAge = 0;

function setName (string _name){
    myName = _name;
}

function setAge (int _age){
    myAge = _age;
}

function getName() view returns (string) {
    return myName;
}

function getAge() view returns (int){
    return myAge;
}

}

1 Like

Hi! it seems to work perfectly.

pragma solidity ^0.4.0;

contract nameAge {

string myName ="";
int myAge = 0;

function setName (string _name){
myName = _name;
}

function setAge (int _age){
myAge = _age;
}

function getName() view returns (string) {
return myName;
}

function getAge() view returns (int){
return myAge;
}
}

1 Like