For Loop To Generate 1-100 #3303
-
i'm writing an a for loop function to generate numbers from 1- 100 and i'm getting errors. This is my code : function returnNumers () public pure returns (uint[] memory){
I know the variable "numbers" is not declared here but i have tried declaring it but it's not working, kindly provide a solution if you can. And make sure you run the code before pasting here too to ensure its working. Thank You |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hello @uthmanabayomi, What error are you getting? |
Beta Was this translation helpful? Give feedback.
-
@uthmanabayomi would be helpful if you share the entire code as it's a bit difficult to understand what you're trying to do. I can see you're using the pure modifier (you can't access or modify any state variables) so I guess you want to do everything in memory. Because you can't initialize dynamic arrays in memory you will have to define how much memory your variable will take. // SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Test {
function returnNumbers() public pure returns (uint[100] memory) {
uint[100] memory numbers; // initialize the array with fixed size
for (uint i = 0; i < 100; i++) {
numbers[i] = i + 1;
}
return numbers;
}
} |
Beta Was this translation helpful? Give feedback.
@uthmanabayomi would be helpful if you share the entire code as it's a bit difficult to understand what you're trying to do. I can see you're using the pure modifier (you can't access or modify any state variables) so I guess you want to do everything in memory. Because you can't initialize dynamic arrays in memory you will have to define how much memory your variable will take.