|
Post by bitvast on May 29, 2014 11:31:49 GMT 1
C requires the return value of a function to be explicitly declared in the function header, but in BaCon you don't do this, at least, you can only differentiate between a value and string as it says in the manual:
But in BaCon the default return type for a value is a long int (if the function doesn't return a string), which is why I got the error when trying to RETURN a float directly without using a intermediate variable declared as FLOATING. Am I right?
I think there should be a note in the manual regarding this, to save confusion, even though it may be obvious to C programmers.
|
|
|
Post by bigbass on May 29, 2014 15:06:41 GMT 1
Hey bitvast Even though we have good documentation There is always something we could add to improve things The Bacon function demo wiki.basic-converter.org/index.php?title=Test12.bacHere are some extra demos if you want to look at them later wiki.basic-converter.org/index.php?title=DemoThe function from C to BaCon question Let's port a code example on the web from a C tutorial to BaCon code The C function example (a reference to the ported BaCon code) www.tutorialspoint.com/cprogramming/c_functions.htmHere is the BaCon function after removing the extra C crud somtimes it's better with a code example it can be compared what is needed from C and how we can reduce the code in BaCon We all want reduced simple to read and use code That's why we still prefer to port code than use ugly C syntax Joe Here is the ported to BaCon code function2.bac
'--- function returning the max between two numbers '====================================================== FUNCTION max(NUMBER num1, NUMBER num2) '====================================================== '--- local variable declaration LOCAL result TYPE NUMBER IF (num1 > num2) THEN result = num1 ELSE result = num2 END IF RETURN result END FUNCTION
'--- calling a function to get max value
PRINT "Max value is : ", max(100, 200)
|
|
|
Post by bitvast on May 29, 2014 17:19:25 GMT 1
Thanks, Joe. The point I was trying to make was that there are two types of "value" - either NUMBER or FLOATING, but BaCon assumes NUMBER is returned from a function. FUNCTION mult1(FLOATING a, FLOATING b) RETURN a * b END FUNCTION
PRINT mult1(2, 3)
FUNCTION mult2(FLOATING a, FLOATING b) RETURN a * b END FUNCTION
PRINT mult2(2.5, 3.5)
FUNCTION mult3(FLOATING a, FLOATING b) LOCAL product TYPE FLOATING product = a * b RETURN product END FUNCTION
PRINT mult3(2.5, 3.5)
Output: In function mult1() the correct answer is returned because 2 x 3 isn't FLOATING. In function mult2() the returned answer is incorrect because the inputs aren't integers and the function doesn't assign the product to a FLOATING type. In function mult3() the returned answer is correct because the product is first assigned to a FLOATING variable then returned. So if you didn't know that when a function returns a value directly (as in RETURN <expression> ) it will always be an integer then you might be puzzled if you were expecting a float. Maybe I'm making a big deal out of this, but the language I'm used to was much simpler regarding types, so I didn't have to think about them much.
|
|
|
Post by alexfish on May 29, 2014 20:35:51 GMT 1
Hi Bitvast
here going to look at a bit of C code , since we know Bacon is a basic to c translator
float mult3(float x, float y) // Returns value (x, y) { float product; //local variable product = (x * y); return product; }
in c one normally prototypes the function to return a value
IE this line
float mult1(float, float); // function prototype
then in the actual function there is a local var 'product' this returns the value , and when the function is no longer used the local does not exist
so yes U'r example works
FUNCTION mult3(FLOATING a, FLOATING b) LOCAL product TYPE FLOATING product = a * b RETURN product END FUNCTION
PRINT mult3(2.5, 3.5)
in U'r multi1 example
the function has no local nor scope , so here bacon has to make a decision on what the prototype is
assuming bacon is returning int as a default.
perhaps may be another one for Peter . with his variable recognition feature , but in general terms it is often best in any language to Declare exactly what U want
BR Alex
|
|
|
Post by vovchik on May 29, 2014 21:09:38 GMT 1
Dear Alex, You are completely right. That technique is also very useful in restaurants. With kind regards, vovchik PS. As you know, I am a pathological fussbudget, too.
|
|
|
Post by alexfish on May 29, 2014 21:29:44 GMT 1
Hi Vovchik
PS we can cheat a bit with DEF FN
@ bitvast
can try bits with DEF FN , there brill in bacon since they are written directly to c as a macro function
here is an example , think also there are a couple of examples in the docs
DEF FN my_mul(x,y) = x*y
PRINT my_mul(1.9,4.9)
BR Alex
|
|
|
Post by bitvast on May 29, 2014 21:35:22 GMT 1
Hi Alex,
This code also works though:
#include<stdio.h> float mult1(float x, float y); int main() { printf ( "%1.2f", mult1(2.5, 3.5)); } float mult1(float x, float y) { return (x * y); } So how does the program know to return a float rather than an int? It's because the return type is given in the function prototype.
In BaCon though, there is no function prototype; the value returned can either be a string or a value, and the value is always an int. I'm just saying that a note about this in the manual might be useful and save some head-scratching for newbies.
|
|
|
Post by alexfish on May 29, 2014 22:01:49 GMT 1
Hi bitvast
think U are correct with that one IE : the docs need updating . since Peter has already mentioned that variables have to be declared with the subs or functions on the BaCon Beta3 thread
BR Alex
|
|
|
Post by Pjot on May 30, 2014 6:52:25 GMT 1
Folks,
In case of a LET, so in case of a plain assignment, BaCon can cast the type of the expression to the type of the variable to which the result is assigned.
So a RETURN of a plain variable works, because BaCon looks up the type of the variable in its internal administration and then it can set the type of the function correctly.
But when RETURNing an expression, the problem is that BaCon cannot see from an expression what the type of the result will be. C does not evaluate in advance, because it is not an interpreter. But at the same time C needs to know this type, to set the type of the function. So in C, a programmer has to set the type of the function himself, manually.
We could extend the FUNCTION statement with a TYPE to declare the type of the function manually also?
BR Peter
|
|
|
Post by bitvast on May 30, 2014 7:35:41 GMT 1
We could extend the FUNCTION statement with a TYPE to declare the type of the function manually also? BR Peter Hi Peter, Just a suggestion, some of the old BASICS like qbasic used data type suffixes. Here's a list of them given in the FreeBASIC docs which can be used when compiling with the "-lang qb" option. Not suggesting you implement all of these for variables but maybe just for the return type of a function. "$" is already appended to the name of a string function so how about using "#" and "&" for FLOATING and NUMBER respectively?
|
|
|
Post by Pjot on May 30, 2014 22:23:53 GMT 1
Thanks bitvast,
That is not such a bad idea. When it comes to numbers, the concept of BaCon is that there are three types (numeric, float, string), though there is no way to distinguish properly between float and integer.
I investigated the idea of putting everything to float, however, C does not like this; for example indexes in arrays *must* be integer type.
We could introduce an optional '%' for integers and an optional '#' for floats?
Also, the optional TYPE keyword for FUNCTION would be suitable, as this is in line with the whole BaCon logic. Let's do both to keep everybody happy ;-)
Regards Peter
|
|
|
Post by bitvast on May 31, 2014 7:33:37 GMT 1
We could introduce an optional '%' for integers and an optional '#' for floats? Also, the optional TYPE keyword for FUNCTION would be suitable, as this is in line with the whole BaCon logic. Let's do both to keep everybody happy ;-) Regards Peter Cool! Thanks, Peter. So, are you doing the &, # for variables too, or just for function names? I like the brevity of var& / var# instead of DECLARE var TYPE NUMBER / FLOATING
|
|
|
Post by vovchik on May 31, 2014 12:41:07 GMT 1
Dear bitvast,
Using "&" may be problematic for vars. It is already a string concatenating operator, a var address pointer and a binary "and".
With kind regards, vovchik
|
|
|
Post by Pjot on May 31, 2014 21:56:55 GMT 1
Hi bitvast, As usual vovchik is right - the '&' symbol cannot be used. However, for integer numbers I myself am used to the '%' symbol as a variable suffix. So if it is OK with you, we could use that too. Then for float we can use the '#' symbol. Well, we can add it variables too, provided that you have to use such variable name with a '%' or an '#' attached in each occasion. So, once you start using the '%' suffix or the '#' suffix for a variable, you have to use this suffix in each occasion where the variable is used. Then, like with strings, "var$" is different from the variable "var". Similarly, "var%" will be different from "var" en "var#" is different from "var". In other words, the '$', '%' and '#' suffix have to be used consequently. If this is OK with you, then technically there is no problem adding this functionality to BaCon. BR Peter
|
|
|
Post by bitvast on Jun 1, 2014 8:33:17 GMT 1
Hi Peter, It all sounds good to me, and having to use the suffix wherever the variable is used will probably result in fewer bugs.
|
|