Sub versus Function ? [SOLVED]
Sub versus Function ? [SOLVED]
Hello ,
I have spent a lot of time trying to find the difference between Function and Sub by reading the manual. And I can pack the same code into either a Sub or a Function.
From what I can see then Sub allows for the use of Constructor and Destructor (Please do *not* attempt to explain the use of Constructor and Destructor to me now - my head is spinning already)
And Function allows for the use of "Calling Convention" (also something mind boggling to me right now)
But apart from that , then what I would really like to know is when (and why if) is it most beneficial to choose Function over Sub ? and Sub over Function ?
What are the major differences and advantages from one over the other ? Has it got to do with if variables are only local or shared globally ?
I have spent a lot of time trying to find the difference between Function and Sub by reading the manual. And I can pack the same code into either a Sub or a Function.
From what I can see then Sub allows for the use of Constructor and Destructor (Please do *not* attempt to explain the use of Constructor and Destructor to me now - my head is spinning already)
And Function allows for the use of "Calling Convention" (also something mind boggling to me right now)
But apart from that , then what I would really like to know is when (and why if) is it most beneficial to choose Function over Sub ? and Sub over Function ?
What are the major differences and advantages from one over the other ? Has it got to do with if variables are only local or shared globally ?
Last edited by Trinity on Sep 29, 2017 16:10, edited 1 time in total.
Re: Sub versus Function ?
Sub cannot return a value.
Function can.
Here is an example:
Function can.
Here is an example:
Code: Select all
function Test1() as string
return "Hello from Function"
end function
sub Test2()
Print "Hello from Sub"
end sub
Print Test1() 'prints the "return" value
Print Test2() 'will error because there is no return value
sleep
Re: Sub versus Function ?
Thank you , I had noticed that it said in the manual that Gosub were no longer available in FB and that return In the -lang fb dialect Return always means return-from-procedure . But I had over looked that Sub could not itself return a value. I had expected other differences, but thank you :-)Imortis wrote:Sub cannot return a value.
Function can.
-
- Posts: 4310
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Sub versus Function ? [SOLVED]
Is true if we write Return rather than return.imortis wrote:Sub cannot return a value.
Function can.
It is worth noting, for some, that a Sub can return a value if that value is a parameter passed ByRef.
Code: Select all
Dim As Long x
Sub Test3( x As Long )
x = 99
End Sub
Sub Test4( Byref x As Long)
x = 99
End Sub
x = 45
Test3( x )
Print x ' x is unchanged at 45 because x is passed ByVal by default
Test4( x ) ' x is changed to 99 because x is passed ByRef
Print x
Sleep
-
- Posts: 4310
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Sub versus Function ? [SOLVED]
I'll probably get shot down in flames for muddying the waters but instead of passing x ByRef we could pass it's address ByVal.
This will also change x to 99 in the calling code. Admittedly, not often used but a lot of the Windows APIs return values via their address.
Code: Select all
Sub Test5( xPtr as Long Ptr )
*xPtr = 99
End Sub
x = 45
Test5( @x )
Print x
Re: Sub versus Function ? [SOLVED]
Yes , I noticed that too yesterday when reading manual. I think that is the general reason for using ByVal and ByREf respectively , though I noticed that manual said that strings should be referred to ByRef only (at least at the moment).deltarho[1859] wrote: It is worth noting, for some, that a Sub can return a value if that value is a parameter passed ByRef.
But thank you for pointing it out.
Thank you for sharing . I find it interesting. I did not know that one could do that :-)deltarho[1859] wrote:I'll probably get shot down in flames for muddying the waters but instead of passing x ByRef we could pass it's address ByVal.
Admittedly, not often used but a lot of the Windows APIs return values via their address.
Last edited by Trinity on Sep 30, 2017 10:20, edited 1 time in total.
Re: Sub versus Function ? [SOLVED]
Where is that written now ?Trinity wrote:..... though I noticed that manual said that stings should be referred to ByRef only (at least at the moment).
(only for fbc rev < 1.00.0 !)
Strings are passed by ByRef mode by default, but you can now (for fbc rev >= 1.00.0) explicitly request the ByVal mode.
Re: Sub versus Function ? [SOLVED]
Ref. : https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgSubfxm wrote:Where is that written now ?Trinity wrote:..... though I noticed that manual said that stings should be referred to ByRef only (at least at the moment).
(only for fbc rev < 1.00.0 !)
Strings are passed by ByRef mode by default, but you can now (for fbc rev >= 1.00.0) explicitly request the ByVal mode.
Quote : SUB : "FBWiki : KeyPgSub" :
A subroutine can also specify how parameters are passed, either as "Byref" or "Byval", as shown in the syntax definition. If a parameter is "Byref", the parameter name literally becomes a reference to the original variable passed to the subroutine. Any changes made to that variable will be reflected outside of the subroutine. If a parameter is passed "Byval", however, the value of any passed variable is copied into a new variable, and any changes made to it will not affect the original. (Note: this does not currently apply to Strings, and "Byval" should be avoided with them for the time being.)
(emphasis in bold by me)
I could not interpret that otherwise than "that strings should be referred to ByRef only (at least at the moment)".
(I might be a stickler for using the manual and I might ask many questions to people here. But I think that the manual is one of our most important tools to learn the language and the least one can do is try to consult the manual as best one can prior to asking others for help. And as the topic for this thread is "Sub versus Function" , then as you can see I read among other about Sub ;-) :-D )
(By the way , FXM , any chance that we could also get the FB manual as downloadable linked HTML pages package for off-line browsing ? , that would be of help when/if using FB on a non-networked computer)
Re: Sub versus Function ? [SOLVED]
Latest available ...Trinity wrote:... any chance that we could also get the FB manual as downloadable linked HTML pages package for off-line browsing ?
nightly builds: compiler's & doc (.chm)
Re: Sub versus Function ? [SOLVED]
Thank you MrSwiss , yes , I am aware of the updated .chm , but I use both .chm and HTML . CHM for it's brilliant search function and fast navigation and HTML (on-line) for it's ability to having many tabs open when reading on a subject that requires knowledge of other stuff or when comparing stuff. Also the on-line HTML is needed for manual reference here.MrSwiss wrote: Latest available ...
nightly builds: compiler's & doc (.chm)
But an off-line HTML would be brilliant for programming and studying FB on a non-networked computer or a lap-top where one not always have internet access. (it wouldn't have to be updated very often , an updated issue like once every six months would be plenty for me)
Thank you , I didn't mean to be bother you !fxm wrote:I forgot (since fbc version 1.00.0!) to remove this remark in parentheses on "SUB" page !
I'm doing it now:
Re: Sub versus Function ? [SOLVED]
Sorry, you lost me there ...Trinity wrote:But an off-line HTML would be brilliant for ...
.chm is compiled HTML (the original is: online HTML) and, with a 3rd. party viewer (maybe not default)
you might even be able to convert back to HTML (KCHM-Viewer, iirc).
btw: you can have more than one page open, at the same time, if that's the question!?!
-
- Posts: 4310
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Sub versus Function ? [SOLVED]
Over the years I have noticed that those who turn to programming forums as their first port of call rarely get beyond mediocrity as a coder; the reason being they often get a specific answer to their question and so miss out on a fuller description in the manual.Trinity wrote:and the least one can do is try to consult the manual as best one can prior to asking others for help.
Re: Sub versus Function ? [SOLVED]
And you me ! :-D (can not understand what was to miss (ref. : "an off-line HTML would be brilliant for programming and studying FB on a non-networked computer or a lap-top where one not always have internet access.")MrSwiss wrote:Sorry, you lost me there ...
(I expect to move some of my programming to an off-line computer at first opportunity)(first opportunity will probably not be in a long time - if you know how poorly I live)
Yes , I have been aware of the conversion possibility for many years , but all programs I used to try to use for CHM conversion to HTML did a really crappy convert back job , which I also would find a waste of time if someone could just zip the HTML pages directly and hand them to us.MrSwiss wrote: .chm is compiled HTML (the original is: online HTML) and, with a 3rd. party viewer (maybe not default)
you might even be able to convert back to HTML (KCHM-Viewer, iirc).
btw: you can have more than one page open, at the same time, if that's the question!?!
I never heard of KCHM-Viewer and iirc . So just tested KCHM-Viewer . Yes , KCHM-Viewer will allow for more tabs , but I constantly would have to click the plus sign as if I try to use right click : open in new tab then it comes with something crap about it wants to open an external program ! So it seem to be rather clunky still (using Windows 7 , 64 bit here)
And the "mIRC" , after installing it look as some on-line irc browser and I do not see anything helpful in manual about opening local CHM files (?)
Anyway , if I could find a free CHM viewer that is good enough that would allow for multi tab browsing using right click open in new tab then that would completely satisfy my needs for off-line use I guess... (if can run on XP)
Yeah , well , you are probably right there.deltarho[1859] wrote:Over the years I have noticed that those who turn to programming forums as their first port of call rarely get beyond mediocrity as a coder; the reason being they often get a specific answer to their question and so miss out on a fuller description in the manual.Trinity wrote:and the least one can do is try to consult the manual as best one can prior to asking others for help.
Once upon a time I used to use the R-T-F-M expression both here and there when people asked question that showed that they didn't bother to read instructions or manual , but for some reason I never really got popular on that one :-D ( LOL and ROTFL)
Re: Sub versus Function ? [SOLVED]
iirc = if I remember correctly (abbreviation, forum language)
XP --> I would NOT any longer rely on XP (since a lot of VISTA and later Win-Interfaces, may be used)
Example: WinFBE (currently under construction), NO support for < VISTA ... (a developer's decision!)
XP --> I would NOT any longer rely on XP (since a lot of VISTA and later Win-Interfaces, may be used)
Example: WinFBE (currently under construction), NO support for < VISTA ... (a developer's decision!)