<100 subscribers
Share Dialog
Share Dialog


Here we have a simple contract, increment and decrement the count store in this contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Counter {
uint public count;
// Function to get the current count
function get() public view returns (uint) {
return count;
}
// Function to increment count by 1
function inc() public {
count += 1;
}
// Function to decrement count by 1
function dec() public {
// This function will fail if count = 0
count -= 1;
}
}
There are some points we saw in the previous post:
https://mirror.xyz/cryptovincent.eth/hNRrl90DpsXUn38xbaPptTaFHA8NZHVufJ_Ho1pbS-c
Then, here are a public state variable “count” and three functions.
The first function, get(), is public and only returns the value of variable “count”, then it’s declared as “view” (because it doesn’t change the blockchain state), and has the “returns ()” saying with that “I will return a value of type “. The last two functions are similar in structure but with different logic: In structure, only has public visibility and different names. In logic, inc() function increments variable counter applying the += operator that is inherited from other languages, “count += 1” it’s the same as “count = count + 1“. dec() function decrements variable counter, similar to its sister function but applying the -= operator that is inherited from other languages, “count -= 1” it’s the same as “count = count - 1“. Also this function will fail if the state variable value is 0, because it is declared as uint (unsigned integer) meaning this type of variable can’t have a negative value. This last functions change the variable state, it has gas costs in order to execute them.
Here we have a simple contract, increment and decrement the count store in this contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Counter {
uint public count;
// Function to get the current count
function get() public view returns (uint) {
return count;
}
// Function to increment count by 1
function inc() public {
count += 1;
}
// Function to decrement count by 1
function dec() public {
// This function will fail if count = 0
count -= 1;
}
}
There are some points we saw in the previous post:
https://mirror.xyz/cryptovincent.eth/hNRrl90DpsXUn38xbaPptTaFHA8NZHVufJ_Ho1pbS-c
Then, here are a public state variable “count” and three functions.
The first function, get(), is public and only returns the value of variable “count”, then it’s declared as “view” (because it doesn’t change the blockchain state), and has the “returns ()” saying with that “I will return a value of type “. The last two functions are similar in structure but with different logic: In structure, only has public visibility and different names. In logic, inc() function increments variable counter applying the += operator that is inherited from other languages, “count += 1” it’s the same as “count = count + 1“. dec() function decrements variable counter, similar to its sister function but applying the -= operator that is inherited from other languages, “count -= 1” it’s the same as “count = count - 1“. Also this function will fail if the state variable value is 0, because it is declared as uint (unsigned integer) meaning this type of variable can’t have a negative value. This last functions change the variable state, it has gas costs in order to execute them.
No comments yet