Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Functions and Gosubs

Author
Message
Bean
20
Years of Service
User Offline
Joined: 23rd Mar 2004
Location: Flower Mound Texas
Posted: 20th Jun 2004 21:30
Being somewhat new to DB I find something that puzzles me nearly everyday. I have a program that was originally written with all Gosubs in the main loop - and the program works just fine . I then changed all the Gosubs to Functions - and the program works fine.I did have to go back and declare all my variables as Global to get the program to work but saw no difference in its execution.
I was wondering if there are any addvantages to using Functions over Gosubs? Is it usually considered "bad form" to use Gosubs?
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 20th Jun 2004 21:39
Gosub is actually a "hold over" from the original BASIC language, in that a program allowed one to define independant operations inside them for better organization. However, the original gosub had a problem in that they were not very descriptive of what the program was doing, and did not return values other than by globals. What was worse, in a gosub you had make sure you didn't use a variable name that had been made before, or the value in the main program would change too!

Functions on the other hand have the advanatage of using local values that do not interfere with the main program's settings, and can return a value without using a global variable. This means they are truly independent, so are generally easier to use and write.

As to which form is "better form" or not that's mostly a matter of your programming style, though functions are usually easier to use. The reason is that local variable thing, in that say, you can use the variable of "X" for any temp value inside any combination of functions, and they won't change their value from operation to operation. If you tried that in a gosub and also used X in the main program, it'd cause you headaches.

S.

Any truly great code should be indisguishable from magic.
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 20th Jun 2004 22:10
the original gosub was something like

gosub 2000

and no it wasn`t descriptive at all, back then you jumped to the line numbers directly, named points had not been invented, functions allow you make a function like the darkbasic functions that are built in, for example you could have a function that adds vat to numbers like this

function vat(number#)
number#=number#*1.175
endfunction number#

then when you want to find the vat on a number just do something like

print "the cost (with vat) is ";vat(99.99)

and the vat function will automaticaly add the vat to the number you gave it, the advantage is that the number# variable is only seen and used by the function, so you can another variable called number# elsewhere and it will not get affected by whatever you are doing inside the function, think of functions as seperate programs that use all their own variables and only pass one number back to the main program, all the other numbers inside a function are local to that function (unless you make em global...eg you want three numbers back from the function), this way you can write routines to do jobs and all you have to do is include them into your code and then call the function with correct values, it doesn`t matter if the function uses a variable name that is also in your main loop, since altering the variable inside the function won`t change the one in the main loop, so you don`t need to keep track of variable names in a large program and keep altering them when they clash.

Mentor.

PC1: P4 hyperthreading 3ghz, 1gig mem, 2x160gig hd`s, Nvidia FX5900 gfx, 6 way surround sound, PC2: AMD 1.2ghz, 512mb ram, FX5200 ultra gfx, stereo 16 bit soundblaster.
Bean
20
Years of Service
User Offline
Joined: 23rd Mar 2004
Location: Flower Mound Texas
Posted: 21st Jun 2004 07:44
Thanks to SandraD and Mentor.
I appreciate responses from programmers that have been "around the block" so to speak.

Login to post a reply

Server time is: 2024-11-26 22:38:44
Your offset time is: 2024-11-26 22:38:44