Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why do we need Functions?
#7
The difference is rather simple:

SUBS **do** something. FUNCTIONS **return** something.

CLS, PRINT, BEEP -- these are all subs as they do something.

_KEYHIT, _MOUSEINPUT, INKEY$ -- these are all functions as they return something.

Now, you talk about returning values via parameter, and this is true -- but only in limited scope as the parameter types must match.

SUB foo (x AS INTEGER) will pass the value of x to the reference variable ONLY if it's an integer. Pass it a byte, long, constant, single, or anything else, and that value won't pass back.

FUNCTION, on the other hand, always returns a given type value.

FUNCTION foo% (x) -- pass it a byte, long, single, const, literal... doesn't matter! You'll still get back an integer value!

**************

As for the biggest difference between the two, I'd say that's obvious with this little example:

PRINT TIMER

Print is a SUB. Timer is a function. Yet, the two are working together here on the same line!! Try that with any two SUBs and see what happens!

PRINT CLS
Reply


Messages In This Thread
Why do we need Functions? - by PhilOfPerth - 08-19-2022, 01:00 AM
RE: Why do we need Functions? - by OldMoses - 08-19-2022, 01:44 AM
RE: Why do we need Functions? - by bplus - 08-19-2022, 02:05 AM
RE: Why do we need Functions? - by OldMoses - 08-19-2022, 02:12 AM
RE: Why do we need Functions? - by vince - 08-19-2022, 03:40 AM
RE: Why do we need Functions? - by PhilOfPerth - 08-19-2022, 05:40 AM
RE: Why do we need Functions? - by OldMoses - 08-19-2022, 12:58 PM
RE: Why do we need Functions? - by SMcNeill - 08-19-2022, 06:28 AM
RE: Why do we need Functions? - by SMcNeill - 08-19-2022, 06:42 AM
RE: Why do we need Functions? - by PhilOfPerth - 08-19-2022, 08:15 AM
RE: Why do we need Functions? - by bartok - 08-19-2022, 01:36 PM
RE: Why do we need Functions? - by mnrvovrfc - 08-20-2022, 11:59 AM
RE: Why do we need Functions? - by bartok - 08-20-2022, 01:54 PM
RE: Why do we need Functions? - by mnrvovrfc - 08-20-2022, 11:56 AM
RE: Why do we need Functions? - by TempodiBasic - 08-20-2022, 04:52 PM
RE: Why do we need Functions? - by bplus - 08-20-2022, 04:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help: Issues with the _PRINTSTRING and _INPUTBOX$ functions in QB64. qbfans 2 376 02-13-2026, 06:07 AM
Last Post: qbfans
  Use of Functions PhilOfPerth 53 7,305 05-30-2025, 01:49 PM
Last Post: TempodiBasic
  C file functions in QB64 ??? Jack 11 2,259 04-29-2023, 06:58 PM
Last Post: Kernelpanic

Forum Jump:


Users browsing this thread: 1 Guest(s)