# Invalid return; gosub has not yet been called

**URL:** <https://forum.choiceofgames.com/t/invalid-return-gosub-has-not-yet-been-called/67692>\
**Category:** ChoiceScript Help\
**Created:** [October 24, 2019, 1:27am UTC](https://forum.choiceofgames.com/t/invalid-return-gosub-has-not-yet-been-called/67692 "2019-10-24T01:27:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xephyrx](https://avatars.discourse-cdn.com/v4/letter/x/c5a1d2/32.png) [@xephyrx](https://forum.choiceofgames.com/u/xephyrx)\
**Post date:** [October 24, 2019, 1:27am UTC](https://forum.choiceofgames.com/t/invalid-return-gosub-has-not-yet-been-called/67692/1 "2019-10-24T01:27:45Z")

</div>

I don’t know what this error means. How am I doing gosub wrong? I’m trying to create a combat system, so when gosub is called it calculates randomly the damage that is done.

```
*fake_choice
    *selectable_if (psionics > 0) #Use psionics.  
        *if (psionics >=90) 
            A powerful hit!
            *set atked true
            *gosub atk_enemy_powerful
        *if (psionics >=70) 
            A strong hit!
            *set atked true
            *gosub atk_enemy_strong
        *if (psionics >=50) 
            A decent hit!
            *set atked true
            *gosub atk_enemy_medium
        *if (psionics >=30)
            A weak hit!
            *set atked true
            *gosub atk_enemy_weak
        *if (psionics <30)
            A very weak hit!
            *set atked true
            *gosub atk_enemy_veryweak
            
    *selectable_if (explosives > 0) #Use explosives. 

    #Retreat
        You attempt to run away!

*label atk_enemy_powerful
*if (atked = true)
    *rand hit 30 60
    *set enemy_dam hit 
*return

*label atk_enemy_strong
*if (atked = true)
    *rand hit 20 50
    *set enemy_dam hit
*return

*label atk_enemy_medium
*if (atked = true)
    *rand hit 10 30
    *set enemy_damn hit
*return

*label atk_enemy_weak
*if (atked = true)
    *rand hit 0 20
    *set enemy_dam hit
*return

*label atk_enemy_veryweak
*if (atked = true)
    *rand hit 0 10
    *set enemy_dam hit
*return

```

---

<div class="post-metadata">

**Author:** ![anon8836198](https://avatars.discourse-cdn.com/v4/letter/a/e68b1a/32.png) [@anon8836198](https://forum.choiceofgames.com/u/anon8836198)\
**Post date:** [October 24, 2019, 2:28am UTC](https://forum.choiceofgames.com/t/invalid-return-gosub-has-not-yet-been-called/67692/2 "2019-10-24T02:28:55Z")

</div>

In your example CS will automatically jump into `*label atk_enemy_powerful` after the first return from subroutine and will bug out on the \*return because gosub wasn’t called. Move the subroutine (all atk labels) somewhere out of game scope, e.g. after chapter \*finish or before chapter start.

Having standard \*choices with \*goto’s should also prove useful in this case. Keep in mind that upon \*return game parser will start at the line directly after \*gosub, so having a goto will help avoid bugs similar to yours.

```
*gosub test
*goto there << the line where game resumes after *return

```

The above \*goto makes sure CS heads in the right direction instead of wandering off.

---

<div class="post-metadata">

**Author:** ![xephyrx](https://avatars.discourse-cdn.com/v4/letter/x/c5a1d2/32.png) [@xephyrx](https://forum.choiceofgames.com/u/xephyrx)\
**Post date:** [October 24, 2019, 2:36am UTC](https://forum.choiceofgames.com/t/invalid-return-gosub-has-not-yet-been-called/67692/3 "2019-10-24T02:36:00Z")

</div>

I’m not sure I understand what you mean. What do you mean move the subroutine? When I move it to the top of the page it says gosub has not yet been called.

---

<div class="post-metadata">

**Author:** ![xephyrx](https://avatars.discourse-cdn.com/v4/letter/x/c5a1d2/32.png) [@xephyrx](https://forum.choiceofgames.com/u/xephyrx)\
**Post date:** [October 24, 2019, 2:44am UTC](https://forum.choiceofgames.com/t/invalid-return-gosub-has-not-yet-been-called/67692/4 "2019-10-24T02:44:29Z")

</div>

I think I fixed it. I added a finish to one of the lines in the code and it stopped giving me the error.

---

<div class="post-metadata">

**Author:** ![anon8836198](https://avatars.discourse-cdn.com/v4/letter/a/e68b1a/32.png) [@anon8836198](https://forum.choiceofgames.com/u/anon8836198)\
**Post date:** [October 24, 2019, 2:59am UTC](https://forum.choiceofgames.com/t/invalid-return-gosub-has-not-yet-been-called/67692/5 "2019-10-24T02:59:14Z")

</div>

I would still advise to simplify and bring more structure to your code in order to avoid future headaches, something like:

```
*label battle
*if enemy_dam > 100
	*goto victory

*choice
	*selectable_if (psionics > 0) #Use psionics
		*gosub psi_calc
		*goto battle
	*selectable_if (explosives > 0) #Use explosives
		*gosub exp_calc
		*goto battle
	#Flee
		*goto flee

*label psi_calc
*if (psionics >=90)
	*rand hit 30 60
	*set enemy_dam + hit
	A powerful hit!
	*return
*elseif (psionics >=70)
	*rand hit 20 50
	*set enemy_dam + hit
	A not-so-powerful hit!
	*return
*elseif (psionics >=50)
	*rand hit 10 40
	*set enemy_dam + hit
	A not-so-powerful hit!
	*return
*else
	*rand hit 10 20
	*set enemy_dam + hit
	A not-so-powerful hit!
	*return

```

Also, \*returns should be indented on the same level as the rest of the currently executable code.

---

<div class="post-metadata">

**Author:** ![Szaal](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/szaal/32/112202_2.png) [@Szaal](https://forum.choiceofgames.com/u/Szaal)\
**Post date:** [October 24, 2019, 5:13am UTC](https://forum.choiceofgames.com/t/invalid-return-gosub-has-not-yet-been-called/67692/6 "2019-10-24T05:13:04Z")

</div>

It is the case where you can reach the subroutine’s `*label` without actually coming from `*gosub`. As a result, the code would eventually meet `*return` without knowing which `*gosub` it is anchored with and should return to.

What you did by adding `*finish` is sealing-off your subroutines from being reached by default, which is one way to solve this issue.
