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
| [reply] |
Re: Difference between subroutine and functions
by holli (Abbot) on Aug 17, 2006 at 13:39 UTC
|
| [reply] [d/l] |
|
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.
| [reply] |
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.
| [reply] |
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 | [reply] |
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!
| [reply] [d/l] [select] |
Re: Difference between subroutine and functions
by diotalevi (Canon) on Aug 17, 2006 at 13:35 UTC
|
| [reply] |
|
| [reply] |
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.
| [reply] [d/l] |
|
My day has gone no better than usual, but at least it wasn't subroutine.
| [reply] |
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?
| [reply] |
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. :-)
| [reply] [d/l] |
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 | [reply] |