60

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?

4
  • 2
    Without picking a specific language, this question is really too broad to be useful.
    – jpaugh
    Aug 26, 2017 at 19:08
  • 3
    @jpaugh: I think it is language agnostic.
    – phoxis
    Aug 27, 2017 at 20:15
  • 1
    The question comes up in many programming languages; but the answers vary according to the rules of a given language. Most modern languages don't even have the concept of a subroutine anymore, at least not with the limitations that originally distinguished it from a function.
    – jpaugh
    Aug 28, 2017 at 0:44
  • 1
    This goes back to Fortran 57, where a function returns a value and has no side-effects, and a subroutine doesn't return a value and can have side-effects.
    – user207421
    Mar 11, 2020 at 9:01

12 Answers 12

23

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.

7
  • 1
    before i was told such a difference i knew what you told, but now i have some confusion. The main context from which this question arose was Intel 8085 ASM code subroutines and functions.
    – phoxis
    May 18, 2011 at 17:11
  • I am not familiar with that technology. Yet, I maintain that the terminology for that language draws a distinction. One that could arguably be a convention of the language. Generally speaking though, I don't think there's a definitive difference. May 18, 2011 at 17:15
  • ya, if this question is not closed, then i will keep an eye here, well try figuring out what other contexts tell.
    – phoxis
    May 18, 2011 at 17:20
  • 9
    In a purely mathematical sense however, a function takes arguments and returns a value. Obviously we can write a function, procedure, method, whatever that does neither of those (ie. void function()). However in some languages (VB) you can not have a Function that doesn't return a value, but it can have no arguments. A subroutine cannot return a value and may or may not have arguments. Object oriented practices complicate this more. But most commonly member functions are called methods, a word that has no mathematical ties (though maybe scientific with a different definition).
    – NerdFury
    May 18, 2011 at 19:51
  • 3
    @NerdFury, I encourage you to post this as your own answer. It would seem by your comment that you have provided an alternate definition. Although I still maintain that the difference between these two terms boils down to specific language conventions, I agree with what you have said. May 18, 2011 at 21:06
14

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 ;-)

9
  • 5
    Explain void function() then? May 18, 2011 at 17:11
  • 2
    A void function doesn't return a value. Your definition is a function returns a value. Weak sauce. May 18, 2011 at 17:32
  • 5
    From Wikipedia: '... the void type in such context is comparable to that of the syntactic constructs which define subroutines in Visual Basic and procedures in Pascal.'
    – NerdFury
    May 18, 2011 at 19:54
  • 1
    @Andreas You should correct your answer because a function has the option to/not to return a value. Also, a function has the option to/not to modify passed arugments. This is quite fundamental.
    – ha9u63a7
    Jun 26, 2013 at 12:26
  • 1
    Since void function() does not have an output, it is a subroutine and strictly speaking, is not a function Mar 11, 2020 at 6:33
8

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

1
  • huh? That definition of subroutine is gibberish to me. Word salad.
    – steve
    Feb 1 at 22:15
4

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.

4

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.

4

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.

0

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.

0

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.

0

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?

0

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.)

-1

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.

-2

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.

1
  • 2
    Your answer might be correct, but that info is already given in all the other answers to this question. Please only answer on an old question if some definition did change (and thus the former correct answer got invalidated) or if you add new information. Oct 5, 2016 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.