Your question has two answers, cause you're really asking two questions.

The first question is "Why do we use subroutines?"

The answer to that is twofold, both relating to putting a code snippet that's used more than once in one place.

  1. You know that if you want to do XYZ, it's always going to be the same way each time you do it.
  2. Modifying the code that does XYZ is done in one place. If you have this code in 5 places, will you remember every place to modify it a year from now?
Your second question is "Why would we pass values to a subroutine?"

This has to do with a few core programming concepts, all of them related. The basic idea is that you want a piece of code to do what it does, without as litle relationship to the rest of the code in your program. Ideally, what you can do is to take a subroutine out of one program and just "plug it in" to another program, knowing exactly how it's going to work. And, more importantly, knowing that it will not affect any other part of your program. This allows you to have confidence in your program.

Passing variables in means that you can stop using global variables. If you're curious as to why globals are (almost always) bad, ask and a bunch of people would be glad to help you understand. :)

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


In reply to Re: What is the purpose of subroutine parameters? by dragonchild
in thread What is the purpose of subroutine parameters? by chip1232

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.