Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Function state mutability can be restricted to pure function

I am new to solidity and I have been trying to print out simple messages using functions in solidity, but I have failed to deploy successfully, and there is an error that I can not figure out what's wrong.

This is what I have tried so far:

 pragma solidity ^0.6.0;
    
    contract test {
        
        string public _feedback;
        
        function reply(string memory feedback) public
        {
           feedback = "Well done!";
        }
    }

The error I am receiving is "Warning: Function state mutability can be restricted to pure function"

like image 884
Alo_159 Avatar asked Dec 30 '20 00:12

Alo_159


People also ask

What is state mutability in Solidity?

State mutability is a concept in solidity that defines the behaviour of functions and how they interact with data stored on the blockchain. In this article, you will learn about the different state mutability modifiers and how to apply them in writing optimized smart contracts.

What is a pure function in Solidity?

In Solidity, a function that doesn't read or modify the variables of the state is called a pure function. It can only use local variables that are declared in the function and the arguments that are passed to the function to compute or return a value.

What is view and pure in Solidity?

View functions are read only function and do not modify the state of the block chain (view data on the block chain). Pure functions do not read and do not modify state of the block chain.

What is pure function in Blockchain?

Pure function declares that no state variable will be changed or read.


1 Answers

The compiler simply WARNS that the result of the execution of the function reply is fixed and "canonically" this can be indicated by adding a pure specifier to it:

        function reply(string memory feedback) public pure
        {
           feedback = "Well done!";
        }

But even without this change, the contract will be compiled and created correctly. Only in its current form, you will not be able to understand in any way what it is working out :-)

like image 151
Mad Jackal Avatar answered Sep 20 '22 17:09

Mad Jackal