Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a function and a subroutine?

Tags:

terminology

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?

like image 305
phoxis Avatar asked May 18 '11 17:05

phoxis


People also ask

What is the difference between function and subroutine in Fortran?

A function must return a single value, and can be invoked from within expressions, like a write statement, inside an if declaration if (function) then , etc. A subroutine does not return a value, but can return many values via its arguments and can only be used as a stand-alone command (using the keyword call ).

What is the difference between function and subroutine in VB net?

A sub performs a task but does not return a value. A function returns a value of the tasks performed. Subs can be recalled from anywhere in the program and in multiple types. Functions are called by a variable.

What is the difference between a subroutine and a sub program?

In Fortran, functions are invoked or referenced; subroutines are called. Normally, the invoking or calling program passes on to the subprogram a set of arguments. The subprogram uses these arguments to compute the results and to pass them back and return control to the invoking or calling program.


1 Answers

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.

like image 50
Jason McCreary Avatar answered Sep 20 '22 14:09

Jason McCreary