Thread: Subroutines

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    37
    Rep Power
    1

    Subroutines


    I'm just over here messing around with subroutines, trying to get used to them, but I have hit a stumbling block

    Why doesn't this code work:


    Code:
    #!/usr/bin/env perl
    use strict;
    use warnings;
    my $number;
     
    sub Experiment
    {
            my $first_val = $_[0];
            my $second_val = $_[1];
            return($first_val + $second_val);
    }
    Experiment(5,6) = $number;
    print $number;
    This is my error message: "Can't modify non-lvalue subroutine call in scalar assignment at ./hw6 line 13, near "$print;"


    This code just adds the two arguments that get inputted. Why doesn't it work? I'm guessing it has something to do with the numerical aspect of it, because I can run a subroutine that does a print job with no problem.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    Look at how you assigned value to $first_val and $second_val.

    Now look at how you try to assign a value to $number. It is at the wrong side at the =.
    Try change that line to:
    Code:
     $number = Experiment(5,6);

    Comments on this post

    • Laurent_R agrees : I agree, just doing "$number = Experiment(5,6);" should solve the issue.

IMN logo majestic logo threadwatch logo seochat tools logo