Skip to content

Functions and commands

tim-hardcastle edited this page Jul 30, 2024 · 6 revisions

Let's look again at our first example script.

cmd

greet :
    get name from Input("What's your name? ")
    post "Hello " + name + "!" to Output()

def

factorial (n) :
    n == 0 : 
        1
    n > 0 : 
        n * factorial n - 1
    else : 
        error "can't take the factorial of a negative number"

It has a command, introduced by the heading cmd, and a function, introduced by the heading def. These are two very different things.

A command does things: it moves data in and out of the service: in this case getting data from the keyboard and posting it to the terminal. You would also use commands to interact with the file system, the database, the system clock, etc. Commands don't return values: they succeed or fail.

A function computes things. It returns a result dependent on the arguments it was called on, and this is all. It has no effect on the outside world, and indeed doesn't know that there is an outside world.

Commands can call functions: functions can't call commands.

This distinctions are fundamental to Pipefish.

Clone this wiki locally