|more useful options|
Re: Re: subroutine syntaxby quidity (Pilgrim)
|on Dec 11, 2000 at 00:40 UTC||Need Help??|
So, As I understand it, I don't have to pass a $variable into a subroutine call before i use the $variable inside the sub?
No, you don't have to pass the variable as an argument to the subroutine, but you will write clearer code if you do. By passing values, you know which values will affect the outcome of the subroutine without having to look through the code for the sub.
Also, does the $variable get modified globally if it is called directly inside the subroutines and modified?
Yes, anywhere that you modify a variable, you will modify the variable (with the exception of localised variables, where it gets a bit more complicated). You can still modify the variable sent to a sub, as @_ becomes an alias to the variables themselves so that:$dog = 'spot';
Is exactly the same as:
You can make a copy of the variable passed to the subroutine, so that changes do not propagate outside the sub so that code like this is safe:
Thereason why you want to pass variables to subroutines as arguments, rather than by using global variables is that if you have a bug in your code, then using global variables means that the bug could be caused by any of the many lines in your program. If you pass variables as arguments, then the bug could only be caused by the lines which call the subroutine, or the subroutine itself.