Smart Contract Development
The course is part of this learning path
This course provides a deep dive into the Solidity programming language which is used to implement smart contracts on blockchain platforms. This course will give you a practical understanding of Solidity and how to develop your own smart contracts.
In this lecture, I really want to highlight the getter and setter functions from solidity. We have talked about them a little bit before but this video will really all about getter and setter functions. For this video, I have prepared a little coding example and you can either follow along in the video keep writing with me to code or you can have a look at our code repository you find the final code there which I put it there after I finished writing this code here in this lecture. Now for a starter I want to start with this very simple example. I have a contract and in my contract I have one variable and unsigned integer 856 called myUint and when I run this smart contract then I cannot do anything there is no interface whatsoever. So, in order to actually do something with our smart contract in order to write any values to store any values or read any values. I would have to have functions to have an interface between what is stored in there and what I can store in there. And this is what getter and setter functions are all about. Getter functions, get values, read values, set of functions, set values, write values, store values. Now I have several options to read values from a smart contract and the easiest one I've showed you before is when I just make a variable public in solidity if I make this variable public then solidity will automatically create a function with the same name as the variable name itself. When I hit 'Deploy' here and have a look at my new smart contract and I have a function name myUint same function name as my variable name. And when I execute this function then I get back the value of this uint. And you can do this with any type of variable in the next video or next videos we're talking about different variable types. We're talking about value types, reference types, we're talking about erase mapping strucks and so on. So, it doesn't really matter which kind of variable you have if you make it public solidity will automatically create this getter function. Now it comes with a specific drawback. Well it's not so apparent with an unsigned integer which is a simple variable type you want just want to get back this value. It becomes more problematic if you have something like a mapping which we can discuss later or erase where you maybe want to have specific keys. And then the interface that solidity creates to get the actual variable might not be the one that you want to have between your outside world and the smart contract. Now let me explain this with a simple example. Let's say you have another unsigned integer and I'm going to make this public again and I call this myotherUint maybe that's coordinates X, Y coordinates or maybe that's some array value that you store there or something that you always want to give back in the list together. So, it wouldn't make sense when I deploy that you have to make two calls to your smart contract in order to get back both values. Maybe you want to have them back together. So, in that case you could write a function a getter function to return both values at the same time. Let me write this, getter function for you and then discuss it. So, we have function. And then for the sake of the example here I call this getBothValues. It doesn't take any arguments. It's a public function. It's a public view function and it returns to uints. And that would be return my unit and return my other unit. Now when I deploy this smart contract then I can get first myOtherUint or myUint, but I can also get them both together at the same time. So, in that case I could influence how I can get the values back. And I personally prefer actually to have actual getter functions even though they maybe do exactly the same as the public key words creating getter function for a simple variable. But I personally really prefer not using the public key word, but have getter functions first for readability. Make it easier to read my smart contract. Make it easier to audit my smart contract and I have full control about what is actually returned. So, for me that is superior over this public key word. But if you just want to have a quick way to read our values that's a perfectly valid and fine methodology to do so. Now, this is pretty much everything I want to cover with the getter functions. Another set of functions are not very different. Setter of functions are functions to write values and there is no automatic way to create setter functions in solidity. I think there are some IDS starting to create templates to automatically create this settter of functions in code. I think there are plug ins for Visual Studio Code which we might discuss later. But for now I want to keep it extremely simple. And I just write one function which sets the value to the myUint. And then an other function which sets both values. So, just to demonstrate how this works. So, we have a function, setMyUint and this takes one uint and this is myUint. And this is a public function. And this sets myUint is myUint and now before I write the other function this is the actual challenge of this lecture. Go create a function which sets both of the uints at the same time. If you want to do this challenge, then pause the video now. If you don't want to do this challenge or if you just want to keep watching then just pause the video now. Now let's write this function function setBothValues for example. And this is just an example name. Any other name that is meaningful would work here. And then we have one uint and myUint and then another myOtherUint and this is obviously also a public function or else we cannot access it. And then we say, myUint is myUint and my other uint is my other uint. And now let's give this a try. I'm going to remove my already deployed smart contracts because I don't need them anymore. Hit 'Deploy' set myUint to five and then getBothValues. So, I see the first one is five, the second one is zero which corresponds to getBothValues myUnit is five and my other unit is zero. And when I setBothValues let's say it was six and seven. Then my values will be set to the corresponding values. That's it for this lecture. And in the next one we're talking about variables and addresses and I'll see you in the next one.
Tom is a CTO, senior back-end developer, and systems architect with over twenty years of hands-on development experience in a variety of languages and systems. He has a CS master's degree and has been working with Ethereum and blockchain technologies since 2016.