Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of Functions
#1
I have never created a Function, but always used Sub for repeated operations. I'm trying to find how to create and apply Functions, and what, if any, are their advantages (in my SIMPLE programmes) over Subs. I've created these few lines to experiment, but get no results from the Function.

Code: (Select All)
Common Shared x, x$

x = 5: x$ = "ABC"

Print "Checkfunction returns";
Print CheckFunction$(x, x$)

CheckSub
Print "CheckSub returns"; x, x$

Function CheckFunction$ (x, x$)
    x = x * 3: x$ = x$ + "DEF"
End Function

Sub CheckSub
    x$ = x$ + x$
    x = x + x
End Sub

 What am I doing wrong?  Confused
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#2
You need to set the function name = the result or result$ you want the function to return.

The function can only retrun one value through its name that you set as usually last line of function.
Code: (Select All)
Print sum(1, 1)
Function sum (a, b) ' sum is default single taking default single a and b as arguments.
    sum = a + b
End Function
b = b + ...
Reply
#3
(05-20-2025, 11:21 PM)PhilOfPerth Wrote: I have never created a Function, but always used Sub for repeated operations. I'm trying to find how to create and apply Functions, and what, if any, are their advantages (in my SIMPLE programmes) over Subs. I've created these few lines to experiment, but get no results from the Function.

Code: (Select All)
Common Shared x, x$

x = 5: x$ = "ABC"

Print "Checkfunction returns";
Print CheckFunction$(x, x$)

CheckSub
Print "CheckSub returns"; x, x$

Function CheckFunction$ (x, x$)
    x = x * 3: x$ = x$ + "DEF"
End Function

Sub CheckSub
    x$ = x$ + x$
    x = x + x
End Sub

 What am I doing wrong?  Confused
Your using function wrong.  Smile


x = 3
print Cubed(x)
end

function Cubed(X)

Cubed = X * X * X

end function
Reply
#4
(05-20-2025, 11:34 PM)ahenry3068 Wrote:
(05-20-2025, 11:21 PM)PhilOfPerth Wrote: I have never created a Function, but always used Sub for repeated operations. I'm trying to find how to create and apply Functions, and what, if any, are their advantages (in my SIMPLE programmes) over Subs. I've created these few lines to experiment, but get no results from the Function.

Code: (Select All)
Common Shared x, x$

x = 5: x$ = "ABC"

Print "Checkfunction returns";
Print CheckFunction$(x, x$)

CheckSub
Print "CheckSub returns"; x, x$

Function CheckFunction$ (x, x$)
    x = x * 3: x$ = x$ + "DEF"
End Function

Sub CheckSub
    x$ = x$ + x$
    x = x + x
End Sub

 What am I doing wrong?  Confused
Your using function wrong.  Smile


x = 3
print Cubed(x)
end

function Cubed(X)

Cubed = X * X * X

end function

Ah, got it (well, some of it).I'll "fiddle" and see what I can do with multiple arguments Should be able to work this out now. Thanks both.
(Still don't see why I need Function though, I can do this and lots of other things with Sub and it seems simpler to use).
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#5
Functions can be used inside functions so you can do multiple functions in a single line of code.

A sub has to be run and the results run some place else.

Look at all the built in functions we use in the QB64 language, for instance LEN(myString$) or Mid$.

You can do this:

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

Now LastLetter$ might be used in another function or sub or by main code.
b = b + ...
Reply
#6
(05-21-2025, 12:39 AM)bplus Wrote: Functions can be used inside functions so you can do multiple functions in a single line of code.

A sub has to be run and the results run some place else.

Look at all the built in functions we use in the QB64 language, for instance LEN(myString$) or Mid$.

You can do this:

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

Now LastLetter$ might be used in another function or sub or by main code.

I can see we may use several arguments in a Function, but  does a function always only return one value?
With a Sub I can change several variables at the same time.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#7
SUBS *do* something.   FUNCTIONS *return* something.     <--That's the difference between the two.

CLS, PRINT, BEEP, and SLEEP all *do* something for you; they're all SUBS.  CLS will clear the screen.  PRINT will print something to the screen.  BEEP makes your wife go crazy at 2 AM. SLEEP, contrary to the name doesn't help you cure insomnia....

On the other hand, COS, ABS, INT, and RND are all FUNCTIONS, as they return *something* to us.  COS returns the cosine value of the angle we give it.   ABS returns the absolute value of a number.  INT tells you what someone's  IQ is, and RND is good for random pickup lines at a bar..   (Or something like that. Tongue )

To use a function, you need two basic things:

1) To give the function a NAME
2) To assign a value to that function's NAME before exiting the function

FUNCTION foo&
    foo& = 3
END FUNCTION

Function Steve$
  Steve$ = "Steve is Awesome"
END FUNCTION

Notice that the function name is important in marking what TYPE of return value we want.  foo& is a LONG.  Steve$ is a STRING.  It's that simple to make your function.

Parameters are simply passed so that you can do something more useful than what I'm doing above with those pitiful functions.  Big Grin

Function AddString$ (first$, second$)
   AddString$ =  first$ + " " + second$
End Function

And then to make use of it is to either assign it to a return value, or else use it as an operation for a SUB.

MyName$ = AddString$("Steve","McNeill")
PRINT MyName$

Or just: PRINT  AddString$("Steve","McNeill")

The first MyName$ is assigning the return to the variable MyName$.  The second is sending it to a SUB to do something directly.  (Print, in this case.)


That's all there is to it.  Nothing complex or complicated to see here.  Wink
Reply
#8
(05-21-2025, 02:33 AM)SMcNeill Wrote: SUBS *do* something.   FUNCTIONS *return* something.     <--That's the difference between the two.

CLS, PRINT, BEEP, and SLEEP all *do* something for you; they're all SUBS.  CLS will clear the screen.  PRINT will print something to the screen.  BEEP makes your wife go crazy at 2 AM. SLEEP, contrary to the name doesn't help you cure insomnia....

On the other hand, COS, ABS, INT, and RND are all FUNCTIONS, as they return *something* to us.  COS returns the cosine value of the angle we give it.   ABS returns the absolute value of a number.  INT tells you what someone's  IQ is, and RND is good for random pickup lines at a bar..   (Or something like that. Tongue )

To use a function, you need two basic things:

1) To give the function a NAME
2) To assign a value to that function's NAME before exiting the function

FUNCTION foo&
    foo& = 3
END FUNCTION

Function Steve$
  Steve$ = "Steve is Awesome"
END FUNCTION

Notice that the function name is important in marking what TYPE of return value we want.  foo& is a LONG.  Steve$ is a STRING.  It's that simple to make your function.

Parameters are simply passed so that you can do something more useful than what I'm doing above with those pitiful functions.  Big Grin

Function AddString$ (first$, second$)
   AddString$ =  first$ + " " + second$
End Function

And then to make use of it is to either assign it to a return value, or else use it as an operation for a SUB.

MyName$ = AddString$("Steve","McNeill")
PRINT MyName$

Or just: PRINT  AddString$("Steve","McNeill")

The first MyName$ is assigning the return to the variable MyName$.  The second is sending it to a SUB to do something directly.  (Print, in this case.)


That's all there is to it.  Nothing complex or complicated to see here.  Wink

Thanks Steve. That clarified things a lot for me! (That doesn't mean I won't be back for more info though.
I ain't very smart but I can lift heavy fings!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#9
Ah, got it (well, some of it).I'll "fiddle" and see what I can do with multiple arguments Should be able to work this out now. Thanks both.
(Still don't see why I need Function though, I can do this and lots of other things with Sub and it seems simpler to use). 



   I was about to go into a similiar explanation as Mr. McNeil but he said it much better than I would have.

I will add,  you aren't wrong about being able to do it all with SUB's.    I like to say there are always multiple ways to flay a feline.


FUNCTIONS can make your code clearer to read though.   And are the excepted means of returning a value.   If you ever get into
doing any assembly language coding the distinction between a SUB & a FUNCTION actually completely disappears.
Reply
#10
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.
b = b + ...
Reply




Users browsing this thread: 1 Guest(s)