Defining More Functions in Our Smart Contract
Learn to add other important functions to our smart contract.
We’ve looked at functions and learned how to define them, so now let’s extend the functionality of our current smart contract and add a few more important functions.
Implementing read-update-delete
Here’s the current state of our contract.
Our idea for the contract is a basic CRUD that manages contact details. Right now, our contract only has the create feature available with the save function. We’ll implement the remaining read, update, and delete portions by creating three extra functions: get, update, and deleteContact. We’ll add one extra feature as a function: the ability to send our contacts some money. Let’s call this sendFunds.
get
Here, we want to look up a contact with a specified id. We’ve already specified our contacts state variable as public, so we can perform a lookup using the GUI button on Remix. But we’ll still create this function to do something similar.
We made this function accept an argument called id, which we’ll use for the lookup. We first check if this contact exists by seeing if the provided id is greater than 0, since the first contact will have an id of 1. Then, we check if the provided id is greater than the number of contacts. This isn’t possible, so it serves as a good check to see if any contact with that id exists. If so, we return it. ...