<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.visual-prolog.com/index.php?action=history&amp;feed=atom&amp;title=Lessons%2FFunctions</id>
	<title>Lessons/Functions - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.visual-prolog.com/index.php?action=history&amp;feed=atom&amp;title=Lessons%2FFunctions"/>
	<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;action=history"/>
	<updated>2026-04-29T19:15:54Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4708&amp;oldid=prev</id>
		<title>Jonas Sonn Jørgensen at 15:23, 4 February 2020</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4708&amp;oldid=prev"/>
		<updated>2020-02-04T15:23:42Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 17:23, 4 February 2020&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l23&quot;&gt;Line 23:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 23:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;In the clause above the returned value is in the variable &amp;lt;vp&amp;gt;Result&amp;lt;/vp&amp;gt;, which receives it value in the second line (i.e. in the &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; of the clause).  What happens in the second line is actually that we call another function, namely the function &amp;lt;vp&amp;gt;format&amp;lt;/vp&amp;gt; in the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;In the clause above the returned value is in the variable &amp;lt;vp&amp;gt;Result&amp;lt;/vp&amp;gt;, which receives it value in the second line (i.e. in the &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; of the clause).  What happens in the second line is actually that we call another function, namely the function &amp;lt;vp&amp;gt;format&amp;lt;/vp&amp;gt; in the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The name &amp;lt;vp&amp;gt;Result&amp;lt;/vp&amp;gt; is used just to illustrate that is does not have to be &amp;lt;vp&amp;gt;Greeting&amp;lt;/vp&amp;gt; even &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;thoug &lt;/del&gt;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.)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The name &amp;lt;vp&amp;gt;Result&amp;lt;/vp&amp;gt; is used just to illustrate that is does not have to be &amp;lt;vp&amp;gt;Greeting&amp;lt;/vp&amp;gt; even &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;though &lt;/ins&gt;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.)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The function &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; is closely related to &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt;.  The input is the same, but &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt; will write the result to the standard output stream, &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; returns the result as a string.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The function &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; is closely related to &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt;.  The input is the same, but &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt; will write the result to the standard output stream, &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; returns the result as a string.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4707&amp;oldid=prev</id>
		<title>Jonas Sonn Jørgensen at 15:16, 4 February 2020</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4707&amp;oldid=prev"/>
		<updated>2020-02-04T15:16:29Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 17:16, 4 February 2020&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l15&quot;&gt;Line 15:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 15:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Build the project, and answer &amp;quot;Yes&amp;quot; to adding the &amp;#039;&amp;#039;&amp;#039;...string.ph&amp;#039;&amp;#039;&amp;#039; include statement.}}&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Build the project, and answer &amp;quot;Yes&amp;quot; to adding the &amp;#039;&amp;#039;&amp;#039;...string.ph&amp;#039;&amp;#039;&amp;#039; include statement.}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Let&amp;#039;s start with the include statement.  In our new clause we are calling the &amp;lt;vp&amp;gt;format&amp;lt;/vp&amp;gt; predicate in the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class. However the &amp;lt;vp&amp;gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;sting&lt;/del&gt;&amp;lt;/vp&amp;gt; class is not visible in the &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; 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 &amp;quot;automation&amp;quot; will deal with most &amp;quot;including&amp;quot; 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&amp;#039;t really get to know the &amp;quot;include stuff&amp;quot;, and the day where the magic cannot figure out what to do we don&amp;#039;t know it either (see [[Ide/Creating new Project Items#Creating a Package|Creating a Package]]).&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Let&amp;#039;s start with the include statement.  In our new clause we are calling the &amp;lt;vp&amp;gt;format&amp;lt;/vp&amp;gt; predicate in the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class. However the &amp;lt;vp&amp;gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;string&lt;/ins&gt;&amp;lt;/vp&amp;gt; class is not visible in the &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; 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 &amp;quot;automation&amp;quot; will deal with most &amp;quot;including&amp;quot; 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&amp;#039;t really get to know the &amp;quot;include stuff&amp;quot;, and the day where the magic cannot figure out what to do we don&amp;#039;t know it either (see [[Ide/Creating new Project Items#Creating a Package|Creating a Package]]).&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Now let us return to the function itself.  As mentioned a function is a predicate that returns a value.  In the declaration of &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; there is an arrow &amp;lt;vp&amp;gt;-&amp;gt;&amp;lt;/vp&amp;gt; between the arguments and the &amp;#039;&amp;#039;return type&amp;#039;&amp;#039;.  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. &amp;lt;vp&amp;gt;Greeting&amp;lt;/vp&amp;gt;) after the type mainly serves documentation purposes.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Now let us return to the function itself.  As mentioned a function is a predicate that returns a value.  In the declaration of &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; there is an arrow &amp;lt;vp&amp;gt;-&amp;gt;&amp;lt;/vp&amp;gt; between the arguments and the &amp;#039;&amp;#039;return type&amp;#039;&amp;#039;.  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. &amp;lt;vp&amp;gt;Greeting&amp;lt;/vp&amp;gt;) after the type mainly serves documentation purposes.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4509&amp;oldid=prev</id>
		<title>Thomas Linder Puls: Less backreference</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4509&amp;oldid=prev"/>
		<updated>2018-09-26T08:53:54Z</updated>

		<summary type="html">&lt;p&gt;Less backreference&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 10:53, 26 September 2018&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l3&quot;&gt;Line 3:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 3:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Let us start by creating a function.  A function is a predicate that returns a value to place where the function was called from.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Let us start by creating a function.  A function is a predicate that returns a value to place where the function was called from.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{Lessons/Exercise|Add this code &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;to your {{Lesson|Hello World!}} project (or &lt;/del&gt;to a &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;similar &lt;/del&gt;console project&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;)&lt;/del&gt;:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{Lessons/Exercise|Add this code to a console project:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;vip&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;vip&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Thomas Linder Puls</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4505&amp;oldid=prev</id>
		<title>Thomas Linder Puls: initial</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4505&amp;oldid=prev"/>
		<updated>2018-09-25T15:06:37Z</updated>

		<summary type="html">&lt;p&gt;initial&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In the {{Lesson|Hello World!}} 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.&lt;br /&gt;
&lt;br /&gt;
Let us start by creating a function.  A function is a predicate that returns a value to place where the function was called from.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code to your {{Lesson|Hello World!}} project (or to a similar console project):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    makeGreeting : (string Name) -&amp;gt; string Greeting.&lt;br /&gt;
clauses&lt;br /&gt;
    makeGreeting(Name) = Result :-&lt;br /&gt;
        Result = string::format(&amp;quot;Hello %!\n&amp;quot;, Name).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project, and answer &amp;quot;Yes&amp;quot; to adding the &amp;#039;&amp;#039;&amp;#039;...string.ph&amp;#039;&amp;#039;&amp;#039; include statement.}}&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s start with the include statement.  In our new clause we are calling the &amp;lt;vp&amp;gt;format&amp;lt;/vp&amp;gt; predicate in the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class. However the &amp;lt;vp&amp;gt;sting&amp;lt;/vp&amp;gt; class is not visible in the &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; 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 &amp;quot;automation&amp;quot; will deal with most &amp;quot;including&amp;quot; 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&amp;#039;t really get to know the &amp;quot;include stuff&amp;quot;, and the day where the magic cannot figure out what to do we don&amp;#039;t know it either (see [[Ide/Creating new Project Items#Creating a Package|Creating a Package]]).&lt;br /&gt;
&lt;br /&gt;
Now let us return to the function itself.  As mentioned a function is a predicate that returns a value.  In the declaration of &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; there is an arrow &amp;lt;vp&amp;gt;-&amp;gt;&amp;lt;/vp&amp;gt; between the arguments and the &amp;#039;&amp;#039;return type&amp;#039;&amp;#039;.  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. &amp;lt;vp&amp;gt;Greeting&amp;lt;/vp&amp;gt;) after the type mainly serves documentation purposes.&lt;br /&gt;
&lt;br /&gt;
A function clause has the form &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt; :- &amp;lt;body&amp;gt;.&amp;lt;/vp&amp;gt;.  The execution flow is that we &amp;quot;enter&amp;quot; at the &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt;&amp;lt;/vp&amp;gt;, then the &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; is executed and then the &amp;lt;vp&amp;gt;&amp;lt;result&amp;gt;&amp;lt;/vp&amp;gt; is evaluated/executed and returned.&lt;br /&gt;
&lt;br /&gt;
In the clause above the returned value is in the variable &amp;lt;vp&amp;gt;Result&amp;lt;/vp&amp;gt;, which receives it value in the second line (i.e. in the &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; of the clause).  What happens in the second line is actually that we call another function, namely the function &amp;lt;vp&amp;gt;format&amp;lt;/vp&amp;gt; in the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
The name &amp;lt;vp&amp;gt;Result&amp;lt;/vp&amp;gt; is used just to illustrate that is does not have to be &amp;lt;vp&amp;gt;Greeting&amp;lt;/vp&amp;gt; even thoug 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.)&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; is closely related to &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt;.  The input is the same, but &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt; will write the result to the standard output stream, &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; returns the result as a string.&lt;br /&gt;
&lt;br /&gt;
Let us call our new function:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        DearGreeting = makeGreeting(&amp;quot;Dear&amp;quot;),&lt;br /&gt;
        stdio::write(DearGreeting),&lt;br /&gt;
        WorldGreeting = makeGreeting(&amp;quot;World&amp;quot;),&lt;br /&gt;
        stdio::write(WorldGreeting).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project (in a window).}}&lt;br /&gt;
&lt;br /&gt;
It is worth noticing that equal is a rather special thing in Visual Prolog. In the function clause &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt; :- &amp;lt;body&amp;gt;.&amp;lt;/vp&amp;gt; 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 &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;unlike&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; an assignement operator as you may know it from other programmig languages.  Notice for example that equal is symetrical:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; clause to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    makeGreeting : (string Name) -&amp;gt; string Greeting.&lt;br /&gt;
clauses&lt;br /&gt;
    makeGreeting(Name) = Result :-&lt;br /&gt;
        string::format(&amp;quot;Hello %!\n&amp;quot;, Name) = Result.&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the project.}}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; clause to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    makeGreeting : (string Name) -&amp;gt; string Greeting.&lt;br /&gt;
clauses&lt;br /&gt;
    makeGreeting(Name) = string::format(&amp;quot;Hello %!\n&amp;quot;, Name).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the project.}}&lt;br /&gt;
&lt;br /&gt;
Here we have used the result of the &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; function directly as the result of &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt;.  As a result we have nothing for the function &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt;, but fortunately the function &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; is optional (if there is no &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; then the &amp;lt;vp&amp;gt;:-&amp;lt;/vp&amp;gt; token must also be left out).&lt;br /&gt;
&lt;br /&gt;
In the same way we can use the returned value from &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; directly in a call to &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        stdio::write(makeGreeting(&amp;quot;Dear&amp;quot;)),&lt;br /&gt;
        stdio::write(makeGreeting(&amp;quot;World&amp;quot;)).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project (in a window).}}&lt;br /&gt;
&lt;br /&gt;
Summary:&lt;br /&gt;
&lt;br /&gt;
* A function declaration contains an arrow &amp;lt;vp&amp;gt;-&amp;gt;&amp;lt;/vp&amp;gt; followed by the type of the value that the function returns.&lt;br /&gt;
* A function clause has the form &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt; :- &amp;lt;body&amp;gt;.&amp;lt;/vp&amp;gt;&lt;br /&gt;
* Or the form &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt;.&amp;lt;/vp&amp;gt; if there is no need for a body.&lt;br /&gt;
* The equal operator is not an assignment operator as known from some other programming lanugages (it is for example symetrical).&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Thomas Linder Puls</name></author>
	</entry>
</feed>