What is the difference between a subroutine and a function? [duplicate]

白昼怎懂夜的黑 提交于 2019-12-01 00:14:09

问题


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.


回答1:


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.


1. Obviously, this is not the formal mathematical definition of a function.


回答2:


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




回答3:


It's worth noting as an addendum to @Oli's answer that in the mathematical sense a function must be "well-defined", which is to say its output is uniquely determined by its inputs, while this often isn't the case in programming languages.

Those that do make this guarantee (and also that their functions not cause side-effects) are called pure functional languages, an example of which being Haskell. They have the advantage (among others) of their functions being provably correct in their behaviour, which is generally not possible if functions rely on external state and/or have side-effects.




回答4:


A function must return some value and must not change a global variable or a variable declared outside of the function's body. Under this situation, a function can only mimic it's mathematical counter part (the thing which maps a mathematical object to another mathematical object)

A subroutine doesn't return anything and usually is impure as it has to change some global state or variable otherwise there is no point in calling it. There is no mathematical parallel for a subroutine.



来源:https://stackoverflow.com/questions/10388393/what-is-the-difference-between-a-subroutine-and-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!