In solidity it's obvious how to get a single string returned from a function. But, I'd like to have the smart contract return multiple string values. Is there a way to do this?

2 Answers 2


There are a few types of string values that you may be referring to.

  • bytes1 - bytes32: fixed size.
  • bytes or string: dynamically sized.

Solidity supports functions with multiple return values.

contract MultiReturner {
    function getData() constant returns (bytes32, bytes32) {
        bytes32 a = "abcd";
        bytes32 b = "wxyz";
        return (a, b);

    function getDynamicData() constant returns (bytes, bytes) {
        bytes a;
        bytes b;
        return (a, b);

You can do the same with bytes or string but with the limitation that solidity does not support returning dynamically sized values to other functions. This means that you would be able to use the getData function from within another contract and retrieve the return values but you would not be able to retrieve the return values from getDynamicData from within a contract.

contract UsesMultiReturner {
    function doIt() {
        mr = MultiReturner(0x1234);

        // this will work
        var (a, b) = mr.getData();

        // this won't work
        var (a, b) = mr.getDynamicData();

You can however retrieve the return values from both getData and getDynamicData when call from outside of the blockchain.

  • since returned type is bytes32, should we typecast var into bytes32?@Piper Merriam.
    – alper
    Commented Jan 11, 2017 at 15:29
  • 4
    In solidity var can be used to delegate selecting the appropriate type to the compiler. a and b will be set as bytes32 by the compiler since the compiler knows that mr.getData() will return type (bytes32, bytes32) Commented Jan 11, 2017 at 15:35
  • var is now deprecated. Is there an update on how to return multiple values using 0.4.21?
    – JohnAllen
    Commented Mar 23, 2018 at 17:14
  • Solidity 0.4.23, but I'm not sure if it actually does the same thing. Commented May 23, 2018 at 23:59

You can use Seriality library. Seriality is a library for serializing and de-serializing all the Solidity types in a very efficient way which mostly written in solidity-assembly

1- By means of Seriality you can easily serialize and deserialize your variables, structs, arrays, tuples, ... and pass them through the contracts and libraries.

2- You can decouple your contract from libraries by serializing parameters into a byte array.

3- It also can be used as an alternative for RLP protocol in Solidity.

Here is a sample :

pragma solidity ^0.4.16;

import "./Seriality.sol";

contract StringsReturn is Seriality {

function stringCaller() public returns( string memory out1,
                                        string memory out2,
                                        string memory out3,
                                        string memory out4,
                                        string memory out5)

    bytes memory buffer = new bytes(320);
    uint offset = stringCallee(buffer);

    out1 = new string (getStringSize(offset, buffer));
    bytesToString(offset, buffer, bytes(out1));
    offset -= sizeOfString(out1);

    out2 = new string (getStringSize(offset, buffer));
    bytesToString(offset, buffer, bytes(out2));
    offset -= sizeOfString(out2);

    out3 = new string (getStringSize(offset, buffer));
    bytesToString(offset, buffer, bytes(out3));
    offset -= sizeOfString(out3);

    out4 = new string (getStringSize(offset, buffer));
    bytesToString(offset, buffer, bytes(out4));
    offset -= sizeOfString(out4);

    out5 = new string (getStringSize(offset, buffer));
    bytesToString(offset, buffer, bytes(out5));


function stringCallee(bytes memory buffer) public returns (uint buffer_size) {

    string memory out1  = new string(32); 
    string memory out2  = new string(32);        
    string memory out3  = new string(32);
    string memory out4  = new string(32);        
    string memory out5  = new string(32);

    out1 = "Come on baby lets dance!";
    out2 = "May I buy you a drink?";
    out3 = "I am an itinerant programmer";
    out4 = "Inam javab lashi!";
    out5 = "Bia inja dahan service";

    // Serializing
    buffer_size =  sizeOfString(out5) +
                   sizeOfString(out4) + 
                   sizeOfString(out3) + 
                   sizeOfString(out2) +

    uint offset = buffer_size;

    stringToBytes(offset, bytes(out1), buffer);
    offset -= sizeOfString(out1); 

    stringToBytes(offset, bytes(out2), buffer);
    offset -= sizeOfString(out2);

    stringToBytes(offset, bytes(out3), buffer);
    offset -= sizeOfString(out3); 

    stringToBytes(offset, bytes(out4), buffer);
    offset -= sizeOfString(out4); 

    stringToBytes(offset, bytes(out5), buffer);

    return buffer_size;

This is the output of stringCaller()

"0": "string: out1 Come on baby lets dance!",
"1": "string: out2 May I buy you a drink?",
"2": "string: out3 I am an itinerant programmer",
"3": "string: out4 Inam javab lashi!",
"4": "string: out5 Bia inja dahan service"

And here is the buffer :


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.