|
Post by rikky on Dec 31, 2023 16:28:29 GMT 1
Hello I have this function: FUNCTION bla(string$) TYPE int PRINT "hallo" RETURN 1 END FUNCTION But when I accidentally forget to write the string$, the program does compile. So my program: FUNCTION bla(string$) TYPE int PRINT "hallo" RETURN 1 END FUNCTION
PRINT IF bla THEN PRINT "dag" PRINT IF bla("blabla") THEN PRINT "dag" PRINT returns:
dag
hallo dag
So you can see that the first call to the function bla, did not even do the function, for 'hallo" is not printed. But it did not return an error either. Oh, and B4 I forget, Happy new year, Rik.
|
|
|
Post by Pjot on Jan 1, 2024 9:02:55 GMT 1
Hi Rik, The program works as it should, it is somewhat confusing. So what happens at this line: IF bla THEN PRINT "dag"
However, if you would like to invoke the function called "bla" then you must add parentheses, as follows: IF bla() THEN PRINT "dag"
This will actually try to make a call to the function, and as you correctly expect, the compilation process will start complaining because the function "bla()" needs an argument. So how come that the same line without parentheses still works in the compilation process? Reason is that the C compiler thinks that you are referring to the address of the function "bla". And the address by itself is some number (not 0) and therefore the IF statement will perform the function. You will see the same thing happening in plain C code: #include <stdio.h>
int bla (char *string) { printf ("hallo\n"); return (1); }
int main () { printf ("\n");
if (bla) /* <---------------- the 'if' you are referring to */ { printf ("dag\n"); }
printf ("\n");
if (bla ("blabla")) { printf ("dag\n"); }
printf ("\n"); }
You can compile this with 'gcc -o prog prog.c' and you will notice that GCC has no problem with this code at all. So from technical point of view we could consider this "as expected". But there is a way to let GCC warn us about such construction. You can add a GCC parameter '-Waddress' to the BaCon compile code: # bacon -o -Waddress -y debug Converting 'debug.bac'... done, 10 lines were processed in 0.001 seconds. Creating lexical analyzer using flex... done. Compiling 'debug.bac'... cc -Waddress -c debug.bac.c cc -o debug debug.bac.o -lm debug.bac:7:4: warning: the address of 'bla' will always evaluate as 'true' [-Waddress]
INFO: see full error report (y/[n])? y debug.bac: In function 'main': debug.bac:7:4: warning: the address of 'bla' will always evaluate as 'true' [-Waddress] 7 | IF bla THEN PRINT "dag" | ^~~
As you can see, the address of the function "bla" will always be true (e.g. not 0).
Hope this explains it all,
Best wishes, Peter
|
|
|
Post by rikky on Jan 1, 2024 9:40:37 GMT 1
Aha, indeed. If I put POKE bla,6 between the function and the main program, then I get: ERROR: signal for SEGMENTATION FAULT received - memory invalid or array out of bounds? Try to compile the program with TRAP LOCAL to find the cause. Thanks for explaining this.
|
|