Named parameters are an important part of the raku language.
In fact, named parameters are used in several other languages as well.
It's in your best interest to learn how they work in order to use the
language properly.

sub a (:$a) {}
          ^^^^  This is a named parameter.

You know it's a named parameter because of the preceding colon (:).
What this means is in order for caller to pass this parameter to this
subroutine, it's necessary for
the caller to call it using a named parameter as an argument.

Here are two examples that utilize the named parameter of the subroutine I
just created above:

######################

my $var = 'test';
a(:a($var));
   ^^^^^^^^ The named parameter

######################

my $a = 'test';
a(:$a));
   ^^^^ The named parameter

######################

In the first example, the value I wanted to pass to the named parameter was
stored in '$var'.
The syntax for passing this value to the named parameter as defined in the
subroutine is :a($var) ...
This is saying, pass the value stored in $var to the named parameter that
the subroutine expects as $a.

In the second example, the value I wanted to pass to the named parameter
was stored in '$a'.
The syntax for passing this value to the named parameter as defined in the
subroutine is :a($a) .... or if you like shorthand  :$a
This is saying, pass the value stored in $a to the named parameter that the
subroutine expects as $a.

What you are accustomed to are positional parameters like the following:

sub a($a, $b, $c) {}

Positional parameters have to be passed in order ...

a(1,2,3);

Named parameters have a feature that order doesn't matter:

sub a(:$a, :$b, :$c) {}

a(:c(1), :a(0), :b(3));

This is advantageous in certain cases.  One such example is passing a bunch
of booleans though it's not limited to booleans as I've demonstrated in my
original post.
Anyways, learn named parameters.  It's essential to the raku programming
language.


On Sat, Feb 8, 2020 at 12:52 AM ToddAndMargo via perl6-users <
perl6-us...@perl.org> wrote:

> >> On Thu, Feb 6, 2020 at 11:43 AM ToddAndMargo via perl6-users
> >> <perl6-us...@perl.org <mailto:perl6-us...@perl.org>> wrote:
> >>
> >>     On 2020-02-05 20:12, Paul Procacci wrote:
> >>      > I wasn't going to follow up but decided to do so since there is a
> >>     small
> >>      > but subtle bug in my original post.
> >>      > I wouldn't want to mislead you Todd.
> >>      >
> >>      > The \d has been changed to [0..9] as the expected input would
> >>     only ever
> >>      > be in that range.  (\d includes Unicode Characters)
> >>      > I've also included an alignment parameter (shadow'ing the sub
> >>     written by
> >>      > you Todd).
> >>      >
> >>      > sub sbprint( Int $n, Int :$alignment = 8) {
> >>      >          my Int $padding = $alignment + $n.msb - ( $n.msb +& (
> >>      > $alignment  - 1 ) );
> >>      >          '0b' ~ "\%0{$padding}b".sprintf($n).comb(/<[0..9]> ** {
> >>      > $alignment }/).join('_')
> >>      > }
> >>      >
> >>      > say sbprint 0x04F842;
> >>      > say sbprint 0x04F842, :alignment(4);
> >>      >
> >>      > # ./test.pl6
> >>      > 0b00000100_11111000_01000010
> >>      > 0b0100_1111_1000_0100_0010
> >>
> >>
> >>     `Int :$alignment = 8` was inspired!
> >>
> >>     What does the ":" do before `alignment`?
> >>
>
> On 2020-02-06 08:52, Paul Procacci wrote:
> > The subroutine I wrote defines a named parameter that goes by the name
> > alignment 'Int :$alignment'.
> > If the caller wants to call the callee using the given named parameter
> > there are several ways to so do ....  hence the ':alignment(4)'.
> >
> > If instead you have a variable already defined you can instead pass that
> > variable as a named parameter via something like the following:
> >
> > $alignment = 4;
> > sbprint 0x04F842, :$alignment;
> >
> >
> > The documentation goes into much more detail about named arguments.
> >
> > https://docs.raku.org/language/functions#Arguments
> >
>
> Sorry, went right over my head.  Thank you for trying anyway.
>


-- 
__________________

:(){ :|:& };:

Reply via email to