Conditional callsubs or retsubs - General - Algorand

Conditional callsubs or retsubs

post by pheathwa on Mar 11, 2022

Similar to branching (bnz, bz etc) is there a way to do conditional callsubs (or retsubs) or can you offer a technique to accomplish the same?

e.g.

load 1

pushint 0

">" // quotes just added for formatting

bnz callsub dothis1

// retsub inside dothis1 would return here, and continue below

load 2

pushint 0

">

bnz callsub dothis2

post by Ben on Mar 11, 2022

Hi pheathwa,

You can definitely do conditional callsub/retsub.

Consider the following TEAL


// itoa
itoa_1:
store 0
load 0
int 0
==
bnz itoa_1_l5
load 0
int 10
/
int 0
>
bnz itoa_1_l4
byte ""
itoa_1_l3:
load 0
int 10
%
callsub inttoascii_0
concat
b itoa_1_l6
itoa_1_l4:
load 0
int 10
/
load 0
swap
callsub itoa_1
swap
store 0
b itoa_1_l3
itoa_1_l5:
byte "0"
itoa_1_l6:
retsub

Here there is a label at the end that handles retsub, if at any point you want to return from the subroutine you can branch to itoa_1_l6 and itll return, otherwise the evaluation will continue inline.

The above was generated with PyTeal which I’ve found is a nice way to see how traditional flow paradigms might look in TEAL.

post by pheathwa on Mar 11, 2022

Super helpful - following is my abbreviated working example. Thank you!

callsub handle_sub1

handle_sub1:

load 2

pushint 0

<=

bnz handle_retsub // Early return

// Do other stuff here

retsub // Normal return

handle_retsub:

retsub