What is the difference between a function and a subroutine?

Solution 1:

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:

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:

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:

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.