{"id":193,"date":"2012-05-09T13:53:27","date_gmt":"2012-05-09T11:53:27","guid":{"rendered":"https:\/\/herr-rau.de\/blogs\/inform\/?page_id=193"},"modified":"2012-06-05T08:31:51","modified_gmt":"2012-06-05T06:31:51","slug":"methoden","status":"publish","type":"page","link":"https:\/\/herr-rau.de\/blogs\/inform\/?page_id=193","title":{"rendered":"Methoden"},"content":{"rendered":"<p>(Hier geht es vorerst nur um Methoden im engeren und herk\u00f6mmlichen Sinn. Zu dem Konzept der <code>rules<\/code> bei Inform: siehe dort.)<\/p>\n<p>Manchmal trifft man in B\u00fcchern auf eine Unterscheidung zwischen <strong>Prozeduren<\/strong> und <strong>Funktionen<\/strong>. Prozeduren sind dabei Routinen ohne R\u00fcckgabewert; Funktionen haben dagegen R\u00fcckgabewerte.<br \/>\nInform 7 unterscheidet sprachlich zwischen diesen beiden Formen. Allerdings spielen weder Prozeduren noch Funktionen eine zentrale Rolle beim Anlegen von Spielen. Wichtiger sind die Regeln, die die Benutzereingabe und Textausgabe beeinflussen, aber dazu sp\u00e4ter mehr.<\/p>\n<hr\/>\n<p>Die Definition einer <strong>Prozedur (ohne R\u00fcckgabewert)<\/strong> beginnt mit dem Schl\u00fcsselwort &#8220;To&#8221;, der Methodenname ist \u00fcblicherweise eine Verbkonstruktion im Infinitiv:<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">To count down:\r\n\tlet x be ten;\r\n\twhile x is at least zero:\r\n\t\tsay x;\r\n\t\tsay &quot;&#x5B;line break]&quot;;\r\n\t\tdecrease x by one.\r\n<\/pre>\n<p>In Java w\u00e4re das: <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">void count_down():\r\n\tint x = 10;\r\n\twhile (x&gt;=0) {\r\n\t\tSystem.out.print(x);\r\n\t\tSystem.out.println();\r\n\t\tx = x-1;\r\n\t}\r\n<\/pre>\n<hr\/>\n<p>Nat\u00fcrlich gibt es eine solche <strong>Funktion auch mit Argumenten<\/strong>:<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">To count down from (x - a number):\r\n\twhile x is at least zero:\r\n\t\tsay x;\r\n\t\tsay &quot;&#x5B;line break]&quot;;\r\n\t\tdecrease x by one.\r\n<\/pre>\n<p>Dabei ist &#8220;a number&#8221; der Datentyp des Arguments. Primitive Datentypen sind vor allem: text, number, truth state; aber nat\u00fcrlich k\u00f6nnte dort auch stehen: room, man, person, thing, container und so weiter.<\/p>\n<p>Danach k\u00f6nnte man die erste Methode oben auch einfacher schreiben, indem die Methode ohne Argumente die mit Argument aufruft:<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">To count down:\r\n\tcount down from ten;\r\n<\/pre>\n<p>Zahlen bis 12 kann man in W\u00f6rtern schreiben.<\/p>\n<hr\/>\n<p>Eine <strong>Funktion (also mit R\u00fcckgabewert)<\/strong> beginnt mit &#8220;To decide&#8221; und einem passenden Fragepronomen und geht dann etwas anders weiter als eine Methode:<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">To decide what number is the square of (x - a number):\r\n\tdecide on x times x.\r\n<\/pre>\n<p>&#8220;What number&#8221; legt dabei den R\u00fcckgabetyp fest, eine (ganze) Zahl. Stattdessen k\u00f6nnte dort auch stehen &#8220;which number&#8221; oder &#8220;what room&#8221;, je nach Datentyp des R\u00fcckgabewerts.<\/p>\n<p>Ein bisschen anders und n\u00e4her am nat\u00fcrlichen Englisch muss man das halten, wenn ein Wahrheitswert zur\u00fcckgegeben werden soll:<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">To decide whether (x - a number) is even:\r\n\tlet the half be x divided by two; &#x5B;Ganzzahldivision]\r\n\tif the half times two is x, decide yes;\r\n\totherwise decide no.\r\n<\/pre>\n<p>&#8220;Whether&#8221; (oder auch &#8220;if&#8221;) impliziert einen Wahrheitswert als R\u00fcckgabetyp. In Java hie\u00dfe die Funktion:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">boolean isEven(int x) {\r\n\tint half = x\/2;\r\n\tif (half*2==x) return true;\r\n\telse return false;\r\n}\r\n<\/pre>\n<p>Ein weiteres Beispiel:<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">To decide what number is (base - a number) to the power of (exponent - a number):\r\n\tlet the result be one;\r\n\twhile exponent is greater than zero:\r\n\t\tnow the result is the result times base;\r\n\t\tdecrease exponent by one;\r\n\tdecide on the result.<\/pre>\n<p>Nat\u00fcrlich beziehen sich Methoden und Funktionen in Spielen meist nicht auf Zahlen, sondern auf Datentypen, die f\u00fcr Spiele wichtiger sind. Eine einfache get-Funktion:<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">To decide which hat is the favourite of (x - a person):\r\n\tdecide on the favourite hat of x.\r\n<\/pre>\n<p>Dabei ist das &#8220;which hat&#8221; oder auch &#8220;what hat&#8221; der R\u00fcckgabeteyp, (x &#8211; a person) das Argument (Klasse: person), das erste &#8220;the favourite of&#8221; der Bezeichner der Methode und das zweite &#8220;the favourite hat of&#8221; der Attributsbezeichner.<\/p>\n<p>Und die entsprechende set-Funktion:<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">To set the favourite of (x - a person) to (z - a hat):\r\n\tnow the favourite hat of x is z.\r\n<\/pre>\n<p>oder auch:<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">To make (z - a hat) the favourite of (x - a person) :\r\n\tnow the favourite hat of x is z.\r\n<\/pre>\n<p>Dazu muss man vorher eine Klasse <code>hat<\/code> angelegt haben: <code>A hat is a kind of thing. It is always wearable.<\/code>. Au\u00dferdem muss die Klasse <code>person<\/code> um ein Attribut erweitert werden: <code>Every person has a hat called favourite hat.<\/code><\/p>\n<p>Allerdings sind die set\/get-Methoden ohnehin nicht sehr wichtig, da alle Attribute \u00f6ffentlich sind.<\/p>\n<hr\/>\n<p>Aufgerufen werden Methoden und Funktionen (immer innerhalb von anderen Methoden oder Regeln) dann zum Beispiel so:<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">\r\n\tcount down;\r\n\tcount down from 15;\r\n\tsay the square of 4;\r\n\tsay 2 to the power of 3;\r\n\tif 3 to the power of 5 is even:\r\n\t\tcount down from 2 to the power of 3;\r\n\tif the favourite of Bogart is not the Homburg:\r\n\t\tmake the Homburg the favourite of Bogart.\r\n<\/pre>\n<hr\/>\n<p>Den Typ des Argument einer Methode kann man noch weiter eingrenzen<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">\r\nTo turn over (x - an open portable container):\r\n<\/pre>\n<p>Dann kann die Methode nur f\u00fcr Container aufgerufen werden, die offen und tragbar sind.<br \/>\nW\u00e4hrend open und portable boolesche Attribute sind, die Inform bereits mitbringt, kann man auch weitere solche Adjektive definieren:<\/p>\n<pre class=\"brush: inform7; title: ; notranslate\" title=\"\">\r\nDefinition: a person is hatless if they do not wear a hat.\r\n<\/pre>\n<p>Dann kann ich <code>(x - a hatless person)<\/code> als Argument einer Methode festlegen. Aber das ist alles etwas f\u00fcr sp\u00e4ter mal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Hier geht es vorerst nur um Methoden im engeren und herk\u00f6mmlichen Sinn. Zu dem Konzept der rules bei Inform: siehe dort.) Manchmal trifft man in B\u00fcchern auf eine Unterscheidung zwischen Prozeduren und Funktionen. Prozeduren sind dabei Routinen ohne R\u00fcckgabewert; Funktionen &hellip; <a href=\"https:\/\/herr-rau.de\/blogs\/inform\/?page_id=193\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":94,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-193","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/herr-rau.de\/blogs\/inform\/index.php?rest_route=\/wp\/v2\/pages\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/herr-rau.de\/blogs\/inform\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/herr-rau.de\/blogs\/inform\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/herr-rau.de\/blogs\/inform\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/herr-rau.de\/blogs\/inform\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=193"}],"version-history":[{"count":0,"href":"https:\/\/herr-rau.de\/blogs\/inform\/index.php?rest_route=\/wp\/v2\/pages\/193\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/herr-rau.de\/blogs\/inform\/index.php?rest_route=\/wp\/v2\/pages\/94"}],"wp:attachment":[{"href":"https:\/\/herr-rau.de\/blogs\/inform\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}