|
Post by andy68 on Jul 16, 2022 18:12:09 GMT -5
Hi, my first post here. Do Function definitions have to be placed at the end of a program? I can't really find that in the description or help menu. Thanks. Andy68
|
|
|
Post by Stefan Pendl on Jul 16, 2022 22:43:18 GMT -5
The functions need to be place inside of the code file, where as of in the beginning or the end is a personal preference. Users that have started with C, might position them before the main code. Users that have started with BASIC usually place them after the main code. There are no restrictions about how you structure your code.
|
|
|
Post by Chris Iverson on Jul 18, 2022 16:02:35 GMT -5
It's not required, but an important thing to keep in mind is that once a SUB or FUNCTION statement is found in the code, no other top-level code will run automatically. It's like finding an END statement in the code.
With the following code:
print 1 print add(3, 4) print 2
Function add(a, b) add = a + b End Function
print 3
If you run it, you'll see that that last print statement never runs.
Now, that just means it won't run automatically. That is, it won't automatically skip over the function definition and execute the code below it. You can still run code that's there just fine; you just have to jump to it.
print 1 print add(3, 4) print 2 goto [after]
Function add(a, b) add = a + b End Function
[after] print 3
|
|
|
Post by mknarr on Jul 19, 2022 8:13:44 GMT -5
I've found over the years that when I write code, it's in the general flow of the program. For me that means Subs and Functions are at the end becasue they are often used multiple times in different parts of the program. Also it makes then easy to find. If you put them in the beginning then you have to skip over them to get to the main part of the program. As Chris pointed out in the middle they can cause problems. Not only do I put Subs first then Functions but they are also in alphabetically order for no other reason than to make them easier to find.
|
|
|
Post by Carl Gundel on Jul 19, 2022 8:38:23 GMT -5
I've found over the years that when I write code, it's in the general flow of the program. For me that means Subs and Functions are at the end becasue they are often used multiple times in different parts of the program. Also it makes then easy to find. If you put them in the beginning then you have to skip over them to get to the main part of the program. As Chris pointed out in the middle they can cause problems. Not only do I put Subs first then Functions but they are also in alphabetically order for no other reason than to make them easier to find. I agree with this way of organizing code. The beginning of the program should (usually) do some sort of initialization that doesn't belong in SUBs or FUNCTIONs.
|
|
|
Post by Rod on Jul 19, 2022 9:20:08 GMT -5
Hands up who knows what “jump to” does? Be honest, do you use it regularly or is it news?
|
|
|
Post by Walt Decker on Jul 19, 2022 10:41:13 GMT -5
Hands up who knows what “jump to” does? Be honest, do you use it regularly or is it news? I think that should be "Heads up".
Jump to = (ASM CODE) JMP Target Code Tag(= Label = code address) = disregard everything that follows and execute the code following tag(Label).
Unless using ASM I use GOTO/GOSUB [LABEL] as little as possible (ASM does not have a GOSUB). I find it much more versitile to use functions.
|
|
|
Post by Rod on Jul 19, 2022 12:17:01 GMT -5
No, I mean the icon in the IDE that looks like a sheet of paper with three green lines and an arrow. Who has ever clicked it?
|
|
|
Post by Walt Decker on Jul 19, 2022 13:24:54 GMT -5
Oh, that. I use that all the time for locating functions and labels in the code (I use labels for finding parts of code, not for code branches).
|
|