Functions in Solidity

Search for a command to run...

No comments yet. Be the first to comment.
When it comes to online security, two terms you’ve probably heard a lot are authentication and authorization. These concepts often go hand in hand, but they’re not the same thing. Understanding the difference between them is important—not just for te...
You don't have to be a professional computer expert to access the dark web. The dark web covers around 6% of the total internet. While the deep web covers around 90% and only 4-5% is the surface web that we are using till now. You just need to have t...

Do you want to get your business on the internet? Your website must rank on Google and other search engines, for increasing your business. Speed of the website is very important for SEO because the faster speed of the website increases user experienc...

GitHub is the best way to host static assets. But most people don't know this so they tend to buy a hosting service that is not cheap. From now on you are gonna host all your static websites on Github pages. Let's understand everything about GitHub p...

You just started learning solidity and the functions look a bit overwhelming in solidity? Don't worry I'll explain everything about the functions, how to use them, visibility, and mutability in this post.
If you are here then I assume you already have learned any of the high-level programming languages such as C++, Java, JavaScript or any one of the object-oriented programming languages.
Functions work the same way in all other languages but are a bit different in syntax. In solidity, functions have some additional keywords which we will understand more in this post.
Syntax of function:
function function_name(parameters) visibility mutability returns(return_type) { ... }
Functions in solidity start with the function keyword, but some functions don't start with the function keyword such as receive() and fallback() functions.
functions in solidity accept a list of parameters, which can also be empty.
function sum() { // no parameter
uint256 a = stateVar1 + stateVar2;
}
function sum(uint256 _a, uint256 _b) { // list of parameters
stateVariableC = _a + _b;
}
Functions in solidity can return any value which has to be defined in the function itself, which can be uint, int, string, bool or bytes.
function sum() view returns(uint256) {
return (a + b);
}
Named returns In this, the return type with the name of the returning variable is defined in the function. (No need to return keyword)
function sum (uint256 _num1, uint256 _num2)
public
pure
returns (uint256 _num3) {
_num3 = _num1 + _num2;
}
Unnamed returns In this return type, the return keyword is used and the values can be returned using the returns keyword.
function sum (uint256 _num1, uint256 _num2)
public
pure
returns (uint256) {
return (_num1 + _num2);
}
Visibility of the functions is defined as who and from where a function can be called, there are four types of visibility for functions, public, external, internal and private. State variables cannot be declared as external, but can be declared as public, internal or private.
public: Public function can be called from any contract and account.
external: External functions can only be called from outside the smart contract.
internal: Internal functions can be called inside the contract that defines it and all other smart contracts that inherit from it.
private: Private functions can only be called within the contract that defines the function.
public functions are the most permissive functions whereas private functions are the most restrictive ones.
view: Functions that are declared with the view keyword can only read the state but cannot modify any state variables.
contract ViewKeyword {
uint256 public num;
function square() view returns (uint256) {
return (num * num); // reading from state but not changing it
}
}
pure: Functions that are declared with pure keyword neither can read state variables nor can read variables from the blockchain.
contract PureKeyword {
function square(uint256 _num) pure returns (uint256) {
return (_num * _num); // neither reading state nor modifying
}
}
Functions declared payable can receive ether into the contract.
contract Payable {
function deposit() public payable { }
// call the deposit() function along with some ether
// it will automatically update the balance of the contract
}
Modifiers are code that can run before and/or after a function call. Modifiers can be used to restrict access, validate inputs and guard against reentrancy attacks.
contract Modifiers {
address public owner;
constructor {
owner = msg.sender;
}
modifier onlyOwner () {
require(msg.sender == owner, "Not Owner");
_;
}
function changeOwner (address _newOwner) public onlyOwner {
// onlyOwner here works as a middleware, the code inside
// changeOwner function will only execute when the code inside
// onlyOwner modifier is executed successfully
owner = _newOwner;
}
}
That was pretty much all the basics of how to use functions in solidity.
I hope this article was a good read for you. Do share it with your friends and other peers.
Thank you!!