What Are Solidity Functions And Modifiers?

 What Are Solidity Functions And Modifiers?

Introduction

Solidity is growing and given that advanced programming constructs for users to write better smart contracts. Functions are the greatest significant component of a smart contract after state variables. These are functions those benefit to create transactions and implement custom logic in Ethereum. Modifiers are distinct functions those help in writing additional freely available and modular smart contracts. Fallbacks are an idea sole to contract-based programming languages. They are performed when a function call does not match any present declared method in the contract.

Function input and output

Functions accept parameters and return values. Functions are completely generic with the use of parameters and return values. Parameters may help in altering function execution. They also provide different execution paths. Solidity permits us to receive multiple parameters within the same function.

Modifiers

Modifiers are an additional concept exclusive to Solidity. Modifiers benefit in adapting the behavior of a function. It may be understood better with the help of an example. The following code does not use modifiers. In this contract, there are defined two-state variables, two functions, and a constructor. The address of the account deploying the contract is being stored by one of the state variables. The global variable msg. the sender is used to inputting the account value in the owner state variable within the constructor. The function code is executed if the caller is the same as the account that deployed the contract. The function code ignores the rest of the code if the caller is not the same as the account that deployed the contract. In terms of readability and manageability, it may be made better while this code works as is. The modifiers can help here. Using the, if conditional statements the checks are made in this instance.

Modifier construct in Solidity

Using the modifier keyword and an identifier the modifiers are defined. Within curly brackets, the code for a modifier is placed. The code within a modifier may authenticate the incoming value. It may conditionally execute the called function after assessment.

The _ identifier is used to replace itself with the function code that is raised by the caller. The AssignDoubleValue function is decorated with the owner modifier. The modifier takes control of the execution and replaces the _ identifier with the called function code. That is AssignDoubleValue. Finally, the modifier looks like the below code during runtime, in EVM:

modifier owner {

// require(msg.sender == owner);

if(msg.sender == owner) {

mydata = _data * 2;

}

}

The view, constant, and pure functions

Solidity offers special modifiers for functions. These are view, pure, and constant. We also called them state mutability attributes. These describe the scope of changes permitted within the Ethereum global state. With the below three activities, writing smart contract functions helps primarily:

  1. Updating state variables
  2. Reading state variables
  3. Logic execution

The execution of functions and transactions is not free as it costs gas. Each

The transaction requires a listed amount of gas founded on its execution. The callers are

accountable for providing that gas for successful execution. This is factual for transactions. Also true for any activity that modifies the global state of Ethereum. Some functions are only responsible for reading and returning the state variable. These are similar stuff getters in other programming languages. They read the present value in a state variable & return values back to the caller. The functions like these do not modify the state of Ethereum. The below statements in relation to things that modify state are mentioned in Ethereum’s documentation (http:/ / solidity. readthedocs. io/ en/ v0. 4. 21/ contracts. html):

  • Text to state variables
  • Producing events
  • Making other contracts
  • By means of selfdestruct
  • Directing Ether via calls
  • Shouting any function not clear view or pure
  • By low-level calls
  • With inline assembly that contains certain opcodes

The address functions

The address delivers five functions and a single property. The only property given by address is the balance property that offers the balance available in an account. That account may be contract or individual in Wei, as shown in the undermentioned code snippet:

.balance ;

The account is a valid Ethereum address and this returns the balance available in this in terms of Wei in the preceding code.

The send method

This method is used to send Ether to a contract account or to a separately owned account. The following code portraying the send method:

.send(amount);

The send function affords 2,300 gas as a fixed limit that can’t be outdated. This is particularly significant when sending an amount to a contract address. This amount of gas is sufficient to send an amount to an independently owned account. The send function returns a boolean( true or false) as a return value.

 

Leave a Comment