Solidity Tutorial
Selected Reading
- Solidity - Discussion
- Solidity - Useful Resources
- Solidity - Quick Guide
- Solidity - Error Handling
- Solidity - Events
- Solidity - Assembly
- Solidity - Libraries
- Solidity - Interfaces
- Solidity - Abstract Contracts
- Solidity - Constructors
- Solidity - Inheritance
- Solidity - Contracts
- Solidity - Restricted Access
- Solidity - Withdrawal Pattern
- Cryptographic Functions
- Mathematical Functions
- Function Overloading
- Solidity - Fallback Function
- Solidity - Pure Functions
- Solidity - View Functions
- Solidity - Function Modifiers
- Solidity - Functions
- Solidity - Style Guide
- Solidity - Special Variables
- Solidity - Ether Units
- Solidity - Conversions
- Solidity - Mappings
- Solidity - Structs
- Solidity - Enums
- Solidity - Arrays
- Solidity - Strings
- Solidity - Decision Making
- Solidity - Loops
- Solidity - Operators
- Solidity - Variable Scope
- Solidity - Variables
- Solidity - Types
- Solidity - Comments
- Solidity - First Application
- Solidity - Basic Syntax
- Solidity - Environment Setup
- Solidity - Overview
- Solidity - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Solidity - Pure Functions
Sopdity - Pure Functions
Pure functions ensure that they not read or modify the state. A function can be declared as pure. The following statements if present in the function are considered reading the state and compiler will throw warning in such cases.
Reading state variables.
Accessing address(this).balance or <address>.balance.
Accessing any of the special variable of block, tx, msg (msg.sig and msg.data can be read).
Calpng any function not marked pure.
Using inpne assembly that contains certain opcodes.
Pure functions can use the revert() and require() functions to revert potential state changes if an error occurs.
See the example below using a view function.
Example
pragma sopdity ^0.5.0; contract Test { function getResult() pubpc pure returns(uint product, uint sum){ uint a = 1; uint b = 2; product = a * b; sum = a + b; } }
Run the above program using steps provided in
chapter.Output
0: uint256: product 2 1: uint256: sum 3Advertisements