Lessons/Functions

In the lesson we created a predicate that took an argument and passed it on to another predicate. The data was flowing inwards. Here we will extend with data that float outwards.

Let us start by creating a function. A function is a predicate that returns a value to place where the function was called from.

Let's start with the include statement. In our new clause we are calling the format predicate in the string class. However the string class is not visible in the main.pro file. At this point we will not concern ourselves with why this is so and what needs to be done, etc. We will simply accept that the Compiler and IDE in collaboration knows how to solve the problem by inserting a suitable include statement in a suitable place. Actually, this "automation" will deal with most "including" that you ever deal with. The back side to this magic (as to all such automation magic) is that, lazy as we are, we don't really get to know the "include stuff", and the day where the magic cannot figure out what to do we don't know it either (see Creating a Package).

Now let us return to the function itself. As mentioned a function is a predicate that returns a value. In the declaration of makeGreeting there is an arrow -> between the arguments and the return type. This arrow indicated that the predicate is a function and the type after the arrow say what type the returned value has. Again the name (i.e. Greeting) after the type mainly serves documentation purposes.

A function clause has the form  = :- .. The execution flow is that we "enter" at the  , then the  </vp> is executed and then the  </vp> is evaluated/executed and returned.

In the clause above the returned value is in the variable Result</vp>, which receives it value in the second line (i.e. in the  </vp> of the clause). What happens in the second line is actually that we call another function, namely the function format</vp> in the string</vp> class.

The name Result</vp> is used just to illustrate that is does not have to be Greeting</vp> even though this is what is written in the declaration. (For a real program I would say: why use two quite unrelated words for the same thing.)

The function string::format</vp> is closely related to stdio::writef</vp>. The input is the same, but stdio::writef</vp> will write the result to the standard output stream, string::format</vp> returns the result as a string.

Let us call our new function:

It is worth noticing that equal is a rather special thing in Visual Prolog. In the function clause  = :- .</vp> it is just a token that separates the head from the result. But in the clause bodies it is an equal operator as you know it from mathematics and logic. Equal is very much unlike an assignement operator as you may know it from other programmig languages. Notice for example that equal is symetrical:

Notice that the result of calling a function is a value, you do not have to bind that value to a variable, you can use it directly.

Here we have used the result of the string::format</vp> function directly as the result of makeGreeting</vp>. As a result we have nothing for the function  </vp>, but fortunately the function <vp> </vp> is optional (if there is no <vp> </vp> then the <vp>:-</vp> token must also be left out).

In the same way we can use the returned value from <vp>makeGreeting</vp> directly in a call to <vp>stdio::write</vp>:

Summary:


 * A function declaration contains an arrow <vp>-></vp> followed by the type of the value that the function returns.
 * A function clause has the form <vp> = :- .</vp>
 * Or the form <vp> = .</vp> if there is no need for a body.
 * The equal operator is not an assignment operator as known from some other programming lanugages (it is for example symetrical).