Lessons/Output arguments

From wiki.visual-prolog.com

Revision as of 18:43, 25 September 2018 by Thomas Linder Puls (talk | contribs) (initial)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In the Functions lesson we created a function, i.e. a predicate that retruned a value. Here we will look at output parameters, which is another way to let data flow outwards.

Exercise Add this code to your Functions project:
class predicates
    nowDateAndTime : (string Date [out], string Time [out]).
    nowDateAndTime(Date, Time) :-
        Now = time::now(),
        Date = Now:formatShortDate(),
        Time = Now:formatTime().
Build the project, and answer "Yes" to adding the ...time.ph include statement.

The first thing to notice here is the use of [out] in the predicate declaration, this "mark" indicates that the corresponding argument is an output argument rather than an input argument.

In the clause we still have an "position" correcponding to that argument, but this time we have to "calculate" and return a value. So in the clause above Date and Time does not have a value when the body starts to execute, it is our responsibility to create bind values to these variables.

In the clause body we call the function now on the class time this function returns a time object that represents the time the function was called. Here we will not go into details about objects, but the next two lines of code shows that objects have predicates associated with them and that these are referenced these using a single colon: <object>:<predicate>.

A time object represents a time and date, and in the last two lines we "reqest" the date using the short format and the time formatted as a string. What exactly these functions does depends on the users settings on the computer (i.e. the users locale settings).

Exercise Update the run clause like this:
    run() :-
        nowDateAndTime(NowDate, NowTime),
        stdio::writef("The time is now % and it is the %.\n", NowTime, NowDate).
Run the project.

Sometimes you will accidentally use a single colon where you intended to use two and vice versa:

Exercise Try using a single colon instead of two and build the project and examine the error message. Revert the problem and try the the opposite error. End without errors.

Output arguments can also be used in functions, and together with input arguments.

Exercise Update nowDateAndTime to be a function that returns the date as an output argument and the time as the function return value.

Notice the error message you get if you don't update the call in the run clause.

Also update the run clause and run the program.


  • Output arguments are declared using the [out] attribute.
  • An output argument is unbound when the clause <body> is entered and must be bound when the predicate returns.
  • There is something called objects.
  • Objects are values that can be passed around and held in variables.
  • Objects have predicates associated and these are referenced with single colon: <object>:<predicate> (predicates in classes are referenced using a double-colon).