1

Is it possible to return multiple values inside a for cycle? I mean, without knowing the number of output (it will be dynamically increased every time an array will be filled), is it possible to represent every element of that array as output of a function?

Something like this

function getList() view public returns (string, address) {

    for (uint i = 0; i < arrayOfStringValues.length; i++){

        return (arrayOfStringValues[i], arrayOfAddressValues[i]);}
}

I tried to do this, but the function returns only the first "couple".

2 Answers 2

2

No.

A function returns a result once. It cannot loop over all your array, and returns a tuple at each iteration.

You can return a dynamic sized array to an external function call ( contract exterior : e.g web3 / other contract ).

In theory it can work like this :

function getList() external returns (string[], address[]) {

        return (arrayOfStringValues[], arrayOfAddressValues[]);
}

But you would have an error as you can't return string[] yet in solidity. so if you can find a way to use bytes32 instead of string, it should work.

Note : If you need to call this external function from inside your contract you can use this.getList() as it will make it look as an external call.

EDIT : i just saw that the latest solidity release 0.4.22 opens the possibility to return string[] and bytes32[] in functions without the external modifier. ( someone verify this please )

4

No.

A function returns a result once. It cannot loop over all your array, and returns a tuple at each iteration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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