{"id":842,"date":"2017-09-21T03:11:24","date_gmt":"2017-09-21T03:11:24","guid":{"rendered":"http:\/\/people.utm.my\/kohmenghock\/?page_id=842"},"modified":"2017-09-21T03:23:51","modified_gmt":"2017-09-21T03:23:51","slug":"codeblocks-simple-guide-using-subroutines-ii","status":"publish","type":"page","link":"https:\/\/people.utm.my\/kohmenghock\/codeblocks-simple-guide-using-subroutines-ii\/","title":{"rendered":"Code::Blocks simple guide &#8211; Using subroutines (II)"},"content":{"rendered":"<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nIn the <a href=\"https:\/\/people.utm.my\/kohmenghock\/codeblocks-simple-guide-using-subroutines-i\/\" target=\"_blank\" rel=\"noopener\">previous note<\/a>, I have mentioned that the overall structure of a subroutine is similar to the main program. The only difference is at the first line appearing in the subroutine program. In this discussion, we will look at a simple example on how subroutines are used.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nSay that we want to calculate kinetic energy and momentum of an object. To do this, we need two input values namely the mass and velocity. We shall ask the user to provide these values by using the <strong>print<\/strong> and <strong>read<\/strong> command as shown below.<\/span><\/p>\n<p><a href=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/09\/Input_mass_and_velocity.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-843 size-full\" src=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/09\/Input_mass_and_velocity.png\" alt=\"\" width=\"799\" height=\"396\" srcset=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/09\/Input_mass_and_velocity.png 799w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/09\/Input_mass_and_velocity-300x149.png 300w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/09\/Input_mass_and_velocity-768x381.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nWe can then proceed to write the subroutine programs, one for kinetic energy calculation and the other for momentum. For the kinetic energy calculation, we need two input and one output variables. The input variables are mass (variable named <strong>mass<\/strong>) and velocity (variable named <strong>vel<\/strong>) while the output variable is the kinetic energy (variable named kinetic). The subroutines to calculate kinetic energy and momentum are shown below.<br \/>\n<strong>Note to remember<\/strong>: Subroutines are written after the <span style=\"color: #ff0000\">end<\/span> statement of the main program!<br \/>\n<\/span><\/p>\n<p><a href=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/09\/subroutines_kinetic_and_momentum.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-844 size-full\" src=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/09\/subroutines_kinetic_and_momentum.png\" alt=\"\" width=\"662\" height=\"530\" srcset=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/09\/subroutines_kinetic_and_momentum.png 662w, https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/09\/subroutines_kinetic_and_momentum-300x240.png 300w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/a><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nIn the <strong>main program<\/strong>, we call these subroutines by using the command<\/span><\/p>\n<p style=\"text-align: center\"><strong><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\">call [name of subroutine](variable 1, variable 2, variable 3, &#8230;)<\/span><\/strong><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nIn the current example, I used a vivid name for the subroutine namely <strong>calc_kinetic<\/strong> and <strong>calc_momentum<\/strong>. The command to call the subroutine in the main program are then<\/span><\/p>\n<p style=\"text-align: center\"><strong><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\">call calc_kinetic(mass,vel,kinetic)<\/span><\/strong><\/p>\n<p style=\"text-align: center\"><strong><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\">call calc_momentum(mass,vel,momentum)<\/span><\/strong><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 12pt;font-family: arial,helvetica,sans-serif\"><br \/>\nAfter the subroutines are executed, the results i.e. the output variables will be passed to the main program for further action. In this example, we will just print the results on the terminal using the formatted print statements. The codes can be found in the following <a href=\"https:\/\/people.utm.my\/kohmenghock\/wp-content\/uploads\/sites\/982\/2017\/09\/code_mometum_kinetic.pdf\" target=\"_blank\" rel=\"noopener\">file<\/a>.<br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous note, I have mentioned that the overall structure of a subroutine is similar to the main program. The only difference is at the first line appearing in the subroutine program. In this discussion, we will look at a simple example on how subroutines are used. Say that we want to calculate kinetic &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/people.utm.my\/kohmenghock\/codeblocks-simple-guide-using-subroutines-ii\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Code::Blocks simple guide &#8211; Using subroutines (II)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":14180,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-842","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages\/842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/users\/14180"}],"replies":[{"embeddable":true,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/comments?post=842"}],"version-history":[{"count":0,"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/pages\/842\/revisions"}],"wp:attachment":[{"href":"https:\/\/people.utm.my\/kohmenghock\/wp-json\/wp\/v2\/media?parent=842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}