{"id":3416,"date":"2025-04-18T07:44:48","date_gmt":"2025-04-18T07:44:48","guid":{"rendered":"https:\/\/webprojects.cloud\/wordpress\/splatco\/?post_type=spl_knowledgebase&#038;p=3416"},"modified":"2025-06-10T11:11:18","modified_gmt":"2025-06-10T11:11:18","slug":"subroutines-what-are-they","status":"publish","type":"spl_knowledgebase","link":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/","title":{"rendered":"Subroutines: What are they?"},"content":{"rendered":"\n<p>A subroutine is a block of code that can be invoked (&#8220;called&#8221;) from anywhere in a program, indeed from many places in a program, and when completed can return control back to the main program. Suppose you have a program where, in several places, you need to turn on a pump if analog input A is more than 39 and the digital input PumpEnable is ON. Otherwise you want the pump off. This code will do it, at least in one place within the program:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">          <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/goto-gosub-and-related-instructions\/goifinoff-iillll\/\">GoIfInOff<\/a>   PumpEnable,PumpOff<br>          <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/analog-instructions\/anin\/\">AnInA<\/a><br>          <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/number-comparison-and-testing-instructions\/comparison-and-testing-for-byte-values\/goifxle-nnllll\/\">GoIfXLE<\/a>     39,PumpOff<br>          <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/input-and-output-instructions-digital\/on-oo\/\">ON<\/a>          Pump<br>          <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/fasttrack-instructions\/goto-llll\/\">GoTo<\/a>        Continue<br>PumpOff:  <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/input-and-output-instructions-digital\/off-oo\/\">Off<\/a>         Pump<br>Continue: ;end of code fragment ... carry on<\/pre>\n\n\n\n<p>Let&#8217;s call this bit of functionality &#8220;PumpServe&#8221;. Imagine we need to do this &#8220;thing&#8221; in 3 different places in a program. We could achieve that by copying and pasting the same code into 3 places, and incidentally changing the line labels Pumpoff and Continue each time. Alternatively we could make &#8220;PumpServe&#8221; into a subroutine. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"312\" height=\"265\" src=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2025\/04\/Subroutines.gif\" alt=\"\" class=\"wp-image-3417\"\/><\/figure>\n\n\n\n<p>The above drawing illustrates how a subroutine is used. In the main program (left) there are several references (calls) to the subroutine, using GoSub (Go to Subroutine) instructions. Each time the subroutine is called,&nbsp;<a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/glossary-of-terms\/#o3807\">SPLatty<\/a>&nbsp;jumps across to the single copy of the PumpServe code, but&nbsp;<em>remembers where he came from<\/em>. When he is has done the PumpServe operation he returns to the line following the GoSub instruction. This is done with a Return instruction. Hence, a GoSub is similar to a GoTo, but has the added feature of remembering where it came from, so SPLatty can find his way back again. There is a special memory within SPLat called a &#8220;subroutine stack&#8221;, that remembers the return address.<\/p>\n\n\n\n<p>A subroutine may call another subroutine, and that one may in turn call another subroutine. However, you cannot &#8220;nest&#8221; subroutines indefinitely. The limit is 4 nested subroutines in dialects prior to 12, and 6 in dialect 12 and later.<\/p>\n\n\n\n<p>Let&#8217;s now re-write the PumpServe function as a subroutine:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">PumpServe:<br>          <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/input-and-output-instructions-digital\/goifinoff-iillll\/\">GoIfInOff<\/a>   PumpEnable,PumpOff<br>          <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/analog-instructions\/anin\/\">AnInA<\/a><br>          <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/number-comparison-and-testing-instructions\/comparison-and-testing-for-byte-values\/goifxle-nnllll\/\">GoIfXLE<\/a>     39,PumpOff<br>          ON          Pump<br>          <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/goto-gosub-and-related-instructions\/return\/\">Return<\/a><br>PumpOff:  Off         Pump<br>          Return<\/pre>\n\n\n\n<p>You will notice there is no longer any need to have the&nbsp;<code>GoTo Continue<\/code>, as it is perfectly OK to have a Return instruction in the middle of the subroutine.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Postscript:<\/h6>\n\n\n\n<p>Some programmers maintain that it is bad practice to write a subroutine with multiple return instructions, as I did above. If you are just starting out it may be a moot point. As you gain experience you may agree, on the basis that with only a single exit point from a subroutine there are fewer hazards if you alter the code for some reason. Re-writing the above example in line with this principle, we would get:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">PumpServe:\n          GoIfInOff   PumpEnable,PumpOff\n          AnInA\n          GoIfXLE     39,PumpOff\n          ON          Pump\n          GoTo        PumpRet\nPumpOff:  Off         Pump\nPumpRet:  Return<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A subroutine is a block of code that can be invoked (&#8220;called&#8221;) from anywhere in a program, indeed from many places in a program, and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3414,"menu_order":0,"template":"","class_list":["post-3416","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: What are they? - 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: What are they? - SPLat Controls\" \/>\n<meta property=\"og:description\" content=\"A subroutine is a block of code that can be invoked (&#8220;called&#8221;) from anywhere in a program, indeed from many places in a program, and...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/\" \/>\n<meta property=\"og:site_name\" content=\"SPLat Controls\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-10T11:11:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2025\/04\/Subroutines.gif\" \/>\n\t<meta property=\"og:image:width\" content=\"312\" \/>\n\t<meta property=\"og:image:height\" content=\"265\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/gif\" \/>\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-what-are-they\/\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/\",\"name\":\"Subroutines: What are they? - SPLat Controls\",\"isPartOf\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2025\/04\/Subroutines.gif\",\"datePublished\":\"2025-04-18T07:44:48+00:00\",\"dateModified\":\"2025-06-10T11:11:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/#primaryimage\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2025\/04\/Subroutines.gif\",\"contentUrl\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2025\/04\/Subroutines.gif\",\"width\":312,\"height\":265},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/#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: What are they?\"}]},{\"@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: What are they? - 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: What are they? - SPLat Controls","og_description":"A subroutine is a block of code that can be invoked (&#8220;called&#8221;) from anywhere in a program, indeed from many places in a program, and...","og_url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/","og_site_name":"SPLat Controls","article_modified_time":"2025-06-10T11:11:18+00:00","og_image":[{"width":312,"height":265,"url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2025\/04\/Subroutines.gif","type":"image\/gif"}],"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-what-are-they\/","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/","name":"Subroutines: What are they? - SPLat Controls","isPartOf":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/#primaryimage"},"image":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/#primaryimage"},"thumbnailUrl":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2025\/04\/Subroutines.gif","datePublished":"2025-04-18T07:44:48+00:00","dateModified":"2025-06-10T11:11:18+00:00","breadcrumb":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/#primaryimage","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2025\/04\/Subroutines.gif","contentUrl":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2025\/04\/Subroutines.gif","width":312,"height":265},{"@type":"BreadcrumbList","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-what-are-they\/#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: What are they?"}]},{"@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\/3416","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=3416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}