(Subroutine) Arguments explained (sort of.)by Andrew_Levenson (Hermit)
|on Dec 12, 2006 at 20:06 UTC||Need Help??|
For someone entirely new to programming, what an "argument" is, exactly, may not be perfectly clear. I know it wasn't for me. I'd get plenty of responses about how it reads the "command line argument" or it "passes the argument on...," which meant nothing to me, but when I asked for further clarification, I was beyond help because nobody understands that one can not know what an argument is.
For those who, like me, just sadly could not pick up what an argument is and why, here is an abstract, in laymen's terms!
An argument is, essentially, data supplied to the script ( or sub routine, or...) to fulfill parameters that need to be met. Okay, not so easy to explain. Perhaps an example will clarify.
This program is going to print out a letter or word supplied to it, but instead of inputting the word/letter, we're going to supply it as a command line argument.
If you run that, nothing happens. That's because you have supplied no arguments, so $argument is empty. Now try running it again, but when you go to open it in the command prompt, follow the program name with a letter or word, as such:
Now, if all goes well and I didn't just make a fool out of myself, the program should print "arg," because "arg" was supplied as the first command line argument. Make sense?
Additionally (and I may be going out on a limb to assume that anyone else is as dense as I am), have you ever noticed that when you call a subroutine, you call it as foo();, not foo;? That's because inside of those parentheses, you're supposed to supply your arguments for the subroutine (fed into the temporary array, @_)! It's like a mini-script, I guess. Sort of. In a way.
So for this,
it would be read as "Print $bar, then print $baz";
Arguments don't always have to be variables, though. For instance,
This sets $shape to "square" and $size to 3, because they are given as the arguments for the subroutine.
I hope that helps!