Jump to content

Turing/Procedures, functions, and processes

From Wikibooks, open books for an open world

Both computer programmers and mathematicians have quickly learned that with complicated problems, it can become near impossible to try and solve everything at the same time. Instead, we try to reduce the problem that we know how to solve. This can be done easily and efficiently with procedures and functions.

First, let's deal with procedures. When you call a procedure inside a program, it jumps to that procedures, executes the code, and goes back.

Sample procedure code coming soon


You can think of functions like variables that calculate things. They return a value when you call them, so they can be used just like variables. This code should help you understand:

Here is a program that will print a string backwards.

function flipWord (w : string) : string
    var tmp : string := ""

    for decreasing i : length (w) .. 1
        tmp := tmp + w (i)
    end for
    result tmp
end flipWord

%%%%%%%%%%MAIN PROGRAM%%%%%%%%%%
var word : string

put "Enter word"
get word : *   %bypasses token-oriented input

put flipWord (word)

There are actually a couple new things. First, the more simple, is the word decreasing in the for-loop. This tells Turing to decrease by one. As you can see, functions allow us to keep our main program nice and neat. It will become imperative to use functions in larger programs, as they can get very complicated!

If you were wondering what 'token-oriented input' meant, it's just telling Turing NOT to consider space to mean that the next variable is being put in. This allows spaces in the word variable.


Processes are like procedures, except they can run concurrently with other parts of your code. This means that, if you're using a loop, your process won't stop you from doing whatever else you need. This is especially useful in cases like playing music, where you need other code to be able to run while the music is playing.

Processes are used similarly to procedures; however, one must replace the word procedure with process. Additionally, processes cannot be started by simply calling them. You must prefix the process name with the command fork.


Data files · Arrays, flexible arrays