QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Kiara87 on November 13, 2020, 08:02:03 pm

Title: difference function end function and sub end sub
Post by: Kiara87 on November 13, 2020, 08:02:03 pm
Hello everyone
function
end function
and
sub
end sub
what's the difference?

some practical examples to better understand the functioning of these two instructions where can I find them?
Title: Re: difference function end function and sub end sub
Post by: SMcNeill on November 13, 2020, 09:00:40 pm
Difference is a FUNCTION returns a value, and a SUB doesn’t.

Some existing examples:

FUNCTIONS:  COS(), SIN(), _PRINTWIDTH(): _FONTHEIGHT()

SUBS: CLS, PRINT, BEEP, SLEEP

Think of the difference in how you use the above commands:

CLS
x = SIN(.5)
Y = COS(.5)
PRINT x, y

With CLS you’re calling an internal routine which clears your screen.  It just does something, and doesn’t return any values for you.  SIN and COS, however, do something with the value you give them (.5, in this case), and they return that result to you (as x and y, respectively, in this case).  PRINT, once again, just does something, but doesn’t return any values for you.

FUNCTIONS do something, then return a value to you.

SUBS simply do something, with no value returned after.
Title: Re: difference function end function and sub end sub
Post by: bplus on November 13, 2020, 10:05:06 pm
Code: QB64: [Select]
  1. a = 2: b = 3
  2. add a, b, total ' calling the sub add which outputs the total in 3rd argument
  3. PRINT total
  4. PRINT add2(a, b) 'calling function that returns then sum in it's name
  5.  
  6. ' so maybe you can see, you cannot print the output of a SUB like you can a function.
  7.  
  8.  
  9. SUB add (a, b, sum)
  10.     sum = a + b
  11.  
  12. FUNCTION add2 (a, b)
  13.     add2 = a + b
  14.  
  15.  
  16.  
  17.  
Title: Re: difference function end function and sub end sub
Post by: Kiara87 on November 14, 2020, 10:19:29 am
example of bplus it seems that both are the same but just write the total on the sub you have to add an extra variable while the function needs to call it as the function
Title: Re: difference function end function and sub end sub
Post by: bplus on November 14, 2020, 11:22:59 am
example of bplus it seems that both are the same but just write the total on the sub you have to add an extra variable while the function needs to call it as the function

SUBs and FUNCTIONs are very similar, a FUNCTION does save on one variable if you need something output from the function and it is not an array nor a UDT then a FUNCTION can return your number of string, otherwise I'd use a SUB.

So in the specific case I used, the function would be better. one less variable.
Title: Re: difference function end function and sub end sub
Post by: SMcNeill on November 14, 2020, 02:28:38 pm
example of bplus it seems that both are the same but just write the total on the sub you have to add an extra variable while the function needs to call it as the function

The only way you can pass values by the parameters is if the variable types match.  Don’t rely upon it, unless you specifically code for it.

Code: QB64: [Select]
  1. add 3, 4, total%
  2. add 3, 4, total!
  3. add 3, 4, total&
  4. add 3, 4, total#
  5.  
  6. PRINT total%, total!, total&, total#
  7.  
  8. SUB add (num1, num2, num3)
  9.     num3 = num1 + num2
  10.  

Note2: Also be aware that FUNCTIONS can pass values via the parameter, as long as the variable types match, as well.
Title: Re: difference function end function and sub end sub
Post by: Kiara87 on November 14, 2020, 03:23:00 pm
The only way you can pass values by the parameters is if the variable types match.  Don’t rely upon it, unless you specifically code for it.

Code: QB64: [Select]
  1. add 3, 4, total%
  2. add 3, 4, total!
  3. add 3, 4, total&
  4. add 3, 4, total#
  5.  
  6. PRINT total%, total!, total&, total#
  7.  
  8. SUB add (num1, num2, num3)
  9.     num3 = num1 + num2
  10.  

Note2: Also be aware that FUNCTIONS can pass values via the parameter, as long as the variable types match, as well.

thanks I will practice to better understand and maybe create more complex functions and more complex subs thanks for advice
Title: Re: difference function end function and sub end sub
Post by: Kiara87 on November 14, 2020, 03:27:49 pm
SUBs and FUNCTIONs are very similar, a FUNCTION does save on one variable if you need something output from the function and it is not an array nor a UDT then a FUNCTION can return your number of string, otherwise I'd use a SUB.

So in the specific case I used, the function would be better. one less variable.

thanks @bplus
yes I had noticed that they are similar

I should find programs created with subs to study them I see in the forum
of your programs in the forum there will be hundreds
Title: Re: difference function end function and sub end sub
Post by: TempodiBasic on November 15, 2020, 01:43:32 pm
@SMcNeill
Steve, fine demo! I have found very interesting the output!
I have forgotten that
Code: QB64: [Select]
  1. SUB add (num1, num2, num3)
  2.     num3 = num1 + num2
is this
Code: QB64: [Select]
  1. SUB add (num1!, num2!, num3!)
  2.     num3! = num1! + num2!
thanks for reminder.
Title: Re: difference function end function and sub end sub
Post by: SMcNeill on November 15, 2020, 01:53:47 pm
@SMcNeill
Steve, fine demo! I have found very interesting the output!
I have forgotten that
Code: QB64: [Select]
  1. SUB add (num1, num2, num3)
  2.     num3 = num1 + num2
is this
Code: QB64: [Select]
  1. SUB add (num1!, num2!, num3!)
  2.     num3! = num1! + num2!
thanks for reminder.

It depends on your default variable type. 

! Is the symbol for SINGLE, which is what we have as a default variable type, unless we use a DEFtype or _DEFINE statement.

Add a DEFLNG A-Z to the top of my example, and watch how output changes.
Title: Re: difference function end function and sub end sub
Post by: TempodiBasic on November 15, 2020, 05:51:22 pm
yes, it's just that you have reminded so specifically:
QB64 has ! as default type for variable with no suffix
so only if you change the default type you get different results...
moreover in QB you can create with the same name different type of variable using suffix!
Maybe it is possible because the QB parser uses the same suffix like part of the name and so
total! and total& are 2 different names.
And this is one of the strong reasons to use OPTION _EXPLICIT to avoid issue coming from mistype.