On-chain example:

pragma solidity 0.4.24;

contract MyContract {
    uint[] public array;

    constructor() public {

    function getArray() public view returns(uint[]) {
        return array;

Off-chain example:

contract("MyContract", function(accounts) {
    it("Test", async function() {
        myContract = await artifacts.require("MyContract.sol").new();
        let array = await myContract.getArray();
        let item0 = await myContract.array(0);

As you can see, the getArray function indeed returns the array.

But when I "call" the array, I need to pass an index (in other words, the array "returns" an item).

Attempting to execute:

let array = await myContract.array();

Results with:

Invalid number of arguments to Solidity function

Why has the Solidity standard defined it this way?

Thank you!

  • Surprising how hard it is to find a good answer to this question. The only thing I've found is that gas considerations played into the design. Maybe an example of (over)protecting the user... – Chan-Ho Suh Sep 2 '19 at 17:57

Solidity creates getter functions automatically for public variables. As your array is public, a public getter function a generated for it automatically. The getter function is used to access the array variables directly and not for retrieving the array itself.

For array types, the getter function requires an integer parameter to denote the index to access. Read more at https://solidity.readthedocs.io/en/v0.4.24/types.html#arrays

So your JavaScript code is simply accessing its public getter function. But, as you noticed, you can also get the original array if you have a custom getter function.

| improve this answer | |
  • 2
    Thanks. You basically described everything that I already know (and have already described myself as part of the question), while leaving the actual question unanswered - why has the language standard decided that getter functions for arrays are to return an element in the array rather than the entire array itself (as one would normally expect from a getter function)? – goodvibration Oct 12 '18 at 20:56
  • 1
    Ah, sorry, misread that part and misinterpreted your question's topic. – Lauri Peltonen Oct 13 '18 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.