OK, I am still kind of new to all of this, and I have looked in a book that I have to try to help me, but my code for a program is getting to be enormous. I would like to shrink it by creating subroutines. I would also like to know how to use them and how to give the subroutine variables to use and have them return others. Here is an example of code that I would like to turn into a subroutine.
{ $xachange = ($blocks[$blockcount][$icount][1] - $datapoint[($blocks[$b +lockcount][$icount][5])][1]); $xbchange = (($datapoint[($blocks[$blockcount][$icount][5] +)][1]) - ($blocks[$blockcount][($icount + 1)][1])); $yachange = ($blocks[$blockcount][$icount][2] - $datapoint +[($blocks[$blockcount][$icount][5])][2]); $ybchange = (($datapoint[($blocks[$blockcount][$icount][5] +)][2]) - ($blocks[$blockcount][($icount + 1)][2])); $da = sqrt(($xachange*$xachange) + ($yachange*$yachange)); $db = sqrt(($xbchange*$xbchange) + ($ybchange*$ybchange)); $nxa = (($blocks[$blockcount][$icount][1] + ($xachange/$da +)*$radius)); $nya = (($blocks[$blockcount][$icount][2] + ($yachange/$da +)*$radius)); $nxb = (($blocks[$blockcount][$icount][1] + ($xbchange/$db +)*$radius)); $nyb = (($blocks[$blockcount][$icount][2] + ($ybchange/$db +)*$radius)); $gxa1 = $nxa / 25.4; $gya1 = $nya / 25.4; $gxb1 = $nxb / 25.4; $gyb1 = $nyb / 25.4; $gpx = $datapoint[($blocks[$blockcount][$icount][5])][1] / + 25.4; $gpy = $datapoint[($blocks[$blockcount][$icount][5])][2] / + 25.4; select JOB; printf "G01X%.3fY%.3f\n", $gxa1, $gya1; printf "G03X%.3fY%.3fI%.3fJ%.3f\n", $gxb1, $gyb1, $gpx, $g +py; select STDOUT; print " 1 [4] G033333333333333333 $blockcount, $icount, 1 +\n"; $blocks[$blockcount][$icount][3] = 1; $icount = $icount + 1; }
Any help would be greatly appreciated, even a small subroutine, where I have to give it global variables, it uses them by naming them different local variables and does calculations, then gives return variables. Thanks

In reply to subroutine help by stu96art

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.