Complete the code to declare a function parameter stored in memory.
function greet(string [1] name) public pure returns (string memory) { return name; }
Function parameters that are strings or arrays usually use memory to store temporary data during function execution.
Complete the code to declare a function parameter that is read-only and cheaper to use.
function readData(string [1] data) external view returns (string memory) { return data; }
calldata is used for function parameters that are read-only and passed from external calls, saving gas.
Fix the error in the code by choosing the correct data location for the state variable.
contract Example {
string [1] name;
function setName(string memory newName) public {
name = newName;
}
}State variables must be stored in storage because they persist on the blockchain.
Complete the code to create a function that accepts a read-only array and stores it in state.
contract DataStore {
uint[] public data;
function storeData(uint[] [1] input) public {
data = input;
}
}The function parameter uses calldata for a read-only array. The assignment copies the array directly, so no extra method is needed.
Fill both blanks to declare a function that modifies a storage array using a memory copy.
contract Modify {
uint[] public numbers;
function updateNumbers(uint[] [1] newNumbers) public {
uint[] storage nums = numbers;
nums = newNumbers;
nums.push([2]);
}
}The function parameter uses memory to hold the new array. The assignment copies the array. Then, 100 is pushed to the storage array.