{"id":2485,"date":"2023-12-20T13:05:08","date_gmt":"2023-12-20T12:05:08","guid":{"rendered":"https:\/\/oopm.org\/?page_id=2485"},"modified":"2024-12-15T09:26:27","modified_gmt":"2024-12-15T08:26:27","slug":"4c-3-parameter-transfer","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=2485","title":{"rendered":"7.3 Parameter transfer and return value"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages2485&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=wpv2pages2485&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 a class instantiation, an object is created and for a method invocation, a method object is created.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then the actual parameters of the invocation\/instantiation are evaluated and transferred to the corresponding data-items of the method object\/object. When the method has been executed, a possible value may be returned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parameter transfer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The transfer of parameters corresponds to assignment of the actual parameters to the formal parameters. Consider the following method invocation:<\/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\">The invocation consists of the following steps:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ol class=\"wp-block-list\">\n<li>A <code>transfer<\/code>-object is generated &#8211; assume that <code>aTransfer<\/code> refers to this object. The situation is then as shown in the snapshot where the reference variables <code>from<\/code> and to<code> <\/code>have the initial value <code>none<\/code> and <code>amount<\/code> has the initial value 0 (zero).<\/li>\n<\/ol>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"233\" height=\"138\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/aTransfer-1-1.jpg\" alt=\"\" class=\"wp-image-10428\" style=\"width:208px;height:auto\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\">2. The actual parameters are then assigned to the corresponding data-items in <code>aTransfer<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>aTransfer.from := account_1010aTransfer.to := account_1022<\/code><code>aTransfer.amount := 218<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The situation is then as shown in the snapshot where the <code>from<\/code> refers to <code>account_1010<\/code>, <code>to<\/code> refers to <code>account_1022<\/code>, and <code>amount<\/code> has the value 218.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"565\" height=\"277\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/aTransfer-2-1.jpg\" alt=\"\" class=\"wp-image-10430\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/aTransfer-2-1.jpg 565w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/aTransfer-2-1-300x147.jpg 300w\" sizes=\"(max-width: 565px) 100vw, 565px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\">3. The statements of <code>aTransfer<\/code> are executed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from.withdraw(amount)\nto.deposit(amount)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The situation is then as shown in the snapshot.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"264\" height=\"274\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/aTransfer-3-1.jpg\" alt=\"\" class=\"wp-image-10432\" style=\"width:236px;height:auto\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In the example, we have two types of parameters:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Amount<\/code> is a a <em>value parameter<\/em> of type integer. A value type is copied to the data-item of the method object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>From<\/code> and <code>to<\/code> are <code>reference parameters<\/code>. For a reference parameter the reference is assigned to the data-item in the method object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For an elaboration of the difference between value and object and thus value assign and reference assign, see section <script>mkRef(\"Objects and values\");<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is a third kind of parameter being virtual methods and\/or virtual classes. In section <script>mkRef(\"Collections\")<\/script>. we saw an example of a virtual class as a parameter:class <code>ElmType<\/code> as a virtual class parameter of class <code>Set<\/code>. In section <script>mkRef(\"Control structure methods\")<\/script>, we show examples of virtual method parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Parameter transfer for a class instantiation, takes place in a similar way. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Return value<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned, a method invocation may return a value being computed by the method. This is e.g. the case for the <code>withdraw<\/code> method of <code>Account<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Account:\n   ...\n   withdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n      balance := balance - amount\n      newB := balance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The value to be returned is defined by the clause <code>-&gt; newB: <strong>var<\/strong> float<\/code>. It defines a data-item <code>newB<\/code>, which holds the value to be returned. In <code>withdraw<\/code>, the statement <code>newB := balance<\/code> assigns a value to <code>newB<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The return value of <code>withdraw<\/code> may e.g. be used in as shown here:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anAmount := anAccount.withdraw(300) <\/code><\/pre>\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=2485\" 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 a class instantiation, an object is created and for a method invocation, a method object is created. Then the actual parameters of the invocation\/instantiation are evaluated and transferred to the corresponding data-items of the method object\/object. When the method has been executed, a possible value may be returned. Parameter transfer The transfer of parameters [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2338,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2485","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\/2485","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=2485"}],"version-history":[{"count":32,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2485\/revisions"}],"predecessor-version":[{"id":10433,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2485\/revisions\/10433"}],"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=2485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}