chip1232 has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow perl monks. I'm trying to lern perl but i'm getting stuck on subroutine parameters. What are they fo? I'm pretty sure i understand what they are but i can't figure out why anyone would need them. Thanks alot!

Edit kudra, 2001-09-17 Changed title

  • Comment on What is the purpose of subroutine parameters?

Replies are listed 'Best First'.
(crazyinsomniac) Re: Use of perl
by crazyinsomniac (Prior) on Sep 17, 2001 at 04:04 UTC
    Hmm, lets see...

    You heard of perl's built-in functions, like print, printf, sprintf, split, substr.... right? (hopefully you've seen at least one of 'em).

    What do they do? -- take a bunch of "parameters" and do stuff with them, whether it be "write" to some kind of "filehandle" or manipulate in some way.

    Now do you see what "parameters" are for? (if you got a function that does something, would you rather call that function 5 times with different parameters, or write the code 5 different times)

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Re: Use of perl
by TStanley (Canon) on Sep 17, 2001 at 04:03 UTC
    This is to provide you with a fairly basic example. Lets say you have a sub routine that adds two number together and gives you the result of that operation. You would pass two numbers (your parameters, or arguements as they are called), the sub would then add them and return the result to your main section.
    #!/usr/bin/perl -w use strict; my $first_number=5; my $second_number=1; my $result=Add($first_number,$second_number); print"And the result is $result.\n"; sub Add{ my($x,$y)=shift; my $result=$x+$y; return $result; }
    The reason you use sub-routines(also known as functions) is to avoid typing code over and over again. This way, if you have to perform a specific operation over and over again on constantly changing data, you can simply send the data to the subroutine, it will do the operation on it, and return the result to you.

    TStanley
    --------
    There's an infinite number of monkeys outside who want to talk to us
    about this script for Hamlet they've worked out
    -- Douglas Adams/Hitchhiker's Guide to the Galaxy
      You mean my($x,$y)=(shift,shift);
Re: What is the purpose of subroutine parameters?
by dragonchild (Archbishop) on Sep 17, 2001 at 16:49 UTC
    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.

      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.