为什么我的 for 循环是非法声明

发布于 2024-12-19 11:12:54 字数 831 浏览 0 评论 0原文

我创建了两个子程序,一个用于执行斐波那契数列,另一个用于测试偶数。当我调用它时,它说我的 for 循环在第 7 行中的 sub Fibonacci 是非法的,为什么?

#!/usr/bin/perl
use strict;
use warnings;


my ($x,$y);
my $num = 0;

sub Fibs($start,$stop){
    for ($start..$stop){
        ($x, $y) = ($y, $x+$y);
            my $total += $y;
        }
    print "$total \n"
}

sub even($num){
    if ($num % 2 == 0){
        return $num;}
}

my $big_total = Fibs(even($num), 3999999)

根据以下建议进行编辑。

显然我错过了一些东西。从反馈更新到新版本。

#!/usr/bin/perl
use strict;
use warnings;


my ($x,$y);
my $num = 0;

sub Fibs{
    my ($start, $stop) = @_ ;
    for ($start..$stop){
        my ($x, $y) = (0,2);
            if ($x % 2 == 0){
                ($x, $y) = ($y, $x+$y);
                    my $total += $y;
        }
}

my $big_total = Fibs(0, 3999999)

I created two subs one to do Fibonacci and the other to test even numbers. When I call it though it is saying my for loop in line 7 the sub Fibonacci is illegal why?

#!/usr/bin/perl
use strict;
use warnings;


my ($x,$y);
my $num = 0;

sub Fibs($start,$stop){
    for ($start..$stop){
        ($x, $y) = ($y, $x+$y);
            my $total += $y;
        }
    print "$total \n"
}

sub even($num){
    if ($num % 2 == 0){
        return $num;}
}

my $big_total = Fibs(even($num), 3999999)

Edited from suggestions below.

Clearly I am missing something. From feedback updated to new version.

#!/usr/bin/perl
use strict;
use warnings;


my ($x,$y);
my $num = 0;

sub Fibs{
    my ($start, $stop) = @_ ;
    for ($start..$stop){
        my ($x, $y) = (0,2);
            if ($x % 2 == 0){
                ($x, $y) = ($y, $x+$y);
                    my $total += $y;
        }
}

my $big_total = Fibs(0, 3999999)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琴流音 2024-12-26 11:12:54

除了缺少左大括号之外,Perl 不支持这种子例程参数声明。

而不是

sub Fibs($start, $stop) {
    ...
}

您需要编写类似的内容:

sub Fibs {
    my($start, $stop) = @_;
    ...
}

(Perl 确实有原型,但它们并不是真正用于声明参数的类型,并且它们不提供名称。请参阅 这篇文章进行讨论。)

其他问题:

您应该添加

use strict;
use warnings;

You never use the $x$y< /code> 您在外部声明的 范围。

您的 even 函数似乎不完整。如果它的参数是奇数,它不会(显式)返回值。它到底想做什么?

In addition to the missing opening braces, Perl doesn't support that kind of declaration for subroutine parameters.

Rather than

sub Fibs($start, $stop) {
    ...
}

you need to write something like:

sub Fibs {
    my($start, $stop) = @_;
    ...
}

(Perl does have prototypes, but they're not really intended for declaring the types of parameters, and they don't provide names. See this article for a discussion.)

Other problems:

You should add

use strict;
use warnings;

You never use the $x and $y that you declare in the outer scope.

Your even function appears to be incomplete. It doesn't (explicitly) return a value if its argument is an odd number. What exactly is it intended to do?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文