| [reply] [d/l] |
Is there really any formal distinction here in any language?
Certainly in Fortran at least there is. The declarative statements FUNCTION and SUBROUTINE define two different types of subprogram. The former returns a value and is invoked like
X = SIN(Y)
whereas the latter does not and is invoked like
CALL RANDOM_NUMBER(A)
They serve distinct purposes. HTH.
| [reply] [d/l] [select] |
Re^5: I'm trying to consolidate my functions into subroutines and Re^5: I'm trying to consolidate my functions into subroutines are already good answers.
Old home computer BASICs (those with line numbers, e.g. C64, ZX 81, ZX Spectrum) have functions and subroutines. Functions (like SIN, COS, PEEK, RAND) are build-in, and there is usually no way to create user-defined functions. Procedures are simply some lines of code called by GOSUB and ending with RETURN, not able to return a value except by modifying global variables.
Pascal and Fortran share the distinction between functions and procedures, forcing the programmer to check the return value, or at least use it in some way. Common workarounds are IFs without any code in the THEN block, or using the unwanted return value as input for a procedure with nearly no side-effects. In Sinclair BASICs (see below), you often see RAND USR 16514 to call machine code at address 16514 and discarding the return value. Actually, the return value is used to initialise the random number generator. But often, the machine code simply does not return, and so even PRINT USR 16514 can be found.
C and C++ have, from a syntactical point of view, only functions. But strictly speaking, any function returning void does just that: It returns nothing and thus is a procedure. Unlike Pascal, Fortran, and the old home computer BASICs, C and C++ allow calling any function like a procedure (i.e. fooBar(); instead of someVar=fooBar();), discarding any return value. This is the source of many bugs, because often functions return an error code or a success indicator; and lazy programmers tend to ignore the possibility of an error, especially in seemingly trivial functions. It is even possible to call procedures as function. Of course, this results in undefined behavior, but gcc accepts that without any warnings, even with -Wall and -pedantic. The logic is simple: You are the programmer, you are explicitly casting, so you must know what you are doing.
/tmp>cat evil.c
#include <stdio.h>
#include <stdlib.h>
typedef int (*func_returning_int)(void);
typedef void (*func_returning_void)(void);
void dummy(void)
{
puts("Hello World");
}
int main(int argc, char ** argv)
{
func_returning_void f = &dummy;
int i = 0;
f();
i = ((func_returning_int)f)();
printf("i=%i\n",i);
return 0;
}
/tmp>CFLAGS="-Wall -pedantic" make evil
cc -Wall -pedantic evil.c -o evil
/tmp>./evil
Hello World
Hello World
i=12
/tmp>
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] [d/l] [select] |
It is even possible to call procedures as function. Of course, this results in undefined behavior,
C99 says, If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
(There is, however, an exception for the main() function that has an implicit "return 0" before closing brace.)
Further, the C standard does not define or use such terminology "procedure" or "subroutine". These words no not even appear in remarks or informal parts. I think this is a very deliberate choice.
The logic or rationale behind those dangerous jump statements (goto/return/break/continue) is that the compiler is unable to follow the program flow or value propagation in the absolute sense. Gcc may warn that a variable may be used uninitialized, while the programmer can easily see this can not happen. The compiler may not even know if the return value was properly computed or if undefined behavior was somehow involved. Thus C does not prohibit questionable constructs such as goto into a scope of a variable (past its initialization).
But note, even when "may be uninitialized" warning is a false positive, it has informational value for the programmer. It may signal a refactoring opportunity, such that would allow the compiler better insight into your program and possibly better code as a result.
| [reply] [d/l] [select] |
I should have worded it "a function or a 'pure' subroutine" as I only meant "pure" to apply to subroutine. By "pure subroutine",
I mean it doesn't return a result. (Which necessarily means, to be useful, it has side effects.1)
As for subroutines vs functions in C, it's really the absence or presence of a return statement that distinguishes a subroutine from a function. If there is no return statement, nothing will be returned. Many C compilers, if the returned type is not void will complain about the lack of a return statement. Likewise, will complain when a void routine does have a return
I chose C because that's one of my 2 main programming languages (the other being Perl). I have passing familiarity with languages that make a more explicit distinction between subroutines and functions, such as Pascal (which another monk mentioned).
---
1A real world example of a useful subroutine is:
void Motor(int speed, enum eDIR dir) { ... }
Which tells the hardware how fast and which direction to run a motor.
| [reply] [d/l] [select] |
what is an example of a Perl builtin or even in a module function that is pure? | [reply] |
| [reply] |