Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

calling subroutines

by bobosm (Initiate)
on Oct 12, 2013 at 15:16 UTC ( #1057990=perlquestion: print w/ replies, xml ) Need Help??
bobosm has asked for the wisdom of the Perl Monks concerning the following question:

I need help, i am new in Perl if you can help me i have a question:

I want to know how to Random generate 5 number in (num1) outside of subroutine (some) Then 5 times calls the subroutine and then displayed the results.

sub some() { print "Enter a number: \n"; chomp (my $num1 = <STDIN>); my $num2 = 2; my $result; $result = $num1+$num2; print "result is: $result"; #return result; }

Comment on calling subroutines
Download Code
Re: calling subroutines
by toolic (Chancellor) on Oct 12, 2013 at 15:29 UTC
    This is one way to call your sub 5 times and display the result:
    use warnings; use strict; for ( 1 .. 5 ) { some(); } sub some { print "Enter a number: \n"; chomp( my $num1 = <STDIN> ); my $num2 = 2; my $result; $result = $num1 + $num2; print "result is: $result\n"; }

    If you want it to do something different, you must be much more specific. See also perlintro

      Tnx for reply, and how I can generate a random number for variable num1 which is in subroutine?

        Pass a parameter through your subroutine:

        my $number = rand; some($number); sub some { my $num1 = shift; print "$num1\n"; }

        But I don't have any idea how you are supposed to be reconciling that with your existing code that takes user input. Why are you taking user input from STDIN if your goal is to generate values from within the program? And is $num2 really supposed to always be the value 2?

        This is homework, right? Even on a student's limited budget, Learning Perl, from OReilly, is a worthwhile purchase. Or if you are lucky you can find it or request it at the library. Also, read perlintro, and perlsyn. You could spend countless hours stumbling around in the dark, or spend two hours reading those two free documents, after which you will be able to complete your assignments with swift confidence.

        If the web server is down for those two final links I posted, just type "perldoc perlintro", and "perldoc perlsyn" on any system that has Perl installed. They're already on that system, no network access required.


        Dave

      I write like that, but it is error: Use unitialized num1 in additional

      for ( 1 .. 5 ) { my $num1 = int(rand(10)); some(); } sub some { print "Enter a number: \n"; #chomp (my $num1 = <STDIN>); my $num1; my $num2 = 2; my $result; $result = $num1 + $num2; print "result is: $result\n"; #return result; }

        Passing variables into a subroutine through osmosis (except for specific situations that you don't need to worry about yet) is a terrible habit to get into.

        To remedy your immediate problem, remove the line from your sub, "my $num1;", because it's creating a lexical variable inside the sub that masks the variable created in your for loop. But, while that will fix the immediate problem, it will start you down the wrong path. What you should be doing is passing parameters into your subroutine like this:

        my $value = "whatever"; some($value); sub some { my( $param ) = @_; print "$param\n"; }

        ...or use shift, or any of the other means demonstrated when you read the document "perlsyn", by typing perldoc perlsyn on your local system with Perl installed.


        Dave

      And some creepy code
      #!/usr/bin/perl -w #generation of some rand use strict; my $RangeVal = undef; #Get STDIN while(1){ print 'Enter qty of random values you need -> '; $RangeVal = <STDIN>; last if $RangeVal =~ /^\d+$/; } $RangeVal = &RandGen($RangeVal); #exec of func foreach (@{$RangeVal}){if(defined $_){print $_,"\n";}} #display #function declaration# sub RandGen{ my $RangeVal = shift; my @OutVector = undef; for(0..$RangeVal-1){push @OutVector,int(rand(10*$RangeVal) +);} $RangeVal = \@OutVector; return $RangeVal; }
Re: calling subroutines
by roboticus (Canon) on Oct 12, 2013 at 15:31 UTC

    bobosm:

    The rand function (see perldoc -f rand) will help you with your first requirement--I'd suggest reading the entire perlfunc documentation to familiarize yourself with what's already done for you.

    For your second requirement, read perldoc perlsyn paying special attention to the while and for loop constructs.

    Update: It seems that the links are broken or the web server is having a bit of trouble. I'll review them a bit later and fix 'em if required.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: calling subroutines
by Marshall (Prior) on Oct 12, 2013 at 15:36 UTC
    If you add 5 random numbers together, that is still just a random number. Sounds like homework.

    my $iRand = int(rand($range)); #???
    Update: If you add several random numbers together, there is the possibility of an overflow of precision - think about that.
Re: calling subroutines
by Lennotoecom (Pilgrim) on Oct 12, 2013 at 17:32 UTC
    five times generates a random number and passes it to the subroutine, printing the random number:
    ($a = int(rand(10)), sub{print shift,"\n"}->($a)) for 1..5;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1057990]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2014-08-29 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (280 votes), past polls