Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a subroutine and a function? [duplicate]

Possible Duplicate:
What is the difference between a ‘function’ and a ‘procedure’?

I searched online for an answer to this question, and the answer I got was that a function can return a value, modify a value, etc., but a subroutine cannot. But I am not satisfied with this explanation and it seems to me that the difference ought to be more than just a matter of terminology.

So I am looking for a more conceptual answer to the question.

like image 673
CodeBlue Avatar asked Apr 30 '12 18:04

CodeBlue


People also ask

What is the difference between subroutine and a function?

Functions and subroutines operate similarly but have one key difference. A function is used when a value is returned to the calling routine, while a subroutine is used when a desired task is needed, but no value is returned.

What is the difference between a subroutine and a function 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 subroutine function?

Functions. A Function is a subroutine that returns a value. The primary purpose of functions is to break up complicated computations into meaningful chunks and name them. The subroutine may return a computed value to its caller (its return value), or provide various result values or output parameters.

What is the difference between a routine and subroutine?

In computer programming, routine and subroutine are general and nearly synonymous terms for any sequence of code that is intended to be called and used repeatedly during the executable of a program. This makes the program shorter and easier to write (and also to read when necessary).


2 Answers

A function mirrors the mathematical definition of a function, which is a mapping from 1 or more inputs to a value.1

A subroutine is a general-purpose term for any chunk of code that has a definite entry point and exit point.

However, the precise meaning of these terms will vary from context to context.


1. Obviously, this is not the formal mathematical definition of a function.
like image 108
Oliver Charlesworth Avatar answered Nov 09 '22 23:11

Oliver Charlesworth


A generic definition of function in programming languages is a piece of code that accepts zero or more input values and returns zero or one output value.

The most common definition of subroutine is a function that does not return anything and normally does not accept anything. It is only a piece of code with a name.

Actually in most languages functions do not differ in the way you declare them. So a subroutine may be called a function, but a function not necessarily may be called a subroutine.

Also there is people that consider functions and subroutines the same thing with a different name.

Subroutine - Wikipedia

like image 23
ceklock Avatar answered Nov 10 '22 01:11

ceklock