Visibility and State Mutability

Visibility

As for variables, function have a visibility. They can be public, private and more 😃

Public

When public, functions can be accessed from anywhere, either outside of the contract or internally. It is the most permissive visibility parameter.

Private

A private function will not be accessible from outside of the contract. It is the most restrictive permission you can set.

External

external functions are exposed to the outside world and to the outside world only. You won't be able to use them within your contract. They can be used by other to communicate with your contract.

Internal

If internal, a function is not exposed outside of the contract. It sounds like private, but its not and will see it later on (it has to do with inheritance 😉)

State Mutability

All functions have not been created equal. Some are doing core feature while others are performing simple operations. And noone wants dummy functions to interact with advanced logic. Therefore, Solidity provides you with state mutability keywords.

Difference with visibility

This keyword is linked with how the function interact with the contract (with its variables). That's why it's different from the visibility keyword which define who can interact with the function. They are complementary keywords and can be used with one another.

View

Exercise
Correct!
False!

Loading...

SpaceMuffin is back! It wants to know its exact number of fans
pragma solidity ^0.4.24; contract SpaceMuffin { uint public fans = 0; // This function accesses variables and modifies them // It needs to have some rights function like() public { fans = fans + 1; } // This function accesses variables but does NOT modify them // That's what's called a `view` function function getFans() public /* insert */ returns (uint) { return fans; } }

view function can wrap complex operations, or just provide a way to expose internal component in a safe way.

Pure

Exercise
Correct!
False!

Loading...

Better say it than not, it is the BEST muffin ever created
pragma solidity ^0.4.24; contract SpaceMuffin { // This function NEITHER accesses variables NOR modifies them // It is `pure` function isBestMuffin() external /* insert */ returns (bool) { return true; } }

Spotting a pure function is easy. Just look at its code. If it doesn't use any internal variables of the contract, either for a read or a write, then it's pure.

Default

By default, a function can access and modify all contract's components. like function for example has a default state mutability, because it accesses and modifies fans.

function like() public /* nothing here */;

Practice

You now know everything about state mutability. But your journey in the decentralized world has just begun.

Exercise
Correct!
False!

Loading...

Complete the following contract with the right state mutability attribute
pragma solidity ^0.4.24; contract SpaceMuffin { uint private fans = 0; // No help this time // You should guess how it works function lesserThan(uint a, uint b) public /* insert */ returns (bool) { return a <= b; } // Hope it will have function hasLotsOfFans() public /* insert */ returns (bool) { return lesserThan(100, fans); } }

Amiral tip

On this website, the use of mutability attribute is enforced. If you forget them, the system won't validate your answer. Now you know

results matching ""

    No results matching ""