{"id":415,"date":"2023-05-14T13:30:01","date_gmt":"2023-05-14T11:30:01","guid":{"rendered":"https:\/\/oopm.org\/?page_id=415"},"modified":"2025-01-12T13:26:44","modified_gmt":"2025-01-12T12:26:44","slug":"2-5-method-with-parameter","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=415","title":{"rendered":"2.6 Method with parameter"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages415&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=wpv2pages415&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\">Next, we add a method for depositing an amount to the account, and since the amount to be deposited in most cases differ from time to time it is possible to define a method with a parameter defining the amount to be deposited.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the next example, we add a method&nbsp;<code>deposit<\/code>, with a parameter&nbsp;<code>amount<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><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<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The variable&nbsp;<code>amount<\/code>&nbsp;is a&nbsp;<em>parameter<\/em>&nbsp;of <code>deposit<\/code>. A value for&nbsp;<code>amount<\/code>&nbsp;must be supplied when <code>deposit<\/code> is invoked as shown here:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code>account_1010.deposit(100)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The value supplied (here 100) is called the <em>actual paramete<\/em>r of <code>deposit<\/code> in contrast to <code>amount<\/code>, which is called the <em>formal parameter<\/em>. The term <em>parameter<\/em> is often used to mean the formal parameter whereas the term <em>argument<\/em> means the actual parameter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above statement is an example of a <em>method invocation<\/em>. It implies that an instance of <code>deposit<\/code> is generated, and this implies that a computation as described by <code>deposit<\/code> is carried out<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> <\/mark>by execution of the statement <code>balance := balance + amount<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this computation, the value 100 is supplied for&nbsp;<code>amount<\/code>. This means that&nbsp;<code>amount<\/code>&nbsp;holds the value 100 when the assignment statement of&nbsp;<code>deposit<\/code>&nbsp;is executed. The body of&nbsp;<code>deposit<\/code>&nbsp;adds&nbsp;<code>amount<\/code>&nbsp;to the current value of&nbsp;<code>balance<\/code>. If&nbsp;<code>balance<\/code>&nbsp;holds the value 350.56 before invocation of the method, it holds the value 450.56 after the execution of the invocation.<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;6a8bd7edb8815&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"6a8bd7edb8815\" class=\"wp-block-image aligncenter size-full wp-lightbox-container\"><img fetchpriority=\"high\" decoding=\"async\" width=\"814\" height=\"200\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on--pointerdown=\"actions.preloadImage\" data-wp-on--pointerenter=\"actions.preloadImageWithDelay\" data-wp-on--pointerleave=\"actions.cancelPreload\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/MethodInvocation.jpg\" alt=\"\" class=\"wp-image-7703\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/MethodInvocation.jpg 814w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/MethodInvocation-300x74.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/MethodInvocation-768x189.jpg 768w\" sizes=\"(max-width: 814px) 100vw, 814px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\tdata-wp-bind--aria-label=\"state.thisImage.triggerButtonAriaLabel\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.thisImage.buttonRight\"\n\t\t\tdata-wp-style--top=\"state.thisImage.buttonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><figcaption class=\"wp-element-caption\">Method invocation<\/figcaption><\/figure>\n\n\n\n<p class=\"has-normal-font-size wp-block-paragraph\">An instance of <code>deposit<\/code> is called a <em>method object<\/em> and is in many ways similar to an object like <code>account_1010<\/code> &#8211; we return to this later.<\/p>\n\n\n\n<p class=\"has-normal-font-size wp-block-paragraph\">In general, a method may have local data-items like <code>owner<\/code>, <code>balance<\/code> and <code>interestRate<\/code> of <code>account_1010<\/code>. The parameter <code>amount<\/code> is also an attribute (data-item) of the method object.<\/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=415\" 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>Next, we add a method for depositing an amount to the account, and since the amount to be deposited in most cases differ from time to time it is possible to define a method with a parameter defining the amount to be deposited. In the next example, we add a method&nbsp;deposit, with a parameter&nbsp;amount: The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":175,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-415","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\/415","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=415"}],"version-history":[{"count":34,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/415\/revisions"}],"predecessor-version":[{"id":10989,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/415\/revisions\/10989"}],"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=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}