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

自闭症网瘾萝莉.ら 提交于 2019-12-01 03:12:11

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.

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

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.

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.

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