Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of Functions
#11
(05-21-2025, 09:12 AM)bplus Wrote: To illustrate further the difference between a Function and an equivalent Sub:
Code: (Select All)
nNames = 5
Dim names$(1 To nNames)
names$(1) = "PhilOfPerth"
names$(2) = "bplus"
names$(3) = "ahenry3068"
names$(4) = "SMcNeill"
names$(5) = "Who am I?"
For i = 1 To nNames

    ' using a function only takes one line to report results
    Print "Function last letter for "; names$(i); " is "; LastLetter$(names$(i))

    ' using a sub like a Function usually requires a call to it then another line to report results
    lastLetter names$(i), ll$
    Print "Sub last letter for "; names$(i); " is "; ll$
    Print
Next

Function LastLetter$ (mystring$)
    LastLetter$ = Mid$(mystring$, Len(mystring$), 1)
End Function

Sub lastLetter (mystring$, myLastLetter$)
    myLastLetter$ = Mid$(mystring$, Len(mystring$), 1)
End Sub

Phil: "With a Sub I can change several variables at the same time."

If you need variables changed then yeah by all means use a Sub.
Functions do a job of processing some information and returning a single result like a formula Area = Pi*R^2
Function AreaCircle(R)
AreaCircle = _Pi * R^2
End Function

The advantage of Functions is they wont change their arguments unless you tell them to.


Note that this is... wrong?  Misleading at the very least.

Functions behave *exactly* as Subs do with regards to passing values and changing their arguments.  I'm not certain what "unless you tell them to" refers to, but if types match and values change inside the function, they'll change outside it too.

x = 1
PRINT foo (x)
PRINT foo (x)
PRINT x

FUNCTION foo (x)
   foo = 2 * x
   X = x + 1
END FUNCTION

^ Prints 2 (x = 1), then 4 (x = 2 after the first call), then 3 (final value of x)
Reply


Messages In This Thread
Use of Functions - by PhilOfPerth - 05-20-2025, 11:21 PM
RE: Use of Functions - by bplus - 05-20-2025, 11:24 PM
RE: Use of Functions - by ahenry3068 - 05-20-2025, 11:34 PM
RE: Use of Functions - by PhilOfPerth - 05-21-2025, 12:25 AM
RE: Use of Functions - by bplus - 05-21-2025, 12:39 AM
RE: Use of Functions - by PhilOfPerth - 05-21-2025, 01:33 AM
RE: Use of Functions - by SMcNeill - 05-21-2025, 02:33 AM
RE: Use of Functions - by PhilOfPerth - 05-21-2025, 04:24 AM
RE: Use of Functions - by ahenry3068 - 05-21-2025, 06:41 AM
RE: Use of Functions - by bplus - 05-21-2025, 09:12 AM
RE: Use of Functions - by SMcNeill - 05-21-2025, 09:45 AM
RE: Use of Functions - by bplus - 05-21-2025, 11:23 AM
RE: Use of Functions - by bplus - 05-21-2025, 11:35 AM
RE: Use of Functions - by Kernelpanic - 05-22-2025, 03:00 PM
RE: Use of Functions - by bplus - 05-21-2025, 11:41 AM
RE: Use of Functions - by SMcNeill - 05-21-2025, 11:45 AM
RE: Use of Functions - by bplus - 05-21-2025, 11:55 AM
RE: Use of Functions - by TempodiBasic - 05-21-2025, 12:06 PM
RE: Use of Functions - by mdijkens - 05-21-2025, 12:11 PM
RE: Use of Functions - by TempodiBasic - 05-21-2025, 08:40 PM
RE: Use of Functions - by Dimster - 05-21-2025, 03:34 PM
RE: Use of Functions - by Kernelpanic - 05-21-2025, 06:01 PM
RE: Use of Functions - by CharlieJV - 05-22-2025, 02:37 AM
RE: Use of Functions - by hsiangch_ong - 05-22-2025, 03:30 PM
RE: Use of Functions - by PhilOfPerth - 05-22-2025, 10:22 PM
RE: Use of Functions - by bplus - 05-22-2025, 11:37 PM
RE: Use of Functions - by madscijr - 05-23-2025, 12:34 AM
RE: Use of Functions - by bplus - 05-23-2025, 10:29 AM
RE: Use of Functions - by bplus - 05-23-2025, 10:49 AM
RE: Use of Functions - by bplus - 05-23-2025, 11:22 AM
RE: Use of Functions - by PhilOfPerth - 05-23-2025, 10:16 PM
RE: Use of Functions - by Pete - 05-23-2025, 10:55 PM
RE: Use of Functions - by madscijr - 05-24-2025, 03:10 AM
RE: Use of Functions - by OldMoses - 05-25-2025, 06:05 PM
RE: Use of Functions - by eoredson - 05-26-2025, 08:59 PM
RE: Use of Functions - by PhilOfPerth - 05-28-2025, 03:32 AM
RE: Use of Functions - by Kernelpanic - 05-26-2025, 11:47 PM
RE: Use of Functions - by Pete - 05-27-2025, 11:25 PM
RE: Use of Functions - by bplus - 05-28-2025, 06:17 AM
RE: Use of Functions - by PhilOfPerth - 05-28-2025, 09:11 AM
RE: Use of Functions - by bplus - 05-28-2025, 09:17 AM
RE: Use of Functions - by PhilOfPerth - 05-28-2025, 09:22 AM
RE: Use of Functions - by SMcNeill - 05-28-2025, 09:53 AM
RE: Use of Functions - by PhilOfPerth - 05-28-2025, 10:59 AM
RE: Use of Functions - by Kernelpanic - 05-28-2025, 05:08 PM
RE: Use of Functions - by TempodiBasic - 05-28-2025, 10:18 PM
RE: Use of Functions - by Pete - 05-29-2025, 12:08 AM
RE: Use of Functions - by Pete - 05-29-2025, 12:22 AM
RE: Use of Functions - by PhilOfPerth - 05-29-2025, 02:02 AM
RE: Use of Functions - by PhilOfPerth - 05-29-2025, 02:42 AM
RE: Use of Functions - by SMcNeill - 05-29-2025, 02:38 AM
RE: Use of Functions - by Pete - 05-29-2025, 06:52 PM
RE: Use of Functions - by Kernelpanic - 05-29-2025, 08:21 PM
RE: Use of Functions - by TempodiBasic - 05-30-2025, 01:49 PM



Users browsing this thread: 2 Guest(s)