in reply to What is the purpose of subroutine parameters?

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.

  • Comment on Re: What is the purpose of subroutine parameters?

Replies are listed 'Best First'.
Re: Re: What is the purpose of subroutine parameters?
by Anonymous Monk on Sep 17, 2001 at 17:35 UTC
    Thank you everyone for all your help. I completly understand it now. I'm having alot of trouble learning this though. I got the book learning perl by o rielly to start with. It's very frustrating. I'll keep trying though. Thanks again for all your help.