Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

(Subroutine) Arguments explained (sort of.)

by Andrew_Levenson (Hermit)
on Dec 12, 2006 at 20:06 UTC ( #589388=perltutorial: print w/ replies, xml ) 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.
use strict; #always and forever, amen. use warnings; #or else. my $argument=$ARGV[0]; #@ARGV is an array containing the arguments supplied. We're taking the + first value. print "\n$argument"; #This here will show you exactly what has been put into $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:
C:>"C:\foo.pl" arg
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,
$bar='Hello,'; $baz=' world!'; foo($bar, $baz); #calls the foo subroutine with $bar and $baz as arguments sub foo { print for @_; #will first print out the contents of $bar, then $baz because #those are the arguments given. }

it would be read as "Print $bar, then print $baz";

Arguments don't always have to be variables, though. For instance,
foo("square", 3); sub foo{ my ($shape, $size) = @_; }
This sets $shape to "square" and $size to 3, because they are given as the arguments for the subroutine.
I hope that helps!

Comment on (Subroutine) Arguments explained (sort of.)
Select or Download Code
Re: Arguments explained (sort of.)
by brusimm (Pilgrim) on Dec 21, 2006 at 20:21 UTC
    Thank you Andrew.

    The simpleness of your explanation does make it a more graspable concept to ponder. I think a node should be created for those of us who are new to Perl and semi new to programming and need things spelled out more verbosely.

    Everyone here is great, but they come from a very knowledgeable perspective that doesn't understand, not understanding..

    I wonder who we should ask? But again, thank you for your perspective.

      Glad to know that I am not the only one.
      Perhaps there should be a "Seekers of Common Knowledge" section...
      C(qw/74 97 104 112/);sub C{while(@_){$c**=$C;print (map{chr($C!=$c?shift:pop)}$_),$C+=@_%2!=1?1:0}}
Reaped: Re: (Subroutine) Arguments explained (sort of.)
by NodeReaper (Curate) on Jul 25, 2008 at 23:46 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://589388]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-11-26 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (174 votes), past polls