Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Difference between subroutine and functions

by madtoperl (Hermit)
on Aug 17, 2006 at 13:30 UTC ( [id://567915]=perlquestion: print w/replies, xml ) Need Help??

madtoperl has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Difference between subroutine and functions

Replies are listed 'Best First'.
Re: Difference between subroutine and functions
by davorg (Chancellor) on Aug 17, 2006 at 13:38 UTC

    What makes you think that we have both functions and subroutines in Perl? Actually we only have subroutines.

    Generally in computer science a function is a special type of subroutine that returns a values (as opposed to being called just for its side-effects). But in Perl (as the cookbook says) we don't make that distinction.

    As an aside, could you please refrain from posting links to copyright-infringing web sites. O'Reilly's authors worked hard to produce those books - it would be nice if they were compensated for their effort.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Difference between subroutine and functions
by holli (Abbot) on Aug 17, 2006 at 13:39 UTC

      function and subroutine are subprograms used in fortran.

      function subprograms are used to compute a single value,while subroutine subprogram are used to compute several values or to perform task.

      main difference between a subroutine and a function as follows:

      1. No value is associated with the name of a subroutine,while the name of a function subprogram must have a value, numerical or logical.
      2. A subroutine may be invoked only by a special calling statement-the call statement. The name of a function subprogram may be used in the same way as library functions,,i.e.,in arithmetic statements,etc.
      3. A FUNCTION subprogram must have at least one argument, while a subroutine need not have any.
      4. Since a function subprogram computes atleast one value, it must contain a return statement. A SUBROUTINE subprogram need not contain return statement.
Re: Difference between subroutine and functions
by BrowserUk (Patriarch) on Aug 17, 2006 at 14:21 UTC

    Traditionally, outside of Perl,

    • Functions:

      returned values that were used as a part of the calling expression.

      Preferably they do not have side-effects.

    • procedures:

      Returned only status. True/false, an error code or an exception.

      Usually side-effectful.

    • subroutine:

      A generic term covering both and more.

    I'm not sure if there is any language that actually enforced these rules. Didn't Pascal complain if you tried to return a value from a procedure?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Difference between subroutine and functions
by marto (Cardinal) on Aug 17, 2006 at 13:49 UTC
    madtoperl,

    I don't know how many times this has been pointed out to people in the past, please do not link to sites that illegally reproduce copyright materials, O'Reilly's CD bookshelf is not free, it is not the sites right to publish these works, making them available in this way. Please apply a little thought about what you are doing.

    Martin
Re: Difference between subroutine and functions
by Sidhekin (Priest) on Aug 17, 2006 at 13:37 UTC

    The way I always explain it: CODE objects are declared as subroutines, but used either as functions or methods. Or even both, if you want to confuse people (CGI.pm does, apparantly):

    sub foo { print @_ } foo "Hello "; # function - prints "Hello " main->foo("!"); # method - prints "main!"

    You cannot call a subroutine except as a function or a method.

    Nor can you write a function or method, except as a subroutine.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: Difference between subroutine and functions
by diotalevi (Canon) on Aug 17, 2006 at 13:35 UTC

    The two words mean the same thing. They're synonyms.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Difference between subroutine and functions
by jeffa (Bishop) on Aug 17, 2006 at 13:44 UTC

    A function is not necessarily a computing term -- in the English language it could mean anything from something your body does to a social event. A subroutine, however, is pretty much limited to a computing term, although i'm sure someone has used it outside of that scope. (UPDATE: touché, ysth!) In Perl, i tend to refer to the built-in functions such as print and sort as functions, but i refer to my own functions as subroutines. Of interest is perldoc -f, which is used to pull up documentation for Perl's built-in subroutines [sic]. ;) Notice it is -f and not -s.

    As diotalevi said, they are homonyms in terms of computing.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      My day has gone no better than usual, but at least it wasn't subroutine.
Re: Difference between subroutine and functions
by cdarke (Prior) on Aug 17, 2006 at 16:36 UTC
    The difference, if any, depends on the language. This being Perl you can use the terms as you wish.
    Personally I refer to built-ins as functions, since they are referred as that in perlfunc, and user written named blocks of code as subroutines, since they begin with 'sub'.
    What do you understand the difference to be?
Re: Difference between subroutine and functions
by jwkrahn (Abbot) on Aug 17, 2006 at 20:04 UTC
    Then why we are having subroutine,function in perl.
    Perl doesn't have that problem, it only has keywords and operators.   :-)
Re: Difference between subroutine and functions
by Anonymous Monk on May 13, 2010 at 10:24 UTC
    In my opinion both are almost same, Function are abstract part of subroutine which is internally implemented according to assembly language,One more Stuff i would like to tell,Generally you know the name of the function but you don't know the name of subroutine instead of it you know the address only, So via using function pointer your subroutine is called.It was my perception,Plz let me know the views. Thanks and Regards Rajan

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-26 08:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found