{"id":3421,"date":"2025-04-18T07:52:50","date_gmt":"2025-04-18T07:52:50","guid":{"rendered":"https:\/\/webprojects.cloud\/wordpress\/splatco\/?post_type=spl_knowledgebase&#038;p=3421"},"modified":"2025-06-10T11:12:17","modified_gmt":"2025-06-10T11:12:17","slug":"subroutines-passing-arguments-and-results","status":"publish","type":"spl_knowledgebase","link":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/","title":{"rendered":"Subroutines: Passing arguments and results"},"content":{"rendered":"\n<p>Some subroutines, I dare say most subroutines in SPLat applications, will simply perform some fixed operation without any information passing between the subroutine and the calling program. In some cases, though, you may need some data to pass one way or the other. You may for example have a subroutine that reads a temperature, performing the analog reading and all the necessary calculations as a service to the &#8220;caller&#8221;.<\/p>\n\n\n\n<p>There several ways of passing information back and forth:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As data held on the register stack (<a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/glossary-of-terms\/#o3808\">X<\/a>, Y, Z and\/or T)<\/li>\n\n\n\n<li>As data held in the floating point registers W and\/or Q<\/li>\n\n\n\n<li>As data held in&nbsp;<a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/glossary-of-terms\/#o414\">RAM<\/a><\/li>\n<\/ul>\n\n\n\n<p>The information passed may be into the subroutine as arguments, or out of the subroutine as results.<\/p>\n\n\n\n<p>The most common method will be in one of the registers, i.e. X, Y etc or W\/Q. Avoid using RAM unless you need to pass more data than will fit in the registers. It is good programming practice to let a given RAM location &#8220;belong&#8221; only to one subroutine or another (or the top level &#8220;main&#8221; program), and not be accessible to others. This is an aspect of what is known as&nbsp;<em>data hiding<\/em>.<\/p>\n\n\n\n<p>One interesting and useful technique is to use X to return an error signal from a subroutine that performs an operation that might go wrong. For example, an industrial cleaning machine may have a detergent pump driven by a subroutine called&nbsp;<code>Squirt. Squirt<\/code>&nbsp;attempts to dispense one dose of detergent. It returns a logical True value if the operation succeeds and False if it fails (say due to the detergent reservoir being empty). The calling program can the simply call&nbsp;<code>Squirt <\/code>and test the outcome, rather than having to first test the reservoir level:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">      GoSub    Squirt      ;Squirt in a dose of soap<br>      <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/goto-gosub-and-related-instructions\/goiff-llll\/\">GoIfF<\/a>    Failed      ;G\/ something went wrong<\/pre>\n\n\n\n<p>This might in turn be part of a larger subroutine that carries out a complete sequence. In that case we might make it so it so the larger subroutine returns a zero in X if everything went OK, and a non-zero error number if something went wrong:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Cycle:<br>      GoSub    Step1         ;Returns False if it fails<br>      <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/stack-instructions\/loadx-nn\/\">LoadX<\/a>    Step1FailCode ;In case Step1 failed<br>      <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/stack-instructions\/swap\/\">Swap<\/a>                   ;Get the Step1 result back to X<br>      <a href=\"http:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/goto-gosub-and-related-instructions\/retiff\/\">RetIfF<\/a><br><br>      GoSub    Step2         ;Returns False if it fails<br>      <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/stack-instructions\/loadx-nn\/\"><\/a><a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/stack-instructions\/loadx-nn\/\"><\/a><a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/stack-instructions\/loadx-nn\/\">LoadX<\/a>    Step2FailCode ;In case Step1 failed<br>      <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/stack-instructions\/swap\/\">Swap<\/a>                   ;Get the Step2 result back to X<br>      <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/goto-gosub-and-related-instructions\/retiff\/\">RetIfF<\/a><br>... etc ...<br>      LoadX    0             ;Signal success<br>      Return<\/pre>\n\n\n\n<p>The above subroutine can be called to perform the whole cycle, and when it is done the outcome is in X for evaluation. This may for example be to display a message on the LCD:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">      GoSub    Cycle         ;Run a cycle, result code returned in X\n      Branch                 ;Test the outcome\n      Target   ItWorked      ;Return code = 0\n      Target   NoSoap        ;Return code = 1\n      Target   OverLoad      ;Return code = 2\n      Target   NoFill        ;Return code = 3\n\nItWorked:\n      OBLCD_Cls\n      OBLCD_Text  \"Cycle complete\"\n      GoTo     DoTheNextThing\n\nNoSoap:\n      OBLCD_Cls\n      OBLCD_Text  \"No detergent\"\n      GoTo     DoTheNextThing\n.... etc ....<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Some subroutines, I dare say most subroutines in SPLat applications, will simply perform some fixed operation without any information passing between the subroutine and the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3414,"menu_order":4,"template":"","class_list":["post-3421","spl_knowledgebase","type-spl_knowledgebase","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Subroutines: Passing arguments and results - SPLat Controls<\/title>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Subroutines: Passing arguments and results - SPLat Controls\" \/>\n<meta property=\"og:description\" content=\"Some subroutines, I dare say most subroutines in SPLat applications, will simply perform some fixed operation without any information passing between the subroutine and the...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/\" \/>\n<meta property=\"og:site_name\" content=\"SPLat Controls\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-10T11:12:17+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/\",\"name\":\"Subroutines: Passing arguments and results - SPLat Controls\",\"isPartOf\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website\"},\"datePublished\":\"2025-04-18T07:52:50+00:00\",\"dateModified\":\"2025-06-10T11:12:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials, application notes and white papers\",\"item\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Tutorial: Subroutines\",\"item\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Subroutines: Passing arguments and results\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/\",\"name\":\"SPLat Controls\",\"description\":\"OEM Embedded Machine Controllers\",\"publisher\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization\",\"name\":\"SPLat Controls\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg\",\"contentUrl\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg\",\"caption\":\"SPLat Controls\"},\"image\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Subroutines: Passing arguments and results - SPLat Controls","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Subroutines: Passing arguments and results - SPLat Controls","og_description":"Some subroutines, I dare say most subroutines in SPLat applications, will simply perform some fixed operation without any information passing between the subroutine and the...","og_url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/","og_site_name":"SPLat Controls","article_modified_time":"2025-06-10T11:12:17+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/","name":"Subroutines: Passing arguments and results - SPLat Controls","isPartOf":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website"},"datePublished":"2025-04-18T07:52:50+00:00","dateModified":"2025-06-10T11:12:17+00:00","breadcrumb":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-passing-arguments-and-results\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webprojects.cloud\/wordpress\/splatco\/"},{"@type":"ListItem","position":2,"name":"Tutorials, application notes and white papers","item":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/"},{"@type":"ListItem","position":3,"name":"Tutorial: Subroutines","item":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/"},{"@type":"ListItem","position":4,"name":"Subroutines: Passing arguments and results"}]},{"@type":"WebSite","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/","name":"SPLat Controls","description":"OEM Embedded Machine Controllers","publisher":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webprojects.cloud\/wordpress\/splatco\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization","name":"SPLat Controls","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg","contentUrl":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg","caption":"SPLat Controls"},"image":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/spl_knowledgebase\/3421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/spl_knowledgebase"}],"about":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/types\/spl_knowledgebase"}],"author":[{"embeddable":true,"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/users\/1"}],"up":[{"embeddable":true,"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/spl_knowledgebase\/3414"}],"wp:attachment":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/media?parent=3421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}