I tried to declare string inside my function but it doesn't work.

How to declare string inside function?

enter image description here

2 Answers 2


I suspect it has to do with some already answered question like Why do Solidity examples use bytes32 type instead of string?

However, a code like the one below seems to work around without declaring a string.

contract A{
        function f() constant returns(string){
            var s="hello";
            return s;


looms like compiler is not getting the righ context for the string declaration.

As such below default rules applies to decide on the storage option based on context: - default storage for member variable is 'storage' - default for function parameters and return types is 'memory'

In EVM context, string is an array which has an extra annotation -'data location'. Thsi could be either 'memory' or 'storage'.

Seems in the below programm, it exolicitly looking to define the storage option.

As you can see below, I specified the stogare option as 'memory' and its compiling fine.

pragma solidity ^0.4.4;

contract Test {

    function print() constant returns(string){
        string memory x = "ddd";
        return x;

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.