Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Raku (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Formal subroutine parameter lists=== Perl defines subroutines without [[parameter (computer science)|formal parameter]] lists at all (though simple parameter counting and some type checking can be done using Perl's "prototypes"). Subroutine arguments passed in are aliased into the elements of the array <code>@_</code>. If the elements of <code>@_</code> are modified, the changes are reflected in the original data. Raku introduces true formal parameters to the language.<ref name="syn6">{{cite web | url=https://design.raku.org/S06.html | title=Synopsis 6: Subroutines | author=Wall, Larry | date=2003-03-21 }}</ref> In Raku, a subroutine declaration looks like this: <syntaxhighlight lang="raku"> sub do_something(Str $thing, Int $other) { ... } </syntaxhighlight> As in Perl, the formal parameters (i.e., the variables in the parameter list) are aliases to the actual parameters (the values passed in), but by default, the aliases are [[constant (programming)|constant]] so they cannot be modified. They may be declared explicitly as read-write aliases for the original value or as copies using the <code>is rw</code> or <code>is copy</code> directives respectively should the programmer require them to be modified locally. ====Parameter passing modes==== Raku provides three basic modes of parameter passing: positional parameters, [[named parameter]]s, and slurpy parameters. Positional parameters are the typical ordered list of parameters that most programming languages use. All parameters may also be passed by using their name in an unordered way. Named-only parameters (indicated by a <code>:</code> before the parameter name) can only be passed by specifying its name, i.e. it never captures a positional argument. Slurpy parameters (indicated by an <code>*</code> before the parameter name) are Raku's tool for creating [[variadic function]]s. A slurpy hash will capture remaining passed-by-name parameters, whereas a slurpy array will capture remaining passed-by-position parameters. Here is an example of the use of all three parameter-passing modes: <syntaxhighlight lang="raku"> sub somefunction($a, $b, :$c, :$d, *@e) { ... } somefunction(1, 2, :d(3), 4, 5, 6); # $a=1, $b=2, $d=3, @e=(4,5,6) </syntaxhighlight> Positional parameters, such as those used above, are always required unless followed by <code>?</code> to indicate that they are optional. Named parameters are optional by default, but may be marked as required by adding <code>!</code> after the variable name. Slurpy parameters are ''always'' optional. ====Blocks and closures==== Parameters can also be passed to arbitrary blocks, which act as [[Closure (computer science)|closures]]. This is how, for example, <code>for</code> and <code>while</code> loop iterators are named. In the following example, a list is traversed, 3 elements at a time, and passed to the loop's block as the variables, <code>$a, $b, $c</code>.<ref name="syn04">{{cite web | url=https://design.raku.org/S04.html | title=Synopsis 4: Blocks and Statements | author=Wall, Larry | date=2009-05-20 }}</ref> <syntaxhighlight lang="raku"> for @list -> $a, $b, $c { ... } </syntaxhighlight> This is generally referred to as a "pointy sub" or "pointy block", and the arrow behaves almost exactly like the <code>sub</code> keyword, introducing an anonymous closure (or anonymous subroutine in Perl terminology).<ref name="syn6"/>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)