Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: I'm trying to consolidate my functions into subroutines

by LanX (Chancellor)
on May 13, 2017 at 16:37 UTC ( #1190201=note: print w/replies, xml ) Need Help??


in reply to I'm trying to consolidate my functions into subroutines

Functions and subroutines are the same thing.

> Any help is well appreciated

Do yourself a favor and start indenting your code properly.

A decent editor does it automatically.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

  • Comment on Re: I'm trying to consolidate my functions into subroutines

Replies are listed 'Best First'.
Re^2: I'm trying to consolidate my functions into subroutines
by Peter Keystrokes (Acolyte) on May 13, 2017 at 16:43 UTC

    Except subroutines are supposed to be reusable.

    "do yourself a favor and start indenting your code properly."

    Explain?
      > Explain

      sub name { print $a; .... }

      not

      sub name { print $a; .... }

      > Except subroutines are supposed to be reusable.

      in which language?

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Is there any discernible improvement in this?

        ##Invoking subroutines HashSequences(); SpecifySeqLengths(my $id, my %seq); ## Open file. Hash sequences. Make the sequence IDs the keys to their ## respective (hashed) sequences. sub HashSequences{ open F, "human_hg19_circRNAs_putative_spliced_sequence.fa", or die + $!; my %seq = (); my $id = ''; while (<F>){ chomp; if ($_ =~ /^>(.+)/){ $id = $1; }else{ $seq{$id} .= $_; } } close F; return (%seq, $id); } ## Request sequence length desired. Sieve sequences of given length in +to ## arrays. Create file containing desired sequences. sub SpecifySeqLengths{ print "Enter Max sequence length: \n"; my $maxlength = <STDIN>; chomp $maxlength; print "Enter Min sequence length: \n"; my $minlength = <STDIN>; chomp $minlength; my @seqarray; foreach $id (keys %seq){ if ((length$seq{$id} <= $maxlength) && (length$seq{$id} >= $mi +nlength)){ push @seqarray, $id; } } for $id (@seqarray){ if (-f $id){print $id, " already exists. It is about to be ove +rwritten"}; open new_F, '>>', "SeqLength_$minlength-$maxlength", or die$!; print new_F ($id."\n".$seq{$id}."\n"); close new_F; } }
      Except subroutines are supposed to be reusable.

      What LanX means is that a function is a subroutine. Specifically, a subroutine that returns a value (or a list of values).

      In Perl, all subroutines return a value, either explicitly or implicitly, so all Perl subroutines are also functions.

      So, the following 3 definitions are equivalent:

      sub myfunction_long { my $z = $_[0] + $_[1]; # add the first 2 parameters and assign res +ult to $z return $z; } sub myfunction_medium { my $z = $_[0] + $_[1]; # add the first 2 parameters and assign res +ult to $z } # value of $z implicitly returned sub myfunction_short { $_[0] + $_[1]; # add the first 2 parameters } # result of expression implicitly returned print myfunction_long(1, 2); # prints 3 print myfunction_medium(1, 2); # prints 3 print myfunction_short(1, 2); # prints 3

      Many people are more comfortable using return even when not needed. When not used, the result of the last executed statement is the value returned.

      In some programming languages, such as C, you can define either a "pure" subroutine or a function:

      int z = 0; void myroutine(int x, y) // "void" tells the compiler that no value is + returned { z = x + y; // add the values of the 2 defined parameters and assig +n the result to the global z } int myfunction(int x, y) // "int" tells the compiler that an integer v +alue is returned { return(x + y); // add the values of the 2 defined parameters then +return the result }

      Perl, however, has no such distinction.

        ... a function is a subroutine ... that returns a value (or a list of values).

        In general, your distinction between function and subroutine as, respectively, returning or not returning a value seems of little use. Is there really any formal distinction here in any language?

        In some programming languages, such as C, you can define either a "pure" subroutine or a function ...

        I'm confused by your use of the term "pure" as it relates to subroutines/functions. As I understand it, a pure function is one whose behavior is not affected in any way by the state of the system in which it's running, has no effect upon the system other than by its return value, and creates no persistent state; the function has no "side effects." Any language can have such functions. As such, a pure function returning void would be useless. (Some languages allow the definition of functions with a pure attribute and purity is enforced by the compiler, but I'm not aware that ISO-C is one of them; I may be behind the latest standard here. (FWIW, a little Googling shows that GNU C has an  __attribute__ "pure" feature.)) Your  myfunction() C function is operationally pure; your  myroutine() function is not: it has the side effect of assignment to the  z variable and the effect of this assignment persists after the function terminates execution. (All three of the Perl functions you give as examples are operationally pure, but as you say, Perl neither defines nor enforces any formal notion of function purity.)

        Update: See the discussion of the implementation of Pure Functions in the D language.


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

        As AnomalousMonk wrote, such function attributes are GNU C extensions. But they can be considered de facto standard features on many platforms. Excerpt from gcc info's:

        • const. Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict class than the `pure' attribute below, since function is not allowed to read global memory....
        • pure. Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. These functions should be declared with the attribute `pure'. For example,
          int square (int) __attribute__ ((pure));
          says that the hypothetical function `square' is safe to call fewer times than the program says....
        Note that in many cases, the compiler can discover those attributes on its own (and even offer suggestions to the programmer via warnings.)

        ps. Take care not to confuse type qualifier const with attribute((const))

Re^2: I'm trying to consolidate my functions into subroutines
by Anonymous Monk on May 13, 2017 at 16:58 UTC

    One wonders here - does a decent editor fix typoes and grammatical errors, such as capitalizing the first word of a sentence or adding a missing dot at the end of sentence?

      OK Jeff! :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190201]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2017-05-27 16:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?