{"id":411,"date":"2023-05-14T13:28:16","date_gmt":"2023-05-14T11:28:16","guid":{"rendered":"https:\/\/oopm.org\/?page_id=411"},"modified":"2025-01-12T13:28:34","modified_gmt":"2025-01-12T12:28:34","slug":"2-6-method-returning-a-value","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=411","title":{"rendered":"2.7 Method returning a value"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages411&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=wpv2pages411&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<p class=\"wp-block-paragraph\">For completeness, we add a method for withdrawing an amount from the account. The name of the method is&nbsp;<code>withdraw<\/code>&nbsp;and it has a parameter&nbsp;<code>amount<\/code> being the amount to be withdrawn from the account. In addition to withdrawing an amount, the method returns the updated balance to the invoker of the method.<\/p>\n\n\n\n<pre class=\"wp-block-code has-primary-color has-custom-color-1-background-color has-text-color has-background has-link-color wp-elements-f351afac802da897afdba6d3a81ca699\"><code>account_1010: <strong>obj<\/strong>\n   owner: <strong>val<\/strong> \"John Smith\"\n   balance: <strong>var<\/strong> float\n   interestRate: <strong>var<\/strong> float\n   addInterest:\n      balance := balance + (balance * interestRate) \/ 100\n   deposit(amount: <strong>var<\/strong> float):\n      balance := balance + amount\n   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\">The arrow (<code>-&gt;<\/code>) in&nbsp;<code>-&gt;&nbsp;newB: <strong>var<\/strong> float&nbsp;<\/code>specifies that the value of the data-item <code>newB<\/code> is returned by <code>withdraw<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen,&nbsp;<code>withdraw<\/code>&nbsp;contains two assignment statements. First&nbsp;<code>amount<\/code>&nbsp;is deducted from&nbsp;<code>balance<\/code>&nbsp;(<code>balance := balance - amount<\/code>) and next, the updated value of&nbsp;<code>balance<\/code>&nbsp;is assigned to <code>newB<\/code>, which is returned as the value of <code>withdraw<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The method&nbsp;<code>withdraw<\/code>&nbsp;may be invoked as shown here:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code>newBalance: <strong>var<\/strong> float\nnewBalance := account_1010.withdraw(140)<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We have declared a variable&nbsp;<code>newBalance<\/code>&nbsp;of type&nbsp;<code>float<\/code>.&nbsp;<\/li>\n\n\n\n<li>The assignment statement&nbsp;<br><code>newBalance := account_1010.withdraw(140)<\/code>&nbsp;is executed.<\/li>\n\n\n\n<li>The method&nbsp;<code>withdraw<\/code>&nbsp;of&nbsp;<code>account_1010&nbsp;<\/code>is executed with the parameter&nbsp;<code>amount<\/code>&nbsp;holding the value <code>140<\/code>.<\/li>\n\n\n\n<li>Assuming that&nbsp;<code>balance<\/code>&nbsp;is <code>450.56<\/code> as in the example above, the new value of&nbsp;<code>balance<\/code>&nbsp;will then be <code>310.56<\/code>.&nbsp;<\/li>\n\n\n\n<li>This new value of&nbsp;<code>balance<\/code>&nbsp;is returned by <code>withdraw<\/code>.&nbsp; <\/li>\n\n\n\n<li>After the execution of&nbsp;<code>newBalance := account_1010.withdraw(140)<\/code>, the variable&nbsp;<code>newBalance<\/code>&nbsp;holds the value <code>310.56<\/code>.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose we would like to prevent a withdraw if there is not enough money on the account. We can do this by using an <code>if:then<\/code> statement to test if <code>balance<\/code> is greater than the <code>amount<\/code> to be withdrawn:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code>   withdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n      if (balance &gt;= amount) :then\n         balance := balance - amount\n      newB<strong> := <\/strong>balance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>if:then<\/code> statement has a condition <code>(balance >= about)<\/code>, which is evaluated<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>and results in a Boolean value that is either true or false. The operator <code>>=<\/code> means greater than or equal. If <code>balance<\/code> is greater than or equal <code>amount<\/code> then <code>(balance >= amount)<\/code> evaluates to true otherwise it evaluates to false.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the condition evaluates to true, then and only then is the part after <code>:then<\/code>  executed &#8211; in this case the statement <code>balance := balance - amount<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>if:then<\/code> statement thus ensures that a <code>withdraw<\/code> is only possible if there is sufficient money on the account. With the above solution, <code>withdraw<\/code> only does not update <code>balance<\/code> &#8211; in practice one has to take some action to inform about the <code>withdraw<\/code> not being possible. We return to that in section <script>mkRef(\"Statements\");<\/script>.<\/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=411\" 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>For completeness, we add a method for withdrawing an amount from the account. The name of the method is&nbsp;withdraw&nbsp;and it has a parameter&nbsp;amount being the amount to be withdrawn from the account. In addition to withdrawing an amount, the method returns the updated balance to the invoker of the method. The arrow (-&gt;) in&nbsp;-&gt;&nbsp;newB: var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":175,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-411","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\/411","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=411"}],"version-history":[{"count":46,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/411\/revisions"}],"predecessor-version":[{"id":10990,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/411\/revisions\/10990"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/175"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}