gosub return versus procedure endprocedure
gosub return versus procedure endprocedure
What is the difference between gosub -return and procedure -endprocedure ?
Re: gosub return versus procedure endprocedure
Allowed only within main body of code. Not within Procedures.
Better to avoid this approach and stick with Procedure..ProcedureReturn..EndProcedure.
Better to avoid this approach and stick with Procedure..ProcedureReturn..EndProcedure.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: gosub return versus procedure endprocedure
First, you have to know that gosub or procedures are based upon the same thing, where the CPU will change its positionning in memory to read its next instruction, and this is slow, and you have to avoid them if you're looking for fast code.
Gosub/Return are the BASIC version of common CPU instructions Jump/Return. The compilation is somehow just a direct translation and so the resulting code is the fastest.
Programming paradigm have derived from being directly CPU inspired and more abstract, giving a more readable code, and especially less what is called «spaghetti code», where it is difficult to track what the program is doing.
So, procedure/function concept is born, where you can give some argument/parameters and get back a result. Internally, the language will use CPU registers and/or memory with some specific instructions to stack data, because it's easier to push/pull than to allocate/free memory. The procedure/endprocedure functions are masking all this stuff to make the programmer's life easier.
POO is another programming paradigm, where code itself is considered as data and can be exchanged, but it's another story.
So, it's up to you to choose between all of this. The faster is NO jump, then gosub, then procedures. But for more readable code, procedures are better.
Gosub/Return are the BASIC version of common CPU instructions Jump/Return. The compilation is somehow just a direct translation and so the resulting code is the fastest.
Programming paradigm have derived from being directly CPU inspired and more abstract, giving a more readable code, and especially less what is called «spaghetti code», where it is difficult to track what the program is doing.
So, procedure/function concept is born, where you can give some argument/parameters and get back a result. Internally, the language will use CPU registers and/or memory with some specific instructions to stack data, because it's easier to push/pull than to allocate/free memory. The procedure/endprocedure functions are masking all this stuff to make the programmer's life easier.
POO is another programming paradigm, where code itself is considered as data and can be exchanged, but it's another story.
So, it's up to you to choose between all of this. The faster is NO jump, then gosub, then procedures. But for more readable code, procedures are better.
Re: gosub return versus procedure endprocedure
I would argue that the main difference is scope. Procedures create/have their own inner scope, allowing local Variables and thus better encapsulation. I guess gosub subroutines don't have such, but i never used them, so correct me if i'm wrong.
Hand in hand with the scope thing goes parameters and returnvalue as means of well defined input and output of the encapsulated code.
<edit>
Scope is also not just a syntactic/abstract thing, but also a physical one. Think of recursion.
Hand in hand with the scope thing goes parameters and returnvalue as means of well defined input and output of the encapsulated code.
<edit>
Scope is also not just a syntactic/abstract thing, but also a physical one. Think of recursion.
Last edited by #NULL on Sun Feb 25, 2018 10:44 am, edited 1 time in total.
Re: gosub return versus procedure endprocedure
Two minutes under the hood, valuable to any beginner.
CPU is reading program from memory.
It reads one instruction at a time from the address given by its internal register PC (Program Counter).
Then this counter is incremented to read the next instruction.
If this instruction is «jump address», (JMP in assembly code) the CPU changes its PC to the given address.
It's basically what is done with a BASIC «GOTO» instruction.
The CPU is also using a reservated space in memory called «stack» that will be useful for context changes.
Let examine how it works.
One of the most common asm instruction is «JSR address» (Jump to SubRoutine).
When the CPU encounters this, it :
The CPU then loads what is on the stack into the PC, what is leading to going back just after the original JSR instruction.
All of this is translated in the PureBASIC high level language to :
Gosub label
Return
Now, let examine what is done with Procedure/Endprocedure.
The CPU has instructions to save data on the stack, and get them back : PUSH and POP.
High level languages will use these to simulate context changes.
Basically, all current data will be saved on the stack when calling procedure, and restored when endprocedure is reached.
The parameters are also stored on the stack and used in the subroutine, and cleared if needed wheb coming back after RET.
So, the difference between gosub and procedure are all this logic of saving/restoring context/values.
No save with gosub, no real context change, just a jump in another part of the code, and get back.
Hope I have been precise enough...
CPU is reading program from memory.
It reads one instruction at a time from the address given by its internal register PC (Program Counter).
Then this counter is incremented to read the next instruction.
If this instruction is «jump address», (JMP in assembly code) the CPU changes its PC to the given address.
It's basically what is done with a BASIC «GOTO» instruction.
The CPU is also using a reservated space in memory called «stack» that will be useful for context changes.
Let examine how it works.
One of the most common asm instruction is «JSR address» (Jump to SubRoutine).
When the CPU encounters this, it :
- saves the current PC in (on) the stack,
- reads the specified address and
- changes its PC.
The CPU then loads what is on the stack into the PC, what is leading to going back just after the original JSR instruction.
All of this is translated in the PureBASIC high level language to :
Gosub label
Return
Now, let examine what is done with Procedure/Endprocedure.
The CPU has instructions to save data on the stack, and get them back : PUSH and POP.
High level languages will use these to simulate context changes.
Basically, all current data will be saved on the stack when calling procedure, and restored when endprocedure is reached.
The parameters are also stored on the stack and used in the subroutine, and cleared if needed wheb coming back after RET.
So, the difference between gosub and procedure are all this logic of saving/restoring context/values.
No save with gosub, no real context change, just a jump in another part of the code, and get back.
Hope I have been precise enough...
Re: gosub return versus procedure endprocedure
as I understand ,procedure and endprocedure is slower then gosub and return ,with procedure there is a lot of push and pop,with gosub and using global variables you can read them direct from memory ,only need the return adress and the statusregister to push and pop.
But I will use procedure instead of gosub.
But I will use procedure instead of gosub.
Re: gosub return versus procedure endprocedure
Wenn es schnell genug ist, um seinen Zweck zu erfüllen, spielt es keine Rolle, was du tust.
Die eleganteste und einfachste Lösung zählt dann immer.
Gosub is a relic of bygone Basic Times
It is probably only included in PB for the sake of completeness
Die eleganteste und einfachste Lösung zählt dann immer.
Gosub is a relic of bygone Basic Times
It is probably only included in PB for the sake of completeness
Re: gosub return versus procedure endprocedure
Perfectly understood. And...ludoke wrote:as I understand ,procedure and endprocedure is slower then gosub and return ,with procedure there is a lot of push and pop,with gosub and using global variables you can read them direct from memory ,only need the return adress and the statusregister to push and pop.
But I will use procedure instead of gosub.
Walbus> Not only. Gosub and goto are this kind of low level instructions that must exists in the paradigm of a all-purposes languages. For example, tiniest and fastest programs must avoid procedures, and, at the very end, any form of jump.
Of course, procedures are better for a clean and readable code.
Re: gosub return versus procedure endprocedure
This is right djes
However, I had no practical application in which this was relevant, although I always try to implement solutions as fast as possible.
For small routines macros are the first choice
In the case of larger routines, gosub's time saving is not an relevant factor.
However, I had no practical application in which this was relevant, although I always try to implement solutions as fast as possible.
For small routines macros are the first choice
In the case of larger routines, gosub's time saving is not an relevant factor.
Re: gosub return versus procedure endprocedure
Yes, it's pretty rare, and macros are cool. However, it's at the cost of space, as same code is copied in different places.
Some can also see that using a procedure with context saving is not useful, a waste of cpu time, when a jump is sufficient.
And, for a purpose of education and knowledge, I think Gosub/Goto are useful, as more and more programmers are ignorant of the basic principles of CPU architecture, and how to optimize.
Some can also see that using a procedure with context saving is not useful, a waste of cpu time, when a jump is sufficient.
And, for a purpose of education and knowledge, I think Gosub/Goto are useful, as more and more programmers are ignorant of the basic principles of CPU architecture, and how to optimize.
Re: gosub return versus procedure endprocedure
Avoiding any form of jump would be almost impossible.djes wrote:For example, tiniest and fastest programs must avoid procedures, and, at the very end, any form of jump.
Conditions (If) and loops (For, While, Repeat) all use (conditional) jump instructions.
A jump instruction doesn't have to be slow. It depends if it's a short/near or a far jump.
Short/near jumps are pretty fast.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: gosub return versus procedure endprocedure
Could macros not even be slower due to cache misses if instatiated at many locations in the code? Because it puts the same code at different locations, whereas a procedure code has only one location in the program?
Re: gosub return versus procedure endprocedure
I learned programming on very slow machines
There you can see very quickly what is good and what is bad
My codes have always been the fastest
Often many times faster than that of the competitors
The art is the simplification and the avoidance of unnecessary
There you can see very quickly what is good and what is bad
My codes have always been the fastest
Often many times faster than that of the competitors
The art is the simplification and the avoidance of unnecessary
Re: gosub return versus procedure endprocedure
Bad use of macros could lead to large redundancy, and cache misses. With experience, some can learn how to balance between code effectiveness and code readability.
A good balance gives beauty
Anyway, some times, one must prefer speed, or readability. A good language gives you control. You are the programmer, you choose.
A good balance gives beauty
Anyway, some times, one must prefer speed, or readability. A good language gives you control. You are the programmer, you choose.
Re: gosub return versus procedure endprocedure
Macros are nothing more than some additional source code, which is inserted at the corresponding places
A negative effect is therefore unimaginable, I guess.
Whether a code has a hundred lines more or less is similar to whether I have one or two grains of sand in my shoe.
A negative effect is therefore unimaginable, I guess.
Whether a code has a hundred lines more or less is similar to whether I have one or two grains of sand in my shoe.