Difference between revisions of "Lessons/Output arguments"
m |
m |
||
Line 17: | Line 17: | ||
The first thing to notice here is the use of <vp>[out]</vp> in the predicate declaration, this "mark" indicates that the corresponding argument is an output argument rather than an input argument. | The first thing to notice here is the use of <vp>[out]</vp> 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 a "position" | In the clause we still have a "position" corresponding to that argument, but this time we have to "calculate" and return a value. So in the clause above <vp>Date</vp> and <vp>Time</vp> 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 <vp>now</vp> on the class <vp>time</vp> 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: <vp><object>:<predicate></vp>. | In the clause body we call the function <vp>now</vp> on the class <vp>time</vp> 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: <vp><object>:<predicate></vp>. |
Revision as of 10:30, 5 February 2020
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.
class predicates nowDateAndTime : (string Date [out], string Time [out]). clauses nowDateAndTime(Date, Time) :- Now = time::now(), Date = Now:formatShortDate(), Time = Now:formatTime().
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 a "position" corresponding 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).
clauses run() :- nowDateAndTime(NowDate, NowTime), stdio::writef("The time is now % and it is the %.\n", NowTime, NowDate).
Sometimes you will accidentally use a single colon where you intended to use two and vice versa:
Output arguments can also be used in functions, and together with input arguments.
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.Summary:
- 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).