{"id":2481,"date":"2023-12-20T13:03:12","date_gmt":"2023-12-20T12:03:12","guid":{"rendered":"https:\/\/oopm.org\/?page_id=2481"},"modified":"2024-12-20T09:27:29","modified_gmt":"2024-12-20T08:27:29","slug":"4c-2-methods","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=2481","title":{"rendered":"7.2 Method details"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages2481&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages2481&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<style>\n#CXXX {\n  width: 30%;\n  padding-left: 15px;\n  margin-left: 15px;\n  float: right;\n  background-color: lightgray;\n  font-size: 14px;\n}\n<\/style>\n<p id=\"CXXX\">In programming languages in general, a method is often called a procedure, or function. The term method was introduced by Smalltalk and has since then been used instead of procedure in OO languages.\nSometimes the term function is used for a procedure that computes a value only based on its parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Objects may have attributes in the form of methods. In the previous chapters, we have seen examples of methods such as&nbsp;<code>addInterest<\/code>,&nbsp;<code>deposit<\/code> and&nbsp;<code>withdraw<\/code>. Here we describe methods in more detail.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A method describes a sequence of statements to be executed. Execution of a statement results in some operations that may change the datums of data-items or compute new datums based on the datums of the data-items of the enclosing object and its possible parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A method has the form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>MethodName<\/em>(<em>Parameter<\/em>s) <em>Return<\/em>Value: <em>SuperMethod<\/em>\n   <em>Declarations<\/em>\n   <em>Statements<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A method has a name as specified by <em><code>MethodName<\/code><\/em>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A method may have parameters as specified by <em><code>Parameters<\/code><\/em> &#8211; if no parameters, the brackets are not needed. The <em><code>Parameters<\/code><\/em> may be one or more declarations of data-items or virtual classes and methods &#8211; see section <script>mkRef(\"arameter transfer and return value\");<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In section <script>mkRef(\"Method returning a value\");<\/script> and chapter <script>mkRef(\"Classes\");<\/script> , we have seen examples of methods&nbsp;<code>deposit<\/code>&nbsp;and&nbsp;<code>withdraw<\/code> that both have one parameter. In the next example we show an example of a method with two parameters:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>transfer(from: <strong>ref<\/strong> Account, to <strong>ref<\/strong> Account, amount: <strong>var<\/strong> float):\n   from.withdraw(amount)\n   to.deposit(amount)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A method may also specify a value to be returned by an invocation of the method &#8211; <code><em>Return<\/em>Value<\/code> is the type of this value. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A method may also have a supermethod as specified by <em><code>SuperMethod<\/code><\/em>. A supermethod is similar to a superclass &#8211; we explain supermethods in section <script>mkRef(\"Subclasses and submethods\");<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The body of a method consist of a sequence of possible declarations and statements as specified by <em><code>Declarations<\/code><\/em> and <em><code>Statements<\/code><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The clause <code><em>Return<\/em>Value<\/code> is either empty or has the form: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-&gt; <em>Return<span style=\"font-family: inherit; font-size: 1.125rem;\">Declaration<\/span><\/em><\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">where <code><em>ReturnDeclaration<\/em><\/code> may be a declaration of a variable (<strong><code>var<\/code><\/strong> or <strong><code>ref<\/code><\/strong>) data-item.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In chapter 2 and 3, the method <code>withdraw<\/code> is an example of a method with a return value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   withdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n      balance := balance - amount\n      newB<strong> := <\/strong>balance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, a method has almost the same structure as a class. The only difference is that a return value may be specified. A class has an implicit return value, being a reference to an object of the class. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method invocation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Chapters 2 and 3, we have seen examples of execution \u2014 invocation, of methods. Below we show an invocation of transfer from the account of John Smith to the account of Liza Jones.:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>transfer(account_1010, account_1022, 218)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the next example, we have defined&nbsp;<code>transfer<\/code>&nbsp;within an object&nbsp;<code>BankSys<\/code>&nbsp;together with class&nbsp;<code>Account<\/code>&nbsp;and an instance of&nbsp;<code>Account<\/code>. We  have modified <code>deposit<\/code> to also return the new <code>balance<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BankSys: <strong>obj<\/strong>\n   <strong>class<\/strong> Account(owner: <strong>var<\/strong> String):\n      balance: <strong>var<\/strong> float\n      -\"-\n      deposit(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n         -\"-      \n      withdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n         -\"-\n\n   transfer(from: <strong>ref<\/strong> Account, to <strong>ref<\/strong> Account, amount: <strong>var<\/strong> float):\n      newBalanceFrom, newBalanceTo: <strong>var<\/strong> float\n      newBalanceFrom := from.withdraw(amount)\n      newBalanceTo := to.deposit(amount)\n\n   account_1010: <strong>obj<\/strong> Account(\"John Smith\")\n   account_1022: <strong>obj<\/strong> Account(\"Liza Jones\")\n   account_1010.deposit(350)\n   transfer(account_1010, account_1022, 218)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The transfer method has two local data-items <code>newBalanceFrom<\/code> and <code>newBalanceTo<\/code>, which are assigned the new values of <code>balance<\/code> of <code>from<\/code> and <code>to<\/code> respectively. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to the declarations of <code>BankSys<\/code>, we have added statements that are executed by <code>BanksSys<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   account_1010: <strong>obj<\/strong> Account(\"John Smith\")\n   account_1022: <strong>obj<\/strong> Account(\"Liza Jones\")\n   account_1010.deposit(350)\n   transfer(account_1010, account_1022, 218)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A method invocation creates a <em>method object<\/em> that has the structure specified the method. The method object contains possible parameters and possible data-items of the method. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following snapshot shows the situation at the end of execution of the <code>transfer<\/code> method object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      transfer(from: <strong>ref<\/strong> Account, to <strong>ref<\/strong> Account, amount: <strong>var<\/strong> float):\n         newBalanceFrom, newBalanceTo: <strong>var<\/strong> float\n         newBalanceFrom := from.withdraw(amount)\n         newBalanceTo := to.deposit(amount)\n   <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"582\" height=\"247\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/aTransferMethodObject-1.jpg\" alt=\"\" class=\"wp-image-10426\" style=\"width:502px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/aTransferMethodObject-1.jpg 582w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/aTransferMethodObject-1-300x127.jpg 300w\" sizes=\"(max-width: 582px) 100vw, 582px\" \/><figcaption class=\"wp-element-caption\">A transfer method object<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned in section <script>mkRef(\"The first program\")<\/script>, the initial value of <code>balance<\/code> is 0 (zero), which means that before <code>transfer<\/code> is executed <code>account_1010.balance<\/code> is 350 and <code>account_2022.balance<\/code> is 0 (zero).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A method object is sometimes called a <em>method invocation<\/em> or just <em>invocation<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is a strong similarity between method invocation and method object on the one side and class instantiation and object on the other side. Both forms of invocation\/instantion create an object and starts executing the statements of the object. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The object (class object or method object) executing a method invocation is denoted the <em>invoker<\/em> of the method object and the method object is sometimes denoted the <em>invokee<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Again, we may see that a method invocation is quite similar to a class instantiation. The difference in wording between invocation and instantiation is that for a method, invocation emphasizes that execution of statements is the primary task. For a class, instantiation emphasise that creation of an object is the primary task.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=2481\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>In programming languages in general, a method is often called a procedure, or function. The term method was introduced by Smalltalk and has since then been used instead of procedure in OO languages. Sometimes the term function is used for a procedure that computes a value only based on its parameters. Objects may have attributes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2338,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2481","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2481"}],"version-history":[{"count":67,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2481\/revisions"}],"predecessor-version":[{"id":10689,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2481\/revisions\/10689"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2338"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}