Sub versus Function ? [SOLVED]

New to FreeBASIC? Post your questions here.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Sub versus Function ? [SOLVED]

Post by Trinity »

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 ?
Last edited by Trinity on Sep 29, 2017 16:10, edited 1 time in total.
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Sub versus Function ?

Post by Imortis »

Sub cannot return a value.
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
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Sub versus Function ?

Post by Trinity »

Imortis wrote:Sub cannot return a value.
Function can.
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 :-)
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Sub versus Function ? [SOLVED]

Post by deltarho[1859] »

imortis wrote:Sub cannot return a value.
Function can.
Is true if we write Return rather than return.

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
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Sub versus Function ? [SOLVED]

Post by deltarho[1859] »

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.

Code: Select all

Sub Test5( xPtr as Long Ptr )
*xPtr = 99
End Sub

x = 45
Test5( @x )
Print x
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.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Sub versus Function ? [SOLVED]

Post by Trinity »

deltarho[1859] wrote: It is worth noting, for some, that a Sub can return a value if that value is a parameter passed ByRef.
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).
But thank you for pointing it out.
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.
Thank you for sharing . I find it interesting. I did not know that one could do that :-)
Last edited by Trinity on Sep 30, 2017 10:20, edited 1 time in total.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Sub versus Function ? [SOLVED]

Post by fxm »

Trinity wrote:..... though I noticed that manual said that stings should be referred to ByRef only (at least at the moment).
Where is that written now ?
(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.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Sub versus Function ? [SOLVED]

Post by Trinity »

fxm wrote:
Trinity wrote:..... though I noticed that manual said that stings should be referred to ByRef only (at least at the moment).
Where is that written now ?
(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.
Ref. : https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgSub

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)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sub versus Function ? [SOLVED]

Post by MrSwiss »

Trinity wrote:... any chance that we could also get the FB manual as downloadable linked HTML pages package for off-line browsing ?
Latest available ...

nightly builds: compiler's & doc (.chm)
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Sub versus Function ? [SOLVED]

Post by fxm »

I forgot (since fbc version 1.00.0!) to remove this remark in parentheses on "SUB" page !

I'm doing it now:
KeyPgSub → fxm [Since fbc revision 1.00.0, "ByVal As String" is working]
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Sub versus Function ? [SOLVED]

Post by Trinity »

MrSwiss wrote: Latest available ...
nightly builds: compiler's & doc (.chm)
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.
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)
fxm wrote:I forgot (since fbc version 1.00.0!) to remove this remark in parentheses on "SUB" page !
I'm doing it now:
Thank you , I didn't mean to be bother you !
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sub versus Function ? [SOLVED]

Post by MrSwiss »

Trinity wrote:But an off-line HTML would be brilliant for ...
Sorry, you lost me there ...

.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!?!
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Sub versus Function ? [SOLVED]

Post by deltarho[1859] »

Trinity wrote:and the least one can do is try to consult the manual as best one can prior to asking others for help.
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
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Sub versus Function ? [SOLVED]

Post by Trinity »

MrSwiss wrote:Sorry, you lost me there ...
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.")
(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)
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!?!
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.
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)
deltarho[1859] wrote:
Trinity wrote:and the least one can do is try to consult the manual as best one can prior to asking others for help.
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.
Yeah , well , you are probably right there.
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)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sub versus Function ? [SOLVED]

Post by MrSwiss »

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!)
Post Reply