Embedded function calls

New to FreeBASIC? Post your questions here.
Flyzone
Posts: 37
Joined: Nov 17, 2017 17:39

Embedded function calls

Postby Flyzone » Mar 25, 2021 21:13

Must functions be compiled separately? I haven't needed to do that or use pre-complied modules before. I'm able to compile a function used with my main program together, but if I try to use another embedded function (within the first function) I can't do it since the required DECLARE that is needed before using a function is prohibited within it.

Got that?

Is that just the way it is?

Code: Select all

'Print ABC
declare function BC (rv as string) as string
dim s as string
s="A"
s=BC(s)
?s
sleep
end

function BC (rv as string) as string
declare function c (rv as string) as string  'Apparently not legit
    rv=rv+"B"
    rv=c(rv)
    BC=rv
end function

function C (rv as string) as string
    rv=rv+"C"
    C=rv
end function
MrSwiss
Posts: 3826
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Embedded function calls

Postby MrSwiss » Mar 25, 2021 21:52

Flyzone wrote:... I can't do it since the required DECLARE that is needed before using a function is prohibited within it.
Declarations must be done for each and every procedure (Sub/Function) before it can be called.
Except, the implementation is made, before it is called the first time (before main-code).

It's one of those "properly ordering" sort of things ...
badidea
Posts: 2333
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Embedded function calls

Postby badidea » Mar 25, 2021 22:02

Flyzone wrote:Got that?

Not really, but what is more crazy is that this 'program' segfaults here (fbc 1.07.2 32-bit & 64-bit linux):

Code: Select all

function BC (rv as string) as string
   BC = rv
end function

(program exited with code: 139)


Edit: This seems a bug only in the compiler for linux. Moved issue here: viewtopic.php?f=17&t=27814&p=281649#p281649
Last edited by badidea on Mar 25, 2021 23:05, edited 6 times in total.
MrSwiss
Posts: 3826
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Embedded function calls

Postby MrSwiss » Mar 25, 2021 22:06

Properly ordered code:

Code: Select all

'Print ABC
declare function BC (rv as string) as String
Declare Function  C (rv as string) as String

dim s as string
s="A"
s=BC(s)
?s
sleep : end

function BC (rv as string) as string
    rv=rv+"B"
    rv=c(rv)
    BC=rv
end function

function C (rv as string) as string
    rv=rv+"C"
    C=rv
end function
Flyzone
Posts: 37
Joined: Nov 17, 2017 17:39

Re: Embedded function calls

Postby Flyzone » Mar 25, 2021 22:17

Right. Got that. I don't mind doing that in the main program for first level calls but seems I shouldn't have to do that for lower level calls.

But I guess that's just the way it is.

(I didn't get the Seg fault but did get a compiler error on the DECLARE unless I moved it to the top).
MrSwiss
Posts: 3826
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Embedded function calls

Postby MrSwiss » Mar 25, 2021 22:23

Flyzone wrote:... but seems I shouldn't have to do that for lower level calls.
How do you think, the compiler knows what you "declare" first, second or other code-level ???
Flyzone
Posts: 37
Joined: Nov 17, 2017 17:39

Re: Embedded function calls

Postby Flyzone » Mar 25, 2021 22:54

MrSwiss wrote:
Flyzone wrote:... but seems I shouldn't have to do that for lower level calls.
How do you think, the compiler knows what you "declare" first, second or other code-level ???


Well, at the risk of getting in trouble here, it should accept the DECLARE in the first lower level function (as I originally wrote in my first post) which would satisfy that requirement -- but it doesn't (It should) -- so I'll just have to live with that.
paul doe
Moderator
Posts: 1430
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Embedded function calls

Postby paul doe » Mar 26, 2021 9:53

Flyzone wrote:Well, at the risk of getting in trouble here, it should accept the DECLARE in the first lower level function (as I originally wrote in my first post) which would satisfy that requirement -- but it doesn't (It should) -- so I'll just have to live with that.

Why? Declarations need to be put at the global scope; they're just that, declarations. FreeBasic, as many other BASIC dialects, does not support nested procedures. Also, you don't need to declare anything if you define before using. In your case:

Code: Select all

function C (rv as string) as string
  rv=rv+"C"
  return( rv )
end function

function BC (rv as string) as string
  rv=rv+"B"
  rv=c(rv)
  return( rv )
end function

'Print ABC
dim s as string
s="A"
s=BC(s)
?s
sleep
end
Flyzone
Posts: 37
Joined: Nov 17, 2017 17:39

Re: Embedded function calls

Postby Flyzone » Mar 26, 2021 13:55

OK.

Somehow I did know that but didn't try it since I'm so used to putting stuff after the mainline code. Tho a bit awkward (for me), but really not so much, that does solve my problem of "out of context" coding.

Another thing I haven't tried is, and I am assuming that I could compile and store such functions in a linklib and reference procedures in similar fashion without declares and just an include filespec. Right?
MrSwiss
Posts: 3826
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Embedded function calls

Postby MrSwiss » Mar 26, 2021 14:31

Flyzone wrote:Another thing I haven't tried is, and I am assuming that I could compile and store such functions in a linklib and reference procedures in similar fashion without declares and just an include filespec. Right?
Nope, won't work. The important part here is: compile.

Just #include (code, uncompiled) would sort of, do the trick.
Provided the #include is made at the top of the code-file.
(A compiled library typically has the necessary Declarations in the .bi-file that also loads the lib.)
Flyzone
Posts: 37
Joined: Nov 17, 2017 17:39

Re: Embedded function calls

Postby Flyzone » Mar 26, 2021 15:15

Yes, I assumed compiled tho I guess I used the wrong terminology. So, a .bi file can be created and the function(s) then stored in a lib.bi somehow then referenced without fanfare in another source. Perhaps I need some sort of EXTERN statement (or not would be better) though there is still the matter of whether the DECLAREs would still be required or not.

I am used to using "old school" 2 pass compilers which were much more convenient and did all this stuff for you. With processors so fast and cheap today is seems to me we got it backwards with one-pass. Yes, they're faster but whose work are we trying to speed up? But that's just ole' me I guess...
MrSwiss
Posts: 3826
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Embedded function calls

Postby MrSwiss » Mar 26, 2021 15:27

EXTERN, is only needed in compiled libraries (static/dynamic linking).

You can also simply #include "blah.bas" (no requirement to call it .bi).

Since the implementation (others may call it definition) is now first, before main-code, no declarations are needed.
caseih
Posts: 1705
Joined: Feb 26, 2007 5:32

Re: Embedded function calls

Postby caseih » Mar 26, 2021 19:01

Flyzone wrote:Yes, I assumed compiled tho I guess I used the wrong terminology. So, a .bi file can be created and the function(s) then stored in a lib.bi somehow then referenced without fanfare in another source. Perhaps I need some sort of EXTERN statement (or not would be better) though there is still the matter of whether the DECLAREs would still be required or not.

I am used to using "old school" 2 pass compilers which were much more convenient and did all this stuff for you. With processors so fast and cheap today is seems to me we got it backwards with one-pass. Yes, they're faster but whose work are we trying to speed up? But that's just ole' me I guess...

I'm not sure what the source of your difficulties here is. Are you familiar with other languages? The way FB works is no different than C, for example. A bi file contains prototypes and structures that the user of a library needs to know in order to call the library functions. This includes the "declare function" and "declare sub" statements. The dll contains the actual implementation of the library functions and could be compiled from a completely different language like C. In short, how FB behaves is nearly identical to C where .bi files are called "header" .h files. Some C compilers may allow you to forward reference a function in the same source code unit (file) without an advance declaration. Others do not. I'm not able to determine what the C standard has to say on the matter, but all code I've ever looked at defines the function prototype before any forward references are made. Certainly to use any kind of library you need a header file or a .bi file to tell the compiler how to build the structures you'll need and tell the compiler what the function names are and how they are called.

On the issue of forward referencing, apparently Quick Basic had no issue with forward referencing a function without any sort of advanced declaration. FB does not allow that, nor can I think of a good reason to allow that.

Modern compilers tend to be single pass for initial parsing, and then after that there might be multiple passes through the abstract syntax tree for a variety of reasons including symbol type resolution and optimization.
Flyzone
Posts: 37
Joined: Nov 17, 2017 17:39

Re: Embedded function calls

Postby Flyzone » Mar 27, 2021 14:36

apparently Quick Basic had no issue with forward referencing a function without any sort of advanced declaration. FB does not allow that, nor can I think of a good reason to allow that.

You can't? You just mentioned two of them. There are others ...

Never programmed in C; never will. You are obviously much younger and the 6 or so languages I have programmed in predate you but the technology has changed considerably and after many years of non-participation I am toying with FB keeping one foot in the past on placing another in today's tech. I hope they don't try to make FB a C lookalike-workalike. I actually heard John Kemeny (originator of Basic) speak and he'd be horrified. However, I like what they've done with FB so far.

Thanks for the info on libs. I'll try them out even though a source include would be easiest but I'd like to recreate what we used to call relocatable modules/libraries just to see how same/different they might be. Some things haven't changed at all.
Last edited by Flyzone on Mar 27, 2021 15:17, edited 1 time in total.
coderJeff
Site Admin
Posts: 3507
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Embedded function calls

Postby coderJeff » Mar 27, 2021 15:12

apparently Quick Basic had no issue with forward referencing a function without any sort of advanced declaration.

QB and QBASIC automatically add the procedure declarations when the IDE saves the file. If you were to use an editor on the file directly or remove the declarations automatically saved, QB's command line compiler 'BC' would throw an error if the procedure was used without a declaration or or before definition.

One of the tricks for limiting the amount of memory consumed by the QB IDE was to off-load the declares to include files and put some modules in libraries.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 6 guests