GoSub and GoTo question

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
fordxhome
Participant
Posts: 11
Joined: Tue Dec 30, 2003 1:11 pm

GoSub and GoTo question

Post by fordxhome »

Hi,
I'm new to DS Basic. I'm writing a Transform Function. Here's the pseudo code:

* Main
GoSub subA
GoSub subB
GoTo Finish

* Subroutine A
subA:
Data = 0 ;* I intentionally make it fail
If (Data = 0) Then
GoTo Finish
End
Return (Data)

* Subroutine B
subB
Print "I am B"
Data = 3
Return (Data)

* Subroutine Finish
Finish:

The question is that when I tested it, I expected no output because the program control came out of subA and should go to Finish which is the end of the function. But I saw the output was "I am B", why did it go to subB. I really want the function to terminate if subA fails. Your advice is appreciated.
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

Gosub is used to branch to a section of code and return at the point of branching. Goto is an unconditional branch without return. In your example, the logic Gosub subA branched to label subA, then the Return statement send the pointer back to the the Gosub SubA statement. It then did a branch at Gosub subB label, and then returned back to the Gosub subB statement. It then did an unconditional branch to Finish.

Because you put a GoTo Finish statement in your subA statement, it branched unconditionally to the Finish label, where it hit the return, which sent the stack pointer back to the Gosub subA statement, where it then ...

Your logic is terrible. You should not program as you did. You are doing internal subroutine calls and not returning to the branch. Your forcible attempt to eject from the logic wrecks the stack pointer. Now of course someone here is bound to tell you the BASIC statement that unloads the stack and returns, but you would not learn to write properly formatted code.

You should consider a TOP-DOWN coding structure, and skip the Gosubs. If you need to conditionalize your gosubs, then set the proper variables and check them on return. You also do not return data in internal subroutines, so a simple RETURN statement is sufficient.
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

I also will add that if you post a functional requirement, there's a lot of folks here who will show you how to write it properly.
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
fordxhome
Participant
Posts: 11
Joined: Tue Dec 30, 2003 1:11 pm

Post by fordxhome »

kcbland wrote:Gosub is used to branch to a section of code and return at the point of branching. Goto is an unconditional branch without return. In your example, the logic Gosub subA branched to label subA, then the Return statement send the pointer back to the the Gosub SubA statement. It then did a branch at Gosub subB label, and then returned back to the Gosub subB statement. It then did an unconditional branch to Finish.

Because you put a GoTo Finish statement in your subA statement, it branched unconditionally to the Finish label, where it hit the return, which sent the stack pointer back to the Gosub subA statement, where it then ...

Your logic is terrible. You should not program as you did. You are doing internal subroutine calls and not returning to the branch. Your forcible attempt to eject from the logic wrecks the stack pointer. Now of course someone here is bound to tell you the BASIC statement that unloads the stack and returns, but you would not learn to write properly formatted code.

You should consider a TOP-DOWN coding structure, and skip the Gosubs. If you need to conditionalize your gosubs, then set the proper variables and check them on return. You also do not return data in internal subroutines, so a simple RETURN statement is sufficient.
Thanks for your reply. I apologize if my example was not appropriate. Coming from an OO programming background, I'm not so used to global variables. Anyway, what I was trying to ask was how you exit a function if you know some data fields are wrong and you don't want to continue examining the rest of them. In your reply, you said "Because you put a GoTo Finish statement in your subA statement, it branched unconditionally to the Finish label, where it hit the return," I thought when it branched to Finish label, it ends the function, is it not? I don't have a return in Finish.
Confused, please explain if you don't mind.
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

If you look at your DS Manager function editor, there is a RETURN (Ans) statement at the bottom of the frame. This means that a DS function will always return the value of what's in the variable Ans. So, if the logic in your user developed function is only:

Code: Select all

Ans = "TEST"
Then DataStage actually writes a function with the logic:

Code: Select all

FUNCTION YourFunction(Arg1)
Ans = "TEST"
RETURN(Ans)
You can see this if you lean back 4" more from the screen, you will see the FUNCTION and RETURN statements.

Your problem is occurring because you didn't understand what a GOSUB and RETURN statement does. A GOSUB statement says: "Stop HERE. Branch to the label and execute all lines until you see a RETURN statement, then come back HERE. Continue to the next line.


Your logic did this:

Code: Select all

* Main 
GoSub subA 
<Stop HERE>  Branch to subA
    subA: 
    Data = 0 ;* I intentionally make it fail 
    If (Data = 0) Then 
       GoTo Finish 
       * Subroutine Finish 
       Finish: 
       RETURN(Ans)  <---------This sends you back to HERE
HERE:
GoSub subB
<Stop THERE>  Branch to subB
   Print "I am B" 
   Data = 3 
   Return (Data)  <---------This sends you back to THERE
THERE:
GoTo Finish 
Finish: 
RETURN(Ans)  <---------This exits the function
Since these are internal subroutines within your function, all variables are GLOBAL. Your Return (Data) statements do nothing. A RETURN always sends the logic back to the last stack push. To be technical, the stack pointer adds the address of the branch to label to the stack. Program flow shifts to that line of code. As you get 3, 4, 5 levels pushed onto the stack, RETURN statements pop off the current pointer, branching logic back to the address of the statement that pushed the label onto the stack.
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
fordxhome
Participant
Posts: 11
Joined: Tue Dec 30, 2003 1:11 pm

Post by fordxhome »

Thank you Ken for your patience.
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

You caught me on a good day. :wink:

Actually, Ray, Lance, and myself are the "resident" answer guys. If you hop over to www.datastagexchange.com, you'll see our smiling faces there. We're the guys who chime in when sufficient help isn't provided, or response time is slow, or maybe we're not liking the contribution to a question. The Inner Circle is the select group who are like the panel, but aren't actually obligated to be around most of the time.

Sometimes we'll just respond search the forum, :twisted: , if we know the material is covered and the poster didn't exercise the search features first.
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Ken, while being very good at explaining things in detail, has seemed a little... well, "cranky"... lately for some reason. :wink: Terrible Twos come early, Ken? :lol:
-craig

"You can never have too many knives" -- Logan Nine Fingers
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

It's called a 3 1/2 year old boy, a 1 1/2 year old boy, a 2 month old boy, self-employed, working 50 hours/week on a customer site, and time on the forum. :cry:

Questions for which the poster could have searched and found the answer can be annoying. Questions that clearly indicate the poster has not even bothered experimenting with the tool really turn my crank some days. I understand sometimes a customer doesn't provide training for new employees or team members. But, consultants who underbid experienced people for project work and then come here to get solutions deserve the wrath brought down on them.

We're here to provide help, but, sometimes the poster needs to help themselves.

I'm actually getting more sleep now, so the attitude is picking up. The baby is sleeping almost 6 hours at a stretch. Now if we could only get him to do it at night... :lol:
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

kcbland wrote:It's called a 3 1/2 year old boy, a 1 1/2 year old boy, a 2 month old boy, self-employed, working 50 hours/week on a customer site, and time on the forum. :cry:
I know, I know... just 'turning your crank' a little. :wink: Besides, I needed to get past that "Posts: 666" milestone. :twisted:

I was wondering how the 'sleep' thing was going, what with the new baby and all. I can sympathize with all of the above... well, except for all them baby boys. My one child is all growed up and out on her own, so my sleep issues are all of my own making - mine and my current customer, anyway.

Stay cool,
-craig

"You can never have too many knives" -- Logan Nine Fingers
Post Reply