What is the difference between a function and a subroutine?

Terminology

Terminology Problem Overview


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, works locally and does not alter any value or work with any value outside its scope (high cohesion). It also returns some value. A subroutine works directly with the values of the caller or code segment which invoked it and does not return values (low cohesion), i.e. branching some code to some other code in order to do some processing and come back.

Is this true? Or is there no difference, just two terms to denote one?

Terminology Solutions


Solution 1 - Terminology

I disagree. If you pass a parameter by reference to a function, you would be able to modify that value outside the scope of the function. Furthermore, functions do not have to return a value. Consider void some_func() in C. So the premises in the OP are invalid.

In my mind, the difference between function and subroutine is semantic. That is to say some languages use different terminology.

Solution 2 - Terminology

A function returns a value whereas a subroutine does not. A function should not change the values of actual arguments whereas a subroutine could change them.

Thats my definition of them ;-)

Solution 3 - Terminology

If we talk in C, C++, Java and other related high level language:

a. A subroutine is a logical construct used in writing Algorithms (or flowcharts) to designate processing functionality in one place. The subroutine provides some output based on input where the processing may remain unchanged.

b. A function is a realization of the Subroutine concept in the programming language

Solution 4 - Terminology

Both function and subroutine return a value but while the function can not change the value of the arguments coming IN on its way OUT, a subroutine can. Also, you need to define a variable name for outgoing value, where as for function you only need to define the ingoing variables. For e.g., a function:

double multi(double x, double y) 
{
  double result; 
  result = x*y; 
  return(result)
}

will have only input arguments and won't need the output variable for the returning value. On the other hand same operation done through a subroutine will look like this:

double mult(double x, double y, double result) 
{
  result = x*y; 
  x=20; 
  y = 2; 
  return()
}

This will do the same as the function did, that is return the product of x and y but in this case you (1) you need to define result as a variable and (2) you can change the values of x and y on its way back.

Solution 5 - Terminology

In terms of Visual Basic a subroutine is a set of instructions that carries out a well defined task. The instructions are placed within Sub and End Sub statements.

Functions are similar to subroutines, except that the functions return a value. Subroutines perform a task but do not report anything to the calling program. A function commonly carries out some calculations and reports the result to the caller.

Solution 6 - Terminology

One of the differences could be from the origin where the terminology comes from.

Subroutine is more of a computer architecture/organization terminology which means a reusable group of instructions which performs one task. It is is stored in memory once, but used as often as necessary.

Function got its origin from mathematical function where the basic idea is mapping a set of inputs to a set of permissible outputs with the property that each input is related to exactly one output.

Solution 7 - Terminology

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.

Solution 8 - Terminology

Yes, they are different, similar to what you mentioned.

A function has deterministic output and no side effects.
A subroutine does not have these restrictions.

A classic example of a function is int multiply(int a, int b)
It is deterministic as multiply(2, 3) will always give you 6.
It has no side effects because it does not modify any values outside its scope, including the values of a and b.

An example of a subroutine is void consume(Food sandwich)
It has no output so it is not a function.
It has side effects as calling this code will consume the sandwich and you can't call any operations on the same sandwich anymore.

You can think of a function as f(x) = y, or for the case of multiply, f(a, b) = c. Yes, this is programming and not math. But math models and begs to be used. So we use math in cs. If you are interested to know why the distinction between function and subroutine, you should check out functional programming. It works like magic.

Solution 9 - Terminology

From the view of the user, there is no difference between a programming function and a subroutine but in theory, there definitely is!

The concept itself is different between a subroutine and a function. Formally, the OP's definition is correct. Subroutines don't take arguments or give return values by formal semantics. That's just an interpretion with conventions. And variables in subroutines are accessible in other subroutines of the same file although this can be achieved as well in C with some difficulties.

Summary:

Subroutines work only based on side-effects, in the view of the programming language you are programming with. The concept itself has no explicit arguments or return values. You have to use side effects to simulate them.

Functions are mappings of input to output value(s) in the original sense, some kind of general substitution operation. In the adopted sense of the programming world, functions are an abstraction of subroutines with information about return value and arguments, inspired by mathematical functions. The additional formal abstraction differentiates a function from a subroutine in programming context.

Details:

The subroutine originally is simply a repeatable snippet of code which you can call in between other code. It originates in Assembly or Machine language programming and designates the instruction sequence itself. In the light of this meaning, Perl also uses the term subroutine for its callable code snippets.

Subroutines are concrete objects.

This is what I understood: the concept of a (pure) function is a mathematical concept which is a special case of mathematical relations with an own formal notation. You have an input or argument and it is defined what value is represented by the function with the given argument. The original function concept is entirely unrelated to instructions or calculations. Mathematical operations (or instructions in the programming world) only are a popular formal representation (description) of the actual mapping. The original function term itself is not defined as code. Calculations do not constitute the function, so that functions actually don't have any computational overhead because they are direct mappings. Function complexity considerations only arrived as there is an overhead to find the mapping.

Functions are abstract objects.

Now, since the whole PC-stuff is running on small machine instructions, the easiest way to model (or instantiate) mathematics is with a sequence of instructions itself. Computer Science has been founded by mathematicians (noteworthy: Alan Turing) and the first programming concepts are based on it so there is a need to bring mathematics into the machine. That's how I imagine the reason why "function" is the name of something which is implemented as subroutine and why the term "pure" function was coined to differentiate the original function concept from the overly broad term-use in programming languages.

Note: in Assembly Language Programming, it is typically said, that a subroutine has been passed arguments and gives a return value. This is an interpretation on top of the concrete formal semantics. Calling conventions specify the location where values, to be considered as arguments and return values, should be written to before calling a subroutine or returning. The call itself takes only a subroutine address, and has no formal arguments or return values.

PS: functions in programming languages don't necessarily need to be a subroutine (even though programming language terminology developed this way). Functions in functional programming languages can be constant variables, arrays or hash tables. Isn't every datastructure in ECMAScript a function?

Solution 10 - Terminology

The difference is isolation. A subroutine is just a piece of the program that begins with a label and ends with a go to. A function is outside the namespace of the rest of the program. It is like a separate program that can have the same variable names as used in the calling program, and whatever it does to them does not affect the state of those variables with the same name in the calling program.

From a coding perspective, the isolation means that you don’t have to use the variable names that are local to the function.

Sub double:

a = a + a

Return

fnDouble(whatever):

whatever = whatever + whatever

Return whatever

The subroutine works only on a. If you want to double b you have to set a = b before calling the subroutine. Then you may need to set a to null or zero after. Then when you want to double c you have to again set a to equal c. Also the sub might have in it some other variable, z, that is changed when the sub is jumped to, which is a bit dangerous.

The essential is isolation of names to the function (unless declared global in the function.)

Solution 11 - Terminology

I am writing this answer from a VBA for excel perspective. If you are writing a function then you can use it as an expression i. e. you can call it from any cell in excel.

eg: normal vlookup function in excel cannot look up values > 256 characters. So I used this function:

Function MyVlookup(Lval As Range, c As Range, oset As Long) As Variant
  Dim cl As Range
  For Each cl In c.Columns(1).Cells
  If UCase(Lval) = UCase(cl) Then
  MyVlookup = cl.Offset(, oset - 1)
  Exit Function
  End If
  Next
End Function

This is not my code. Got it from another internet post. It works fine.

But the real advantage is I can now call it from any cell in excel. If wrote a subroutine I couldn't do that.

Solution 12 - Terminology

Every subroutine performs some specific task. For some subroutines, that task is to compute or retrieve some data value. Subroutines of this type are called functions. We say that a function returns a value. Generally, the returned value is meant to be used somehow in the program that calls the function.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionphoxisView Question on Stackoverflow
Solution 1 - TerminologyJason McCrearyView Answer on Stackoverflow
Solution 2 - TerminologyAndreasView Answer on Stackoverflow
Solution 3 - TerminologyAmitView Answer on Stackoverflow
Solution 4 - TerminologyNKayView Answer on Stackoverflow
Solution 5 - TerminologysainaView Answer on Stackoverflow
Solution 6 - TerminologyAzeem AkhterView Answer on Stackoverflow
Solution 7 - TerminologyVlad BezdenView Answer on Stackoverflow
Solution 8 - Terminologyakgren_soarView Answer on Stackoverflow
Solution 9 - TerminologyChrisoLosophView Answer on Stackoverflow
Solution 10 - TerminologyHarry BinswangerView Answer on Stackoverflow
Solution 11 - TerminologyDaredevilView Answer on Stackoverflow
Solution 12 - TerminologyTi KanonView Answer on Stackoverflow