What is the difference between a function and a subroutine?

♀尐吖头ヾ 提交于 2019-12-02 15:08:10

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.

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

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

saina

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.

NKay

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.

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.

Daredevil

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.

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.

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