1

I´m newbie in perl and I need to define a subroutine in perl, but I don't understand the difference between subroutine and function.

When should I use any of them and how can I send parameters?

1
  • What do you mean by "when should I use any of them"? Commented Nov 11, 2015 at 17:33

2 Answers 2

7

In the documentation, "function" refers to list operators (e.g. chr, print), named unary operators (e.g. chdir) and named nullary operators (e.g. time). These are sometimes called "builtin functions" to avoid ambiguity (though there are builtin subroutines too, such as utf8::upgrade).

In practice, "function" is commonly used to refer to both builtin functions and anything declared with sub.


Arguments are usually passed to subroutine as follows:

foo($x, $y)

Operators don't technically have parameters; they have operands. Most operators that qualify as functions resemble subroutines. perlfunc documents how to use each one.

Sign up to request clarification or add additional context in comments.

Comments

4

There isn't a real difference. "Subroutine" is just a name for a function that you write, as opposed to one of Perl's builtin functions. It's okay to call them functions too.

3 Comments

perldoc perlglossary says "If the subroutine returns a meaningful value, it is also called a function," but this doesn't jive with common usage (or even the rest of the docs; not everything in perlfunc returns a value). It seems like function and subroutine are used interchangeably now.
I think the important part of the question isn't about the definitions, though, but rather "When should I use any of them and how can I send parameters?"
@ThisSuitIsBlackNot You're right, but that's really a separate question and one that's a) too broad or b) a duplicate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.