return from function early - Kdb+ / qStudio

Home Forums Kdb+ / qStudio return from function early

Tagged: 

This topic contains 1 reply, has 2 voices, and was last updated by  admin 6 years, 5 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #74

    Simon

    Does q have the ability to return from a function early? Like “break” in C++ ?

    #130

    admin
    Keymaster

    kdb supports the full range of execution controls, while, if, do, exceptions.

    Consider the following while loop:

    q){ i:0; while[i<5; show "hello ",string i; i+:1] }[]
    "hello 0"
    "hello 1"
    "hello 2"
    "hello 3"
    "hello 4"

    If we wanted to break from it early dependent on a condition we could do:

    q){ i:0; while[i<5; show "hello ",string i; if[i~3; :`p]; i+:1] }[]
    "hello 0"
    "hello 1"
    "hello 2"
    "hello 3"
    `p

    The single colon at :`p with nothing on the left is the equivalent of return in most other languages.

    Some more details on debugging and exceptions can be found at:
    http://www.timestored.com/kdb-guides/debugging-kdb

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.