0
0
Blockchain / Solidityprogramming~10 mins

View and pure functions in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a view function that returns a stored number.

Blockchain / Solidity
function getNumber() public [1] returns (uint) {
    return storedNumber;
}
Drag options to blanks, or click blank then click option'
Aexternal
Bpure
Cpayable
Dview
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pure' instead of 'view' when reading state variables.
Forgetting to specify 'view' or 'pure' for functions that don't modify state.
2fill in blank
medium

Complete the code to declare a pure function that returns the sum of two numbers.

Blockchain / Solidity
function add(uint a, uint b) public [1] returns (uint) {
    return a + b;
}
Drag options to blanks, or click blank then click option'
Apayable
Bpure
Cview
Dexternal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view' instead of 'pure' when the function does not read state.
Omitting the keyword for functions that do not access state.
3fill in blank
hard

Fix the error in the function declaration to make it a view function.

Blockchain / Solidity
function getBalance() public [1] returns (uint) {
    return balance;
}
Drag options to blanks, or click blank then click option'
Aprivate
Bpayable
Cview
Dpure
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pure' which disallows reading state variables.
Using 'payable' which is unrelated to reading state.
4fill in blank
hard

Fill both blanks to create a pure function that returns the product of two numbers.

Blockchain / Solidity
function multiply(uint x, uint y) public [1] returns (uint) {
    return x [2] y;
}
Drag options to blanks, or click blank then click option'
Apure
Bview
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view' instead of 'pure' for functions that do not access state.
Using '+' instead of '*' for multiplication.
5fill in blank
hard

Fill all three blanks to create a view function that returns the length of a stored string.

Blockchain / Solidity
function getStringLength() public [1] returns (uint) {
    return [2].[3];
}
Drag options to blanks, or click blank then click option'
Aview
BstoredString
Clength
Dpure
Attempts:
3 left
💡 Hint
Common Mistakes
Marking the function as 'pure' when it reads state.
Using incorrect variable name or property for length.