Possible Duplicate:
What is the difference between a ‘function’ and a ‘procedure’?
I searched online for an answer to this question, and the answer I got was that a function can return a value, modify a value, etc., but a subroutine cannot. But I am not satisfied with this explanation and it seems to me that the difference ought to be more than just a matter of terminology.
So I am looking for a more conceptual answer to the question.
Functions and subroutines operate similarly but have one key difference. A function is used when a value is returned to the calling routine, while a subroutine is used when a desired task is needed, but no value is returned.
A function must return a single value, and can be invoked from within expressions, like a write statement, inside an if declaration if (function) then , etc. A subroutine does not return a value, but can return many values via its arguments and can only be used as a stand-alone command (using the keyword call ).
Functions. A Function is a subroutine that returns a value. The primary purpose of functions is to break up complicated computations into meaningful chunks and name them. The subroutine may return a computed value to its caller (its return value), or provide various result values or output parameters.
In computer programming, routine and subroutine are general and nearly synonymous terms for any sequence of code that is intended to be called and used repeatedly during the executable of a program. This makes the program shorter and easier to write (and also to read when necessary).
A function mirrors the mathematical definition of a function, which is a mapping from 1 or more inputs to a value.1
A subroutine is a general-purpose term for any chunk of code that has a definite entry point and exit point.
However, the precise meaning of these terms will vary from context to context.
A generic definition of function in programming languages is a piece of code that accepts zero or more input values and returns zero or one output value.
The most common definition of subroutine is a function that does not return anything and normally does not accept anything. It is only a piece of code with a name.
Actually in most languages functions do not differ in the way you declare them. So a subroutine may be called a function, but a function not necessarily may be called a subroutine.
Also there is people that consider functions and subroutines the same thing with a different name.
Subroutine - Wikipedia
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With