Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why pass variables to subs if you can get at them anyway? 1

Status
Not open for further replies.

MatthewP

Programmer
Jan 16, 2001
176
0
0
GB
If I call a sub I can get at all the other values in my script anyway, and I've never bothered passing variables to them. But looking through various posts to this group, and in various other places, a lot of people tend to pass the variables the sub needs to it, and pick them up with @_ - even though they're available anyway.

Does this actually do anything useful - like speed up the program perhaps? Or is it just efficient use of memory, or 'traditional' coding.. because I've never done it and just rely on the fact that I know what values I've got in the rest of the program. Am I missing out on something?

Cheers,
Matt.
 
No it doesn't speed up the program. Subroutines basically exist to allow you to write a chunk of code for a specific purpose - so that that chunk of code can be re-used(or called) when that purpose is needed again.

If you want to create true re-usable code segments, then you'll eventually get into creating perl "modules" - read up on packages and modules in any Perl books you have, or by doing "perldoc perlmod" at a command prompt.

The way you are writing your subroutines is fine, except that if you write a subroutine that could be used in other scripts, then for you to be able to use that subroutine in the other script, you'll have to duplicate the code in each other script that you want that subroutine in.

If you instead write subroutines to be self-contained, that is to accept a set of defined(by you) input parameters, and return a defined(by you) set of output parameters, then you can plunk that subroutine into a module, and just "use" that module anytime you need access to that subroutine.

Here's a simple subroutine example to illustrate the point:

sub add_2_numbers {
my $num1 = shift; ### shift off 1st parameter from @_
my $num2 = shift; ### shift off 2nd parameter from @_
my $sum = $num1 + $num2;
return $sum;
} ### end sub add_2_numbers


and then a call to that subroutine might look like

my $summ = add_2_numbers($first_number, $second_number);

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Thanks - actually I do write modules and pass the variables there.. I was just wondering why everyone else seems to always pass variables regardless - especially in books. I was starting to think I was missing out on some important feature or something.

Thanks!
Matt.
 
No important feature that I'm aware of. I guess it's a style thing - I now write all of my subroutines to accept in specific parameters and return specific parameters. Your way is fine too.
Hardy Merrill
Mission Critical Linux, Inc.
 
The subroutine call with parameters eliminates a capability of not deliberate change of a variable. Such change can result in an error, specially with large volume of code.
I like use two Perl pragmas:
Code:
use strict;
use vars qw($global_variable_1 $global_variable_2 $global_variable_n);
This pragmas is very useful for code systematization.
 
Hate to disagree with Hardy - but I think it *is* an important feature. If you don't pass your subroutines all of the information they need to do thier job you are not being anywhere *near* lazy enough....

If you pass everything as a param to a subroutine, you can pick up that sub and copy it into another program with no effort at all.

If you don't..... You'll spend all of your time wondering why your perfectly good sub doesn't work when you move it.

The idea is to be sure that a sub doesn't change *anything* except the variables you give it in the arg list. You cna then be sure it won't have any nasty side effects when you move it to a new home in another program. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top