{"id":3419,"date":"2025-04-18T07:47:31","date_gmt":"2025-04-18T07:47:31","guid":{"rendered":"https:\/\/webprojects.cloud\/wordpress\/splatco\/?post_type=spl_knowledgebase&#038;p=3419"},"modified":"2025-06-10T11:11:40","modified_gmt":"2025-06-10T11:11:40","slug":"subroutines-the-subroutine-stack","status":"publish","type":"spl_knowledgebase","link":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/","title":{"rendered":"Subroutines: The subroutine stack"},"content":{"rendered":"\n<p>Subroutines are made possible by a mechanism called a subroutine stack. The subroutine stack is a &#8220;Last-in First-out&#8221; memory. This memory stores program memory addresses. When\u00a0<a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/glossary-of-terms\/#o3807\">SPLatty<\/a>\u00a0executes a\u00a0<code>GoSub<\/code>\u00a0he notes where he is, and sticks the note on top of the stack, before jumping to the new place in the program. If inside that subroutine there is another\u00a0<code>Gosub<\/code>, he again notes where he is, places that note on top of the previous one, and goes to the new place. When he encounters a\u00a0<code>Return<\/code>\u00a0he removes the top note off the stack of notes and goes to the place designated on the note. That way he can &#8220;unwind&#8221; the places he has to return to perfectly.<\/p>\n\n\n\n<p>The stack of return addresses, the subroutine stack, can only contain a limited number of addresses. For dialects prior to dialect 12 the capacity is 4. From dialect 12 the capacity is 6.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Things you shouldn&#8217;t do (with subroutines, that is)<\/h6>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Don&#8217;t do a\u00a0<code>GoTo<\/code>\u00a0to get out of a subroutine. This is one of the most common &#8220;mystery&#8221; mistakes I see people making. If you\u00a0<code>GoSub<\/code>\u00a0you\u00a0<em>must\u00a0<\/em><code>Return<\/code>.<\/li>\n\n\n\n<li>Don&#8217;t do a\u00a0<code>GoTo<\/code>\u00a0into a subroutine. The trap here is that people sometimes accidentally write\u00a0<code>GoTo<\/code>\u00a0when they meant\u00a0<code>GoSub<\/code>.<\/li>\n\n\n\n<li>Don&#8217;t forget the\u00a0<code>Return<\/code>\u00a0instruction (that&#8217;s the one I do). If I have a really confounding bug I search for missing\u00a0<code>Return<\/code>s. It is really hard to see something that&#8217;s not there!<\/li>\n\n\n\n<li>You can use subroutines within\u00a0<a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-multitrack\/\">MultiTrack<\/a>\u00a0tasks. However, you may not yield from inside a subroutine. That means\u00a0<code><a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/multitrack-instructions\/yieldtask-d16\/\">YieldTask<\/a>, <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/fasttrack-instructions\/waiton-ii\/\">WaitOn<\/a>, <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/timing-instructions\/pause-tttt\/\">Pause<\/a><\/code>\u00a0and related yielding instructions are not allowed inside such subroutines. SPLat\/PC will flag an error and the controller will suffer a fatal runtime error if you violate this rule.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Subroutines are made possible by a mechanism called a subroutine stack. The subroutine stack is a &#8220;Last-in First-out&#8221; memory. This memory stores program memory addresses&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3414,"menu_order":2,"template":"","class_list":["post-3419","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: The subroutine stack - 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: The subroutine stack - SPLat Controls\" \/>\n<meta property=\"og:description\" content=\"Subroutines are made possible by a mechanism called a subroutine stack. The subroutine stack is a &#8220;Last-in First-out&#8221; memory. This memory stores program memory addresses....\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/\" \/>\n<meta property=\"og:site_name\" content=\"SPLat Controls\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-10T11:11:40+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=\"1 minute\" \/>\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-the-subroutine-stack\/\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/\",\"name\":\"Subroutines: The subroutine stack - SPLat Controls\",\"isPartOf\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website\"},\"datePublished\":\"2025-04-18T07:47:31+00:00\",\"dateModified\":\"2025-06-10T11:11:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/#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: The subroutine stack\"}]},{\"@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: The subroutine stack - 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: The subroutine stack - SPLat Controls","og_description":"Subroutines are made possible by a mechanism called a subroutine stack. The subroutine stack is a &#8220;Last-in First-out&#8221; memory. This memory stores program memory addresses....","og_url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/","og_site_name":"SPLat Controls","article_modified_time":"2025-06-10T11:11:40+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/","name":"Subroutines: The subroutine stack - SPLat Controls","isPartOf":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website"},"datePublished":"2025-04-18T07:47:31+00:00","dateModified":"2025-06-10T11:11:40+00:00","breadcrumb":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-subroutines\/subroutines-the-subroutine-stack\/#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: The subroutine stack"}]},{"@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\/3419","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=3419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}