{"id":7319,"date":"2014-12-26T00:15:53","date_gmt":"2014-12-26T00:15:53","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=7319"},"modified":"2015-08-23T18:25:18","modified_gmt":"2015-08-23T18:25:18","slug":"perl-subroutines","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-subroutines\/","title":{"rendered":"Perl:  subroutines"},"content":{"rendered":"<h1>The tutor continues about programming with Perl. \u00a0Tutoring computer science, subroutines have both theoretical and practical importance.<\/h1>\n<p>&nbsp;<br \/>\nBack in my <a href=\"?p=6395\">November 14 post<\/a> I opened up the discussion on subroutines.  In computer science, a subroutine is a self-contained body of code that performs a specific task.<\/p>\n<p>Today, we&#8217;ll look at a simple example:  a subroutine that calculates the final price of merchandise with a price, discount, and tax rate given by the user.  The subroutine itself resides at the bottom of the program, but is called earlier.<\/p>\n<p><span style=\"font-family:monospace;color:brown\">#!\/usr\/bin\/perl<br \/>\nprint &#8220;Hello.  Welcome to the final price calculator.\\n&#8221;;<br \/>\nprint  &#8220;Enter the sticker price of the merchandise, please.&#8221;;<br \/>\n$price=&lt;STDIN&gt;;<br \/>\nprint &#8220;Enter the discount percentage, if there is one.\\n&#8221;;<br \/>\nprint &#8220;For example, 25 means 25 percent off.&#8221;;<br \/>\n$discount=&lt;STDIN&gt;;<br \/>\nif(!$discount){<br \/>\n$discount=0;<br \/>\n}<br \/>\nprint &#8220;Now, enter the tax percentage; eg., 12 means 12 percent.&#8221;;<br \/>\n$taxrate=&lt;STDIN&gt;;<br \/>\n&#038;finalprice($price,$discount,$taxrate);<br \/>&#10;<br \/>\nsub finalprice{<br \/>\n$finprice=$_[0]*(1-$_[1]\/100)*(1+$_[2]\/100);<br \/>\nprint &#8220;Sticker price is $_[0]&#8221;;<br \/>\nprint &#8220;Discount is $_[1]&#8221;;<br \/>\nprint &#8220;Tax rate is $_[2]\\n&#8221;;<br \/>\nprint &#8220;The final price at the till should be $finprice\\n\\n&#8221;;<br \/>\n}<\/span><\/p>\n<p>Note the subroutine definition starts with <span style=\"font-family:monospace;color:brown\">sub<\/span>.  A subroutine defined as <span style=\"font-family:monospace;color:brown\">sub bob1<\/span> would be invoked with the call <span style=\"font-family:monospace;color:brown\">&#038;bob1(parameter list)<\/span>.<\/p>\n<p>For Christmas Day, this is probably enough. I will be discussing the issue of parameters in a coming post.<\/p>\n<p>To all my readers:  Happy Holidays.  As with every post, I hope this one finds you in good spirits:)<\/p>\n<p><em>Source:<\/em><\/p>\n<p>McGrath, Mark.  <em>Perl in easy steps.<\/em>  Southam:  Computer Step, 2004.<\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tutor continues about programming with Perl. \u00a0Tutoring computer science, subroutines have both theoretical and practical importance. &nbsp; Back in my November 14 post I opened up the discussion on subroutines. In computer science, a subroutine is a self-contained body &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-subroutines\/\"> <span class=\"screen-reader-text\">Perl:  subroutines<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[173,291],"class_list":["post-7319","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-perl-programming","tag-subroutines"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/7319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=7319"}],"version-history":[{"count":23,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/7319\/revisions"}],"predecessor-version":[{"id":11939,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/7319\/revisions\/11939"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=7319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=7319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=7319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}