• Print

Author Topic: 'Local' Functions and Subs within Functions and Subs  (Read 440 times)

DeeBee

  • Sr. Member
  • ****
  • Posts: 491
    • Donnelly-House
    • Email
'Local' Functions and Subs within Functions and Subs
« on: October 01, 2011, 05:56:39 AM »
Can you have 'Local' ('private') Functions and Subs declared within Functions and Subs?

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #1 on: October 01, 2011, 08:13:07 AM »
GOSUBs are OK.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

DSMan195276

  • Hero Member
  • *****
  • Posts: 1978
  • Yes
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #2 on: October 01, 2011, 08:46:49 AM »
To expand on what Clippy said, here's an example:

Code: [Select]

foo 'Call sub foo

GOSUB bar 'Fails, because you can only access the GOSUB bar inside of the SUB foo

END

SUB foo ()
  GOSUB bar 'calls the GOSUB bar inside of foo
 
EXIT SUB 'Exit the SUB foo before the bar GOSUB starts to run

bar: 'GOSUB bar
'Do suff
RETURN 'Exit the GOSUB
END SUB

I hope that helps,

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #3 on: October 01, 2011, 08:55:17 AM »
You must use EXIT SUB to leave the SUB or you will get a "Return without GOSUB" error.

If you want to run something before exit you can use GOTO and a line label to go to the end of the SUB instead of a GOSUB which requires a RETURN.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

DeeBee

  • Sr. Member
  • ****
  • Posts: 491
    • Donnelly-House
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #4 on: October 01, 2011, 10:39:52 AM »
Yeah. I want to do this:

Code: [Select]
SUB primary

   FUNCTION secondary

   END FUNCTION ' secondary

   a = secondary()

END SUB ' primary

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #5 on: October 01, 2011, 12:32:21 PM »
You don't need to put a FUNCTION inside of a SUB. Just make them separately and refer to the function inside of the SUB. If it is only used once, then build it in the SUB code.

SUB procedures are normally used to do repetitive tasks. Besides, you can't do that.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

kidfrommars

  • Full Member
  • ***
  • Posts: 215
    • C & Fortran Polyglot
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #6 on: October 01, 2011, 06:07:33 PM »
That's something only Pascal does. It is pretty good for modularity.

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #7 on: October 01, 2011, 06:56:00 PM »
That doesn't sound too good to me! Why on earth would you want to do that when you can use them both separately? If you put the function inside of the SUB it is only used there! GOOFY!
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

mcalkins

  • Hero Member
  • *****
  • Posts: 1269
    • qbasicmichael.com
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #8 on: October 01, 2011, 07:23:39 PM »
http://en.wikipedia.org/wiki/Nested_function

I'm not keen on the concept, but I was never a Pascal programmer...

I think C++ allows the use of private functions within classes, but that's not the same as nesting functions.

This is where using multiple modules would be practical, for isolating variables and functions. In multimodular Assembly programs, only labels declared "global" are exported.

Since QB64 doesn't seem to allow for multimodular programming (aside from library usage), you can use either of Clippy's suggestions, either GOSUB within a SUB/FUNCTION procedure, or additional SUB/FUNCTION procedures. In the case of the latter, I would add that you can name the secondary function in such a way as to make it obvious that it is only called from the primary function. For example, if you have a function, abc, that is the only thing that calls function xyz, then xyz could be named abc_xyz. Even if the function isn't actually private, you don't need the compiler to enforce what you already know...

Regards,
Michael
The QBASIC Forum Community: http://www.network54.com/index/10167 Includes off-topic subforums.
QB64 Off-topic subforum: http://qb64offtopic.freeforums.org/

codeguy

  • Hero Member
  • *****
  • Posts: 3552
  • what the h3ll did i name that code?
    • stuff at dkm
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #9 on: October 06, 2011, 09:47:23 AM »
umm, allowing nested functions/subs could lead to some TERRIBLY sloppy code. my suggestion is to do as others have said and make them into discrete subs and call them from within functions or subs. just sayin' :P
« Last Edit: October 06, 2011, 09:57:26 AM by codeguy »
http://denteddisk.forums-free.com/make-an-appointment-with-the-resident-code-guru-f34.html

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #10 on: October 06, 2011, 09:53:07 AM »
Nesting a SUB or FUNCTION makes it USELESS to other procedures so why even think about it?
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

DeeBee

  • Sr. Member
  • ****
  • Posts: 491
    • Donnelly-House
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #11 on: October 06, 2011, 10:13:40 AM »
On the contrary, it creates better, tighter code, rather than "sloppy code".
It also allow isolation, privacy, and compartmentalization.

Depending on the language used, the parent's local variables are (often) accessible
to the local/private function/sub as 'global' values. (which is sometimes nice/necessary)
Otherwise, you have no ability to access those values, unless they are passed as
parameters (which I had to do) to the called sub/func, which can sometimes be
unwieldy, and it is also usually not "readily evident" what you are doing.

If you have a func/sub that is ONLY used by a certain func/sub, then it is
"sloppy" to make it "globally usable" by any other func/sub.

If you are using classes and objects and properties and methods (or, performing
OO-like programming using a non-OO language), then private functions and
properties are all-important.

If you have only (for the most part) programmed in (simple) BASIC or QB64,
then I understand why some of these concepts seem foreign. But you will be
better, stronger programmers if you read up on them, and understand them,
even if only a little bit.

Yes, understanding, or using, most of these concepts will make you better QB64 programmers.

You might want to step into the 21st Century.  ;)

http://en.wikipedia.org/wiki/Types_of_programming_paradigms

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #12 on: October 06, 2011, 10:23:52 AM »
WTF is the difference between calculating something inside of a procedure and CALLING IT a function name? You WASTE TWO CODE LINES just naming it and closing it! SILLY!

Besides if it is used more than once in a procedure it can ALREADY be made into a GOSUB! But I'm BETTING that you will call that SLOPPY too! I call it "working with the dick ya got"! TRY making that better!  ;D

I am SURE that Galleon DOES NOT INTEND to CATER to every WHIM of the user either!
« Last Edit: October 06, 2011, 10:38:13 AM by Clippy »
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

DeeBee

  • Sr. Member
  • ****
  • Posts: 491
    • Donnelly-House
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #13 on: October 06, 2011, 10:37:47 AM »
Are you being purposely obtuse?

You make things subroutines so they can be called from multiple locations
to execute the same functionality without copying and pasting the code
throughout your code. Which you would then have to modify in multiple
locations for debugging and enhancement and customization.
(plus it makes your code much less understandable and readable and sloppy)

That's modular programming 101.

"Brute force" programming _IS_ POOR programming. (usually)

And "GOTO" and "GOSUB" are pretty much universally considered archaic
and poor programming practice, and are deprecated in most BASIC languages.
(or are considered deprecated)

They should basically NEVER be used.

Using GOTO and GOSUB _IS_ "sloppy programming." Period.

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: 'Local' Functions and Subs within Functions and Subs
« Reply #14 on: October 06, 2011, 10:48:52 AM »
Quote
"Brute force" programming _IS_ POOR programming. (usually)

If a SUB or FUNCTION is to be used universally then make it into a separate procedure! This ISN'T ROCKET SCIENCE son. This language isn't called BASIC without reason! Adding special stuff for crybabys will just make the process MORE complicated and CHASE new people away like Free Basic did when it decided to become a C gaming language. WE DON'T WANT OR NEED THAT!

QBASIC SURVIVED ON "BRUTE FORCE"! It wouldn't still be around if it hadn't! I'm STILL using it on my XP!

PS: I DEPRECATED your input a LONG TIME AGO, but if you WHINE long enough perhaps Galleon can allow it! He already allows DATA inside of sub-procedures.

PSS: QUIT BOUNCING the topic from one procedure to GLOBAL procedures. You just want to argue!
« Last Edit: October 06, 2011, 11:08:48 AM by Clippy »
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

  • Print