gosub return versus procedure endprocedure

Just starting out? Need help? Post your questions and find answers here.
ludoke
Enthusiast
Enthusiast
Posts: 153
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

gosub return versus procedure endprocedure

Post by ludoke »

What is the difference between gosub -return and procedure -endprocedure ?
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: gosub return versus procedure endprocedure

Post by skywalk »

Allowed only within main body of code. Not within Procedures.
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
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: gosub return versus procedure endprocedure

Post by djes »

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.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: gosub return versus procedure endprocedure

Post by #NULL »

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.
Last edited by #NULL on Sun Feb 25, 2018 10:44 am, edited 1 time in total.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: gosub return versus procedure endprocedure

Post by djes »

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 :
  • saves the current PC in (on) the stack,
  • reads the specified address and
  • changes its PC.
Then it continues execution at this address, until it encounters a «RET» (return) instruction.

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...
ludoke
Enthusiast
Enthusiast
Posts: 153
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

Re: gosub return versus procedure endprocedure

Post by ludoke »

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
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: gosub return versus procedure endprocedure

Post by walbus »

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
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: gosub return versus procedure endprocedure

Post by djes »

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.
Perfectly understood. And...

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.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: gosub return versus procedure endprocedure

Post by walbus »

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.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: gosub return versus procedure endprocedure

Post by djes »

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.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: gosub return versus procedure endprocedure

Post by wilbert »

djes wrote:For example, tiniest and fastest programs must avoid procedures, and, at the very end, any form of jump.
Avoiding any form of jump would be almost impossible.
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)
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: gosub return versus procedure endprocedure

Post by #NULL »

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?
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: gosub return versus procedure endprocedure

Post by walbus »

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
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: gosub return versus procedure endprocedure

Post by djes »

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.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: gosub return versus procedure endprocedure

Post by walbus »

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.
Post Reply