On Tue, Jan 21, 2020 at 07:16:17PM -0800, Todd Chester via perl6-users wrote: > > > On 2020-01-21 18:57, Tom Browder wrote: > > On Tue, Jan 21, 2020 at 18:34 Todd Chester via perl6-users > > <[email protected] <mailto:[email protected]>> wrote: > > > > On 2020-01-21 16:09, Todd Chester via perl6-users wrote: > > >> 4) A block (that is the { ... } bit) will always 'return' the last > > >> expression evaluated. > > > > > > Seems to me I have see the last expression returned even without > > > the {...}. Maybe I am misremembering. > > > > > > Todd, the {} is the block defining the subroutine. > > > > > sub AplusB( $a, $b --> Int ){$a+$b;} > > > > > > The above is the sub's definition. > > > > > > > > &AplusB > > > > > > The above should generate an error because the mandatory args are > > missing, depending on the context (don't quote me on that). > > &AplusB is printer out by REPL. I don't know why. REPL > does that a lot.
The REPL tells you that the result of the expression you entered (the
sequence of characters "sub AplusB......{$a+$b}") was to define
a subroutine called "AplusB", which is what you wanted it to do.
The "&" sigil (a sigil is basically the character before a thing's name in
Raku and Perl 5) means that the name "AplusB" refers to a subroutine,
not a variable or anything else.
> > > AplusB 2, 3
> > 5
> >
> > The above shows the sub being called with the required two args, and the
> > 5 is the returned value which is probably shown in the REPL since there
> > is no semicolon after the call and the 5 is not usually seen otherwise.
>
> $ p6 'sub AplusB( $a, $b --> Int ){$a+$b}; say AplusB 2, 3;'
> 5
>
>
> > -Tom
>
> Hi Tom,
>
> I was trying to cut things down to the simplest terms.
> I was responding to Richard's statement
>
> >> 4) A block (that is the { ... } bit) will always 'return' the last
> >> expression evaluated.
>
> I created a sub without the "{...}" and showed where it would
> return the last equation without the "{...}". I may have
> misunderstood Richard.
You created a sub with "$a+$b" in a "{...}" block. The "{$a+$b}"
sequence of characters in what you typed is the "{...}" block that
Richard is referring to - a block that starts with a "{" character, ends
with a "}" character and contains a series of Raku expressions
separated by semicolons.
In your case the block only contains a single expression, "$a+$b", which
represents the sum of the two arguments to the subroutine - this is
*exactly* what Richard meant. If you were trying to illustrate his point
that a block without an explicit "return" statement would return
the last expression, you succeeded. If you were trying to demonstrate
something that does not have a "{...}", well, nope, "{$a+$b}" is exactly
the "{...}" that he was talking about, so the fact that AplusB returns
the sum of $a and $b is exactly what he meant.
G'luck,
Peter
--
Peter Pentchev roam@{ringlet.net,debian.org,FreeBSD.org} [email protected]
PGP key: http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint 2EE7 A7A5 17FC 124C F115 C354 651E EFB0 2527 DF13
signature.asc
Description: PGP signature
