# API function with return type 'string'

**URL:** <https://forum.legato.io/t/api-function-with-return-type-string/4790>\
**Category:** Legato Application Framework\
**Created:** [October 31, 2019, 4:49pm UTC](https://forum.legato.io/t/api-function-with-return-type-string/4790 "2019-10-31T16:49:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidc](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@davidc](https://forum.legato.io/u/davidc)\
**Post date:** [October 31, 2019, 4:49pm UTC](https://forum.legato.io/t/api-function-with-return-type-string/4790/1 "2019-10-31T16:49:39Z")

</div>

Hiya,

TL;DR: Is it possible to define an API function with the return type ‘string’?

* * *

I would like to build an API function to return the text representation of an enum value (similar to what LE\_RESULT\_TXT() uses).

I have defined an api function like so:

```auto
FUNCTION string ModeToString
(
    int32 pMode IN
);

```

where pMode is my input value to translate.

I’ve implemented this in my C file like this:

```auto
const char *myapi_ModeToString ( int32_t pMode )
{
    switch (pMode)
    {
        case 0: return "MODE 0";
        case 1: return "MODE 1";
        default: return "(unknown)";
    }
}

```

but the build tools are not happy and throw this error when parsing the .api file:

```auto
In file included from /home/developer/legato-18.10.3-octave/framework/include/legato.h:200:0,
                 from /home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.h:17,
                 from /home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.c:12:
/home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.c: In function ‘Handle_myapi_ModeToString’:
/home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.c:356:15: error: too few arguments to function ‘le_pack_PackString’
/home/developer/legato-18.10.3-octave/framework/include/le_log.h:561:15: note: in definition of macro ‘LE_ASSERT’
In file included from /home/developer/legato-18.10.3-octave/framework/include/legato.h:215:0,
                 from /home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.h:17,
                 from /home/developer/dev-18.10.3-octave/project/mbService/_BUILD_motherboard.wp77xx/api/01815889a894043e810253f2f92771e5/server/myapi_server.c:12:
/home/developer/legato-18.10.3-octave/framework/include/le_pack.h:358:20: note: declared here
[7/84] Generating IPC interface code
ninja: build stopped: subcommand failed.
Makefile:84: recipe for target 'build' failed
make: *** [build] Error 1

```

Any suggestions gratefully accepted.

ciao, Dave

---

<div class="post-metadata">

**Author:** ![raf](https://avatars.discourse-cdn.com/v4/letter/r/848f3c/32.png) [@raf](https://forum.legato.io/u/raf)\
**Post date:** [November 1, 2019, 1:01am UTC](https://forum.legato.io/t/api-function-with-return-type-string/4790/2 "2019-11-01T01:01:50Z")

</div>

Hi @davidc,

You can return a string from an API function, however, only as an `OUT` parameter to the function and not as a return type.

From here: [Syntax - Legato Docs](https://docs.legato.io/latest/apiFilesSyntax.html#apiFilesSyntax_function)

> The `returnType` is optional, and if specified, can be any type that’s not an array, string, or handler.

Maybe try something like this?

**.api** file

```auto
DEFINE MAX_STR_LEN = 32;

FUNCTION ModeToString
(
    int32 mode IN,
    string modeStr[MAX_STR_LEN] OUT
);

```

**.c** file

```auto
void myapi_ModeToString(int32_t mode, char* modeStr, size_t modeStrNumElements)
{
    // evaluate 'mode'
    // write string to 'modeStr'
}

```

Raf

---

<div class="post-metadata">

**Author:** ![davidc](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@davidc](https://forum.legato.io/u/davidc)\
**Post date:** [November 1, 2019, 9:06am UTC](https://forum.legato.io/t/api-function-with-return-type-string/4790/3 "2019-11-01T09:06:47Z")

</div>

Hi @raf

Ta. I knew how to do that … was wanting to know if I could have a `string` as the function return type, not as a function parameter.

ciao, Dave

---

<div class="post-metadata">

**Author:** ![plu](https://avatars.discourse-cdn.com/v4/letter/p/e19b73/32.png) [@plu](https://forum.legato.io/u/plu)\
**Post date:** [December 19, 2019, 8:20am UTC](https://forum.legato.io/t/api-function-with-return-type-string/4790/4 "2019-12-19T08:20:52Z")

</div>

hi @davidc, your request is currently not possible, and the solution is given into the post from raf,  
best regards,
