function v/s subroutine

Apr 8, 2009 at 1:43pm
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

Apr 8, 2009 at 1:48pm
No.

What is your defintion of a function and a subroutine?
Apr 8, 2009 at 1:52pm
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 Apr 8, 2009 at 1:52pm
Apr 8, 2009 at 2:16pm
so if a function in c++ has no return type then can it be called a sub routine?
Apr 8, 2009 at 2:21pm
OPs statement is true in general programming.
In C/++, however, everything is a function regardless of return type.
However,
A function should not change the values of actual arguments whereas a subroutine could change them.
is completely false.
Apr 8, 2009 at 3:21pm
It may not be false. Without a definition of the terms you cannot be sure.
Apr 8, 2009 at 4:13pm
Yes, I can. Both functions and subroutines are allowed to change parameters passed by reference as they see fit.
Apr 8, 2009 at 6:21pm
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 Apr 8, 2009 at 6:27pm
Apr 8, 2009 at 10:48pm
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.
Apr 9, 2009 at 1:49am
Apr 9, 2009 at 3:18am
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'.
Apr 9, 2009 at 4:41am
great that makes things quite clear
Apr 9, 2009 at 9:27am
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.