Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 14:14 28 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Micromite syntax question: gosub in sub

     Page 1 of 2    
Author Message
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 03:37pm 21 Feb 2017
Copy link to clipboard 
Print this post

I'm looking to combine two programs, developed separately, of moderate complexity. I wish to call one (which handles a display) from the other as a subroutine.

All of the variables in the subroutine can be defined as LOCAL, with global variables defined in the main routine being modified as needed.

My question is, can I use GOSUBs within the subroutine, with use of the variables defined as LOCAL? Something like this.
[code]
DIM as string A,B,C
main:
' do
gosub subA
display
Print A
' loop
end

subA:
return

SUB DISPLAY()
LOCAL AS INTEGER i,j,k
i = 10
j = 20
gosub iTimesj
EXIT SUB

iTimesj:
k = i * j
A = str$(k)
print i,j,k,A
return

END SUB
[/code]
Ok, I just tried it (MUPv3 board) and " 0 0 00" and "0" were printed, which appears to mean that LOCAL variables within a SUB subroutine are not visible in a GOSUB subroutine defined inside the "SUB ... END SUB".

Is there a way to do what I want to do considering that there may be conflicting variable definitions between the two programs, or must I work through and eliminate the conflicts?

Edited by lizby 2017-02-23
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 04:29pm 21 Feb 2017
Copy link to clipboard 
Print this post

You should not nest subroutines.
Do it like this (not tested!):
[code]
DIM as string A,B,C
main:
'  do
  subA
  display
  Print A
'  loop
  end

SUB subA()
END SUB

SUB DISPLAY()
  LOCAL AS INTEGER i,j,k
  i = 10
  j = 20
  iTimesj i,j,k
END SUB

SUB iTimesj(i,j,k):
  k = i * j
  A = str$(k)
  print i,j,k,A
END SUB
[/code]

Also try to be consistent by using defined subroutines only and also names of variables using a consistent style of upper/lower case.
It will then be more readable. (Not such a point in a quick short sample but in bigger programs it becomes very helpful)



Microblocks. Build with logic.
 
VK2MCT
Senior Member

Joined: 30/03/2012
Location: Australia
Posts: 120
Posted: 09:11pm 21 Feb 2017
Copy link to clipboard 
Print this post

This 'works'
John VK2MCT

option EXPLICIT
DIM as string A,B,C
main:
' do
gosub subA
display
Print A
' loop
end

subA:
end sub

SUB DISPLAY()
LOCAL AS INTEGER i,j,k
i = 10
j = 20
iTimesj(i,j,k)
END SUB

SUB iTimesj(i,j,k)
k = i * j
A = str$(k)
print i;j;k;A
END SUBEdited by VK2MCT 2017-02-23
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9063
Posted: 01:15am 22 Feb 2017
Copy link to clipboard 
Print this post

I don't know if there is any TECHNICAL reason not to nest gosubs inside subs, but I think it is generally frowned upon and probably not considered to be good practise to do so.

Having said that, if it works for you.......

I tend to agree with MikroBlocks on this one.
Smoke makes things work. When the smoke gets out, it stops!
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 04:03am 22 Feb 2017
Copy link to clipboard 
Print this post

Lance,

The answer to your explicit question "can I use GOSUBs within the subroutine, with use of the variables defined as LOCAL" is that you can. The global DIM as string A,B,C command will enable C$ to pass to the iTimesj subroutine where it is finally defined and printed.

However, you have nested the definition of the subroutine defined with the older structure [iTimesj: -- return] within the superior subroutine defined with the newer structure [SUB DISPLAY() -- EXIT SUB -- END SUB]. This could potentially confuse either you or the interpreter. You should move the definition of [iTimes: -- return] after the [END SUB 'DISPLAY()] command.

It is, of course, confusing, mostly to someone trying to understand what you have written, to use both the older [GOSUB label -- label: -- return] structure and the newer [label -- SUB label -- END SUB 'label] structure in the same program.

Paul in NYEdited by Paul_L 2017-02-23
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 04:18am 22 Feb 2017
Copy link to clipboard 
Print this post

Lance,

Is there any reason that iTimesj: has to be defined within the DISPLAY() routine?

It would be much nicer like this:

DIM as string A,B,C
main:
' do
subA
display
Print A
' loop
END

SUB subA
' something here eventully
END SUB 'subA

SUB DISPLAY()
LOCAL AS INTEGER i,j
i = 10
j = 20
iTimesj
END SUB 'DISPLAY()

SUB iTimesj
LOCAL AS INTEGER k
k = i * j
A = str$(k)
print i,j,k,A
END SUB 'iTimesj

The original EXIT SUB command only prevented the program flow from falling into the iTimesj subroutine a second time. It is neater to end the function instead. Also, the integer k is only used in iTimesk so it should really be LOCAL to that function.

Paul
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9063
Posted: 04:37am 22 Feb 2017
Copy link to clipboard 
Print this post

  Paul_L said  It is, of course, confusing, mostly to someone trying to understand what you have written, to use both the older [GOSUB label -- label: -- return] structure and the newer [label -- SUB label -- END SUB 'label] structure in the same program.


Agree 100%
I used to heavily use GOSUB's, but now prefer SUB/END SUB and it's added advantages.
Smoke makes things work. When the smoke gets out, it stops!
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 06:43am 22 Feb 2017
Copy link to clipboard 
Print this post

Thanks to all. I understand the valid points made by several as regards coding style for future development, but my question was about combining existing programs (not shown) with possible definition conflicts and the scope of LOCAL variables within a SUB subroutine.

I think my example showed what I feared, that LOCAL variables in the SUB subroutine are not visible to GOSUB subroutines even if those subroutines come before the END SUB command.

I had hoped that there might be a way around this, but it looks as if re-coding is the only option.

Paul--the specific reason for having iTimesj within the SUB DISPLAY routine was to see if LOCAL variables in DISPLAY were visible in GOSUB iTimesj--they are not.

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 07:39am 22 Feb 2017
Copy link to clipboard 
Print this post

Just curious, are you familiar with Javascript?

Microblocks. Build with logic.
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 08:56am 22 Feb 2017
Copy link to clipboard 
Print this post

  MicroBlocks said   Just curious, are you familiar with Javascript?

Javascript, C, FORTRAN, COBOL, various assemblers, various Basics, Lua, PHP ....

If anyone wonders, the DISPLAY program I'm integrating is from PICAXE Basic, so only GOSUB, no SUB subroutines. Translation to MMBasic was pretty easy, and I now have it working with the main program, which has a more modern structure with SUB subroutines.

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 11:19am 22 Feb 2017
Copy link to clipboard 
Print this post

Lance,

That's the problem with more modern structures. You have to rip apart the older structures and re-write code that has been working for years.

A few years ago Citibank called me to ask if I knew anyone who could re-work their original COBOL on CICS on VMOS 3.0 mainline banking program which has been running since about 1970. It consisted of about 270,000 lines of code!

I had to tell them that most of the guys I knew who could do it were long since dead or so old and geriatric that they couldn't handle it. It seems that the schools are not teaching something as well organized and procedural as COBOL any more.

OOP seems to be the thing nowadays. I always thought that it was too easy to mistype it as OOPS.

Paul
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 02:50pm 22 Feb 2017
Copy link to clipboard 
Print this post

Gee, Paul, had you known me then you could have offered me--CICS COBOL at the U.S. Senate 1976-79. For a suitably outrageous hourly fee I might have been persuaded to give up retirement for a while.

Lance

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 04:16pm 22 Feb 2017
Copy link to clipboard 
Print this post

Lance,

You're just lucky enough that your employer is still in business to pay your pension! I don't think the U.S. Senate is likely to run out of money anytime soon.

No such luck here. After a third of a century at work I can now fly for free anyplace Pan Am flies. I fooled them though, I saved and invested enough so that I don't need a pension.

I put together the maintenance database at Pan Am using CICS COBOL & DB2 beginning in 1969. It kept track of every piece part used, what component the part was installed in by the serial number of the parent component / aircraft, what aircraft the component was installed in, which technician repaired the piece part / component / aircraft, what the technician did to fix it, where in the world the work was done, how many hours they spent on it, the storage locations of the components / piece parts if they were not installed on an aircraft, and so forth ad infinitum .....

I think it might even have known enough to conclude which technicians' wives had headaches!

It didn't know anything about international weather, fuel loading, cargo loading, passenger loading, flight plans, or flight crew assignments. That was the operations side of the company.

When I wasn't fooling with the computer program I was diagnosing broken aircraft.
They broke em, we fixed em. It was a fun job, especially when we were trying to figure out what happened just before an accident. Sometimes I felt like Sherlock Holmes. It's amazing how many different ways an aircraft can break.

I guess the U.S. Senate would break in real interesting ways too. That must have been fun!

Paul
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 10:26pm 22 Feb 2017
Copy link to clipboard 
Print this post

Off-topic, but . . .

  Paul_L said   It's amazing how many different ways an aircraft can break.


The (UK) press would really go to town if they heard a comment like that from someone 'official!
For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 11:08pm 22 Feb 2017
Copy link to clipboard 
Print this post

  WhiteWizzard said   Off-topic, but . . .

The (UK) press would really go to town if they heard a comment like that from someone 'official!

If you can get a hold of one take a look at the trouble trees in the diagnostic sections of any Boeing aircraft manual. Chapter 23 is communications, chapter 34 is navigation, together they account for about 60% of the manual or about 12,000 pages for the B747.

Paul in NY
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 06:00am 23 Feb 2017
Copy link to clipboard 
Print this post

Paul, I suspect a pension from the Senate would be reliable, but I didn't work there or anywhere else long enough to get one. Leading edge of the "gig" economy--I never worked anywhere longer than 4 years.

As far as a "broken" Congress goes, not so much then as now, though I do remember one late December session when Jesse Helms, trying to move along (i.e., stop impeding) some crucial legislation said, "As much as I enjoy the company of my dear friend, Ted Kennedy, I don't want to spend my Christmas with him". (All speech on the Senate floor was piped into our office.)

Meanwhile, work continues apace on the Explore-64 board--a very nice piece of kit. So one month ago today I received my first micromite. Now I have four of them with two more on order (Explore-28s), and am running a program of 800+ lines as I work through debugging and adding simulation routines. I understand that this is how addiction starts.

Thanks to all for the great micromite ecosystem.

Lance

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 02:09am 24 Feb 2017
Copy link to clipboard 
Print this post

Lance,

Back around 1964 I figured out that I too was a temporary worker. I had worked for a few project teams. When the project was done the boss shook your hand, thanked you for doing a great job, and handed you a pink slip. I felt like a migrant worker. The only permanent jobs were in maintenance organizations. Pan Am had to keep fixing all those aircraft. In the interest of long term stability and the urge to have a normal family life I joined up. It lasted almost a third of a century.

It must have been fun sitting there in the geek quarters listening to the Senate politicians bloviate while you were trying to write some workable code or analyze a bunch of data. I imagine that some of the impersonations produced by your bunch of geeks must have been pretty funny.

The micromite ecosystem is definitely addictive thanks to Geoff's wonderful adaptation of MSBasic. So far I've been doing it all in my head ... just trying to build code that looks like it will work. I'm pretty close to having some complete code which looks good and I'm afraid that I'm going to have to start buying hardware soon. That won't work out too well, I'm afraid that I'll wind up breaking most of the stuff I buy.

PaulEdited by Paul_L 2017-02-25
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1985
Posted: 03:10am 24 Feb 2017
Copy link to clipboard 
Print this post

  Paul_L said  
bloviate


I learned a new word today Edited by CaptainBoing 2017-02-25
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 03:27am 24 Feb 2017
Copy link to clipboard 
Print this post

  CaptainBoing said  
  Paul_L said  
bloviate


I learned a new word today


Ditto . . .

Never heard it before - so will now drop it in the odd sentence here when chatting to people to see their reaction
For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 05:09am 24 Feb 2017
Copy link to clipboard 
Print this post

Paul--coders didn't have to listen to the bloviation. This was after I moved from the Computer Center to the misnamed Democratic Policy Committee, where my work was about a third coding and two-thirds writing/editing (with respect to what was said on the Floor--I not only had to listen to it, I had to go back and read it in the Congressional Record if there was a recorded vote on the issue under discussion, and write an abstract of the "Pros and Cons").


PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
     Page 1 of 2    
Print this page
© JAQ Software 2024