![]() |
Use of Functions - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Use of Functions (/showthread.php?tid=3699) |
Use of Functions - PhilOfPerth - 05-20-2025 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$ What am I doing wrong? ![]() RE: Use of Functions - bplus - 05-20-2025 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) RE: Use of Functions - ahenry3068 - 05-20-2025 (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.Your using function wrong. ![]() x = 3 print Cubed(x) end function Cubed(X) Cubed = X * X * X end function RE: Use of Functions - PhilOfPerth - 05-21-2025 (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.Your using function wrong. 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). RE: Use of Functions - bplus - 05-21-2025 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. RE: Use of Functions - PhilOfPerth - 05-21-2025 (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. 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. RE: Use of Functions - SMcNeill - 05-21-2025 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. ![]() 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. ![]() 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. ![]() RE: Use of Functions - PhilOfPerth - 05-21-2025 (05-21-2025, 02:33 AM)SMcNeill Wrote: SUBS *do* something. FUNCTIONS *return* something. <--That's the difference between the two. 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! RE: Use of Functions - ahenry3068 - 05-21-2025 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. RE: Use of Functions - bplus - 05-21-2025 To illustrate further the difference between a Function and an equivalent Sub: Code: (Select All) nNames = 5 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. |