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

Re: Defining Subroutines

by Zaxo (Archbishop)
on Oct 24, 2002 at 03:02 UTC ( [id://207613]=note: print w/replies, xml ) Need Help??

in reply to Defining Subroutines

You got it backwards. Define a function with sub. You can call it with &max, but it's better to call using parentheses until you're comfortable with what the ampersand does. It causes prototypes to be ignored, and it arranges for the function to be called with the existing @_ as arguments.

After Compline,

Replies are listed 'Best First'.
Re: Re: Defining Subroutines
by rir (Vicar) on Oct 24, 2002 at 04:11 UTC
    Not quite, using the ampersand causes the routine call to use the existing @_ as the default arguments. If you provide arguments they are used instead.
    sub z { my $i =1; foreach (@_) { print "$i : $_\n"; $i++; } } sub x { &z; # 1 &z( "good", "bye"); &z; # same as one again &z(); # called with empty list } x( "say", "hello");
Re^2 Defining Subroutines
by @rocks (Scribe) on Oct 24, 2002 at 04:40 UTC
    Ok Zaxo, Thank you very much for the information. I got it now, thank you.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://207613]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-07-16 07:16 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.