Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: [Homework Question] Subroutines & References

by Hayest (Acolyte)
on Feb 11, 2015 at 16:28 UTC ( [id://1116349]=note: print w/replies, xml ) Need Help??


in reply to Re: [Homework Question] Subroutines & References
in thread [Homework Question] Subroutines & References

#!/usr/bin/perl use warnings; use List::Util qw(sum); my @list_of_numbers = (1 .. 50); my $i = (); my $a = (); sub mean { return sum(@_)/@_; } sub above_mean { $i == mean(@list_of_numbers); foreach (@list_of_numbers) { if ($a > $i) { print "$a is above mean, which is mean(@list_of_numbers)"; } } } print above_mean(@list_of_numbers);

This now returns:

Use of uninitialized value $a in numeric gt (>) at main.pl line 16.

However, I thought I declared $a earlier in my program?

Replies are listed 'Best First'.
Re^3: [Homework Question] Subroutines & References
by choroba (Cardinal) on Feb 11, 2015 at 16:33 UTC
    == is the numeric comparison operator, not the assignment operator, which is quite similar: =. See perlop for details. warnings should have told you:
    Useless use of numeric eq (==) in void context at ...

    Also, you don't populate $a anywhere. Maybe, you meant

    foreach my $a (@list_of_numbers) {
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      I realized the == error once it executed, however, you helped out with the suggestion of the foreach statement. I've got it running properly now. Thank you!
Re^3: [Homework Question] Subroutines & References
by AnomalousMonk (Archbishop) on Feb 11, 2015 at 17:21 UTC
    print above_mean(@list_of_numbers);

    Just as a matter of curiosity, you might try this variation on the quoted statement:
        print above_mean();
    Does calling the  above_mean() function without any parameters make any difference to the value returned by the function? If not, why not?


    Give a man a fish:  <%-(-(-(-<

      calling above_mean(); function without parameters make does not make any difference to the value returned by the function. I believe this is because @numbers (which I had as the parameter) is already declared inside of my subroutine above_mean - is this accurate?

        I would state it slightly differently. The function returns nothing different when it is passed no parameters because it takes no parameters — or more precisely, it does nothing with any parameters it may be passed. The array  @list_of_numbers is accessed (not declared) within the function. This treats the array as a global variable (even though technically it is lexical). I notice other instances of such access in your functions. Accessing a mutable global (as opposed to a constant) within a function is generally considered a Bad Thing, and is officially Frowned Upon. You have been warned.


        Give a man a fish:  <%-(-(-(-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1116349]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-24 06:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found