in reply to Passing an array to a subroutine help?!
Also, you really should use strict to avoid unintended errors that perl will help you find.my $min = minimum(@values); my $max = maximum(@values); my $average = average(@values);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Passing an array to a subroutine help?!
by Douglas0028 (Novice) on Mar 15, 2011 at 20:15 UTC | |
I'm not exactly sure what you mean. Sorry for being a pain, but what does my $min = minimum(@values); do exactly? I really want to understand exactly how everything works. I also should've mentioned that the calculations must be performed by the subroutines for the assignment. I say this because I'm not sure what you meant, and I don't know if the first three lines of code you posted are doing the calculations or not. Thank you so much for your reply though! This has been driving me insane. | [reply] [d/l] |
by wind (Priest) on Mar 15, 2011 at 20:28 UTC | |
Each of your subroutines returns a value. You must capture this returned value if you want to display the results in the following print statement. Here is your code with the small additions pointed out above
Note: Code run through Perl::Tidy to clean up the formatting | [reply] [d/l] |
by Douglas0028 (Novice) on Mar 15, 2011 at 20:51 UTC | |
Thank you again for your replies. My code now looks as follows...and works...
I'm elated that it works now, but I'm still not quite understanding how part of it works (which is just as important to me as having it work). We were taught in class to call subroutines by the method I have shown above which is commented out. I'm especially confused by this, because for the "Average" subroutine, I figured I would need to pass "@values" as well as "$arraysize" for it to work, but it seems to work without "$arraysize". $arraysize isn't a global variable in the program because I used my $arraysize, right? So how can the subroutine still work without passing it to that? I feel like there's some simple concept here that I am just not able to grasp or something, and it's extremely aggravating. Another question I have now, is if the subroutines return $min $max $average, why do I need those three lines that set those variables to something again? Shouldn't they just work in the print statements after they're returned from the subroutines? | [reply] [d/l] [select] |
by chromatic (Archbishop) on Mar 15, 2011 at 21:11 UTC | |
We were taught in class to call subroutines by the method I have shown above which is commented out. That's a shame; your instructor is doing you and your colleagues a disservice. That syntax has unfortunate side effects. $arraysize isn't a global variable in the program because I used my $arraysize, right? $arraysize is a lexical variable due to your use of my, but it is visible to the whole program because its lexical scope is the file itself. It's effectively global, even though it's not a package variable. Shouldn't they just work in the print statements after they're returned from the subroutines? The values returned from the functions (I use these words very carefully) come from lexical variables with smaller scopes—the scope of the functions themselves:
There are two lexical variables named $max in this code. Within the function, all references to $max refer to the innermost variable. If it didn't declare $max as a lexical within the function, all references to $max would refer to the outer (file-scoped) $max. You're correct that you wouldn't have to use return explicitly in that case... ... but when you do treat your functions as black boxes which take inputs and return results and do not modify external variables, you tend to end up with better code. You can be certain that your functions do not modify any external variables on which other functions rely, which makes it much easier to write larger and more robust programs. If you'd like to learn more details about lexical scoping and functions and ways to write reliable Perl 5 code, my book Modern Perl is available in several electronic versions for free. It might be a good supplement for your class. | [reply] [d/l] [select] |
by Corion (Patriarch) on Mar 15, 2011 at 21:00 UTC | |
The variable $min in your main program has nothing to do with the variable $min in the subroutine sub min {...}. Also see Coping With Scoping. | [reply] [d/l] [select] |