function v/s subroutine

i found a statement in wikianswers that says

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

what if i pass the arguments in the function by reference then they can be changed. Doesnt this contradict the above statement


What is your defintion of a function and a subroutine?
That statement isn't true: in C++ you always have functions, some could have no return type ( void ), some could have arguments by pointer or reference (and modify the values of arguments) and some could have both
Last edited on
so if a function in c++ has no return type then can it be called a sub routine?
OPs statement is true in general programming.
In C/++, however, everything is a function regardless of return type.
A function should not change the values of actual arguments whereas a subroutine could change them.
is completely false.
It may not be false. Without a definition of the terms you cannot be sure.
Yes, I can. Both functions and subroutines are allowed to change parameters passed by reference as they see fit.
That phrase quoted by the original poster is in a JAVA faq section not C++.
can any JAVA programmers here confirm the validity of that particular statement then or as is mst likely the case - the faq is talking crap?
Last edited on
The quote is talking about functions and subroutines, which suggests it's talking about them from the computer science perspective, and not from that of a particular language.
The distinction in Function/Subroutine comes from Fortran II (maybe others) circa 1960. In Fortran you had to specify that a return value was required by declaring the subprogram to be a FUNCTION. SUBROUTINES could not return a value via the return statement. Both could alter arguments.

C (circa 1970) removed the distinction. All subprograms were functions and by default, return an 'int', This later was altered with the inclusion of 'void' and made subprograms smell and feel that Fortran SUBROUTINES. C++ adopted the convention.

In Ada there is a clear distinction betwen FUNCTIONS and SUBROUTINES. In FUNCTIONS you can not alter arguments (although you can if you use wrappers). In SUBROUTINES you can modify arguments but you can not return values.

Other languages do other things. For C/C++ any FUNCTION can be made a SUBROUTINE by declaring the return value to be 'void'.
great that makes things quite clear
Subroutine also describes a callable section of code with no notion of parameters as seen in Macro Assembers, COBOL and BASIC. They use global variables.
Topic archived. No new replies allowed.