Author Topic: difference function end function and sub end sub  (Read 3779 times)

0 Members and 1 Guest are viewing this topic.

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
difference function end function and sub end sub
« 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?
« Last Edit: November 13, 2020, 08:34:34 pm by Kiara87 »
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: difference function end function and sub end sub
« Reply #1 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: difference function end function and sub end sub
« Reply #2 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.  

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: difference function end function and sub end sub
« Reply #3 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
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: difference function end function and sub end sub
« Reply #4 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.
« Last Edit: November 14, 2020, 11:27:52 am by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: difference function end function and sub end sub
« Reply #5 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.
« Last Edit: November 14, 2020, 02:32:33 pm by odin »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: difference function end function and sub end sub
« Reply #6 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
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: difference function end function and sub end sub
« Reply #7 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
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: difference function end function and sub end sub
« Reply #8 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.
Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: difference function end function and sub end sub
« Reply #9 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: difference function end function and sub end sub
« Reply #10 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.

Programming isn't difficult, only it's  consuming time and coffee