What is the difference between a function and a subroutine?

前端 未结 11 1568
攒了一身酷
攒了一身酷 2021-01-30 06:32

What is the difference between a function and a subroutine? I was told that the difference between a function and a subroutine is as follows:

A function takes parameters

11条回答
  •  一整个雨季
    2021-01-30 07:29

    Based on Wikipedia subroutine definition:

    In computer programming, a subroutine is a sequence of program instructions that perform a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.

    Subroutines may be defined within programs, or separately in libraries that can be used by many programs. In different programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.

    In Python, there is no distinction between subroutines and functions. In VB/VB.NET function can return some result/data, and subroutine/sub can't. In C# both subroutine and function referred to a method.

    Sometimes in OOP the function that belongs to the class is called a method.

    There is no more need to distinguish between function, subroutine and procedure because of hight level languages abstract that difference, so in the end, there is very little semantic difference between those two.

提交回复
热议问题