• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 09:12
CEST 15:12
KST 22:12
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Code S Season 1 (2026) - RO4 & Finals Preview5[ASL21] Ro4 Preview: On Course12Code S Season 1 - RO8 Preview7[ASL21] Ro8 Preview Pt2: Progenitors8Code S Season 1 - RO12 Group A: Rogue, Percival, Solar, Zoun13
Community News
Weekly Cups (May 11-17): Classic wins double0Code S Season 1 (2026) - RO8 Results2Weekly Cups (May 4-10): Clem, MaxPax, herO win1Maestros of The Game 2 announcement and schedule !16Weekly Cups (April 27-May 4): Clem takes triple0
StarCraft 2
General
Weekly Cups (May 11-17): Classic wins double Code S Season 1 (2026) - RO4 & Finals Preview Team Liquid Map Contest #22 - The Finalists Code S Season 1 (2026) - RO8 Results Code S Season 1 (2026) - RO12 Results
Tourneys
$1,400 SEL Season 3 Ladder Invitational GSL Code S Season 2 (2026) GSL Code S Season 1 (2026) $5,000 WardiTV Spring Championship 2026 Maestros of The Game 2 announcement and schedule !
Strategy
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players
External Content
The PondCast: SC2 News & Results Mutation # 526 Rubber and Glue Mutation # 525 Wheel of Misfortune Mutation # 524 Death and Taxes
Brood War
General
25 Years Since Brood War Patch 1.08 Lights Ro.8 Review (asl s21) BGH Auto Balance -> http://bghmmr.eu/ BW General Discussion vespene.gg — BW replays in browser
Tourneys
[ASL21] Semifinals B [BSL22] RO8 Bracket Stage + Another TieBreaker [ASL21] Ro8 Day 4 Escore Tournament StarCraft Season 2
Strategy
Muta micro map competition Fighting Spirit mining rates [G] Hydra ZvZ: An Introduction Simple Questions, Simple Answers
Other Games
General Games
Stormgate/Frost Giant Megathread Warcraft III: The Frozen Throne ZeroSpace Megathread War of Dots, 2026 minimalst RTS Nintendo Switch Thread
Dota 2
The Story of Wings Gaming
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
US Politics Mega-thread European Politico-economics QA Mega-thread YouTube Thread Russo-Ukrainian War Thread UK Politics Mega-thread
Fan Clubs
The herO Fan Club!
Media & Entertainment
[Manga] One Piece Anime Discussion Thread [Req][Books] Good Fantasy/SciFi books
Sports
2024 - 2026 Football Thread McBoner: A hockey love story Formula 1 Discussion
World Cup 2022
Tech Support
streaming software Strange computer issues (software) [G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
Why RTS gamers make better f…
gosubay
How EEG Data Can Predict Gam…
TrAiDoS
ramps on octagon
StaticNine
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1515 users

Stack vs Recursion

Blogs > EsX_Raptor
Post a Reply
EsX_Raptor
Profile Blog Joined February 2008
United States2802 Posts
April 24 2009 06:15 GMT
#1
Alright, we all know what a recursive function is (C++ example):

[image loading]


But I heard that it is also possible to implement that same thing, but using a stack.

I just want to hear what you guys think would be the way to do it, or how to approach it, 'cause I spent the entire day today trying to figure out how.

*
Divinek
Profile Blog Joined November 2006
Canada4045 Posts
April 24 2009 06:17 GMT
#2
Well if you understand what a stackframe is in java and how recursion works there,it's the same idea
Never attribute to malice that which can be adequately explained by stupidity.
Oh goodness me, FOX tv where do you get your sight? Can't you keep track, the puck is black. That's why the ice is white.
BottleAbuser
Profile Blog Joined December 2007
Korea (South)1888 Posts
Last Edited: 2009-04-24 06:22:52
April 24 2009 06:19 GMT
#3
Recursion is implemented with a stack in most computers, actually.

Here's an explicit stack example (Java noob here, sorry):

int factorial(int n){
Stack s = new Stack();
for(int i=1; i<=n;i++){
s.push(i);
}
int result = 1;
while(!s.isEmpty()){
result*=s.pop();
}
return result;
}
Compilers are like boyfriends, you miss a period and they go crazy on you.
cgrinker
Profile Blog Joined December 2007
United States3824 Posts
April 24 2009 06:20 GMT
#4
Do you have direct control of the stack in C++? We do it when we code in assembly (Jasmin) put the idea of recursion is that when your function calls itself every call is pushed onto the stack so that once you reach your base case you start popping values down the stack.

Is it a trick question? the recursive function is computed on the stack (just like everything else) when its compiled down to x86. Are you sure you aren't supposed to be doing your assingment in 8086 op codes?
EsX_Raptor
Profile Blog Joined February 2008
United States2802 Posts
Last Edited: 2009-04-24 06:33:55
April 24 2009 06:32 GMT
#5
its not an assignment, I just want to know how to program recursive functions using stacks instead.
(edit: for example, what would I do when I need to implement a naturally recursive function in FORTRAN which doesn't allow recursion)

In C++, I actually made the stack myself and I just push, pop and top stuff from it.

I'll do what you said cgrinker, brb.
haduken
Profile Blog Joined April 2003
Australia8267 Posts
April 24 2009 06:39 GMT
#6
??? weird question.

Recursive functions make implied use of the stack by storing auto variables, function heads and other infos.

If you are talking about computing models then stack is recursive by its own defination (I could be wrong here).
Rillanon.au
cgrinker
Profile Blog Joined December 2007
United States3824 Posts
April 24 2009 06:50 GMT
#7
in x86!

;Factorial using recursion
;x is the input

ADD AX, x
ADD BX, x
fact; begin
MUL BX, AX
SUB AX, 1
CMP AX, 1
JNZ fact
NOP
HLT
cgrinker
Profile Blog Joined December 2007
United States3824 Posts
April 24 2009 06:52 GMT
#8
If you can't do function calls then you are going to have to use a pointer function like JMP (jump) or, god forbid some kind of goto thing. Then you need to compare the value to see if you have reached your base case. Its kinda backwards in assembly.
prOxi.swAMi
Profile Blog Joined November 2004
Australia3091 Posts
April 24 2009 06:53 GMT
#9
Recursive functions, a function calling itself, IS using a stack...
Oh no
imDerek
Profile Blog Joined August 2007
United States1944 Posts
April 24 2009 06:57 GMT
#10
all procedure calls are using a stack to keep track of frames
Least favorite progamers: Leta, Zero, Mind, Shine, free, really <-- newly added
evanthebouncy!
Profile Blog Joined June 2006
United States12796 Posts
April 24 2009 07:00 GMT
#11
If you want to do it with stack consider this code in MIPS:
It's the tower of Hannoi:

#$s1 saves n number
#$s2 saves source
#$s3 saves dest
#$s4 saves help
#$ra saves return_address via JAL

.data
n: .word 30
source: .asciiz "A"
destination: .asciiz "B"
helper: .asciiz "C"
newline: .asciiz "\n"

.text
main:
lw $s1, n
la $s2, source
la $s3, destination
la $s4, helper
la $s5, newline
jal loop
j done

loop:
addi $sp, $sp, -20 #pull down stack and save junks
sw $ra, 0($sp) #save return address first
sw $s1, 4($sp) #push n to stack
sw $s2, 8($sp) #push source to stack
sw $s3, 12($sp) #push dest to stack
sw $s4, 16($sp) #push helper stack

jal test #see if we reached 0, if no, continue, if yes, go to end_loop

addi $s1, $s1, -1 #decrease n by 1
move $t0, $s3 #swap dest with help
move $s3, $s4
move $s4, $t0
jal loop #loop again, send it downward to the tree

li $v0, 4 #print stuff
lw $a0, 8($sp)
syscall
lw $a0, 12($sp)
syscall
addi $a0, $s5, 0
syscall

lw $s2, 8($sp) #load up the source/helper, swap them
lw $s3, 12($sp)
lw $s4, 16($sp)
move $t0, $s2
move $s2, $s4
move $s4, $t0
jal loop #going down again.

end_loop: #get the register return address, and return it
lw $ra, 0($sp) #reload register
lw $s1, 4($sp) #reload n to return to upper level
addi $sp, $sp, 20 #pop stacks
jr $ra #return to what called it

test:
beq $s1, $zero, is_zero #if it is 0, go to is_zero option
jr $ra #if it's not 0, simply return to deepen the tree
is_zero:
j end_loop #jump to end of the loop

done:
li $v0, 10
syscall
Life is run, it is dance, it is fast, passionate and BAM!, you dance and sing and booze while you can for now is the time and time is mine. Smile and laugh when still can for now is the time and soon you die!
tec27
Profile Blog Joined June 2004
United States3702 Posts
April 24 2009 07:28 GMT
#12
On April 24 2009 15:20 cgrinker wrote:
Do you have direct control of the stack in C++? We do it when we code in assembly (Jasmin) put the idea of recursion is that when your function calls itself every call is pushed onto the stack so that once you reach your base case you start popping values down the stack.

Is it a trick question? the recursive function is computed on the stack (just like everything else) when its compiled down to x86. Are you sure you aren't supposed to be doing your assingment in 8086 op codes?

You're confusing "the stack" with "a stack". A stack, as I'm sure you probably realize is just a type of data structure with a LIFO interface. "The stack" that you speak of is a specific usage of that data structure for storing info such as function parameters/return addresses. So while when you implement this algorithm using a lower level language like assembly, it may be easier to use "the stack" as the stack in question, higher level languages would typically create a new object of a stack class and use that.
Can you jam with the console cowboys in cyberspace?
Cambium
Profile Blog Joined June 2004
United States16368 Posts
Last Edited: 2009-04-24 07:53:19
April 24 2009 07:52 GMT
#13
int factorial( int n ) {
Stack stach = new Stack();
for( int i = 0; i < n; i ++ )
stach.push( i );

int result = 1;
while( !stach.empty() ){
result * = (int) stach.pop();
}
return result;
}
When you want something, all the universe conspires in helping you to achieve it.
Cambium
Profile Blog Joined June 2004
United States16368 Posts
April 24 2009 07:54 GMT
#14
The stack is completely unnecessary -_-;;
When you want something, all the universe conspires in helping you to achieve it.
EsX_Raptor
Profile Blog Joined February 2008
United States2802 Posts
April 24 2009 14:33 GMT
#15
yes exactly, sorry for the ambiguity, i meant using A stack with a LIFO interface.

trying to control THE stack would be something really complex actually, and im not yet into that level of programming >.<

i'll try to implement a quicksort using a stack.
Please log in or register to reply.
Live Events Refresh
Kung Fu Cup
11:00
#8
IntoTheiNu 728
RotterdaM624
WardiTV589
Rex140
SteadfastSC119
CosmosSc2 39
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Ryung 981
RotterdaM 624
Rex 140
SteadfastSC 119
CosmosSc2 39
StarCraft: Brood War
Bisu 2491
Horang2 1497
Jaedong 1305
actioN 676
Nal_rA 566
BeSt 431
EffOrt 421
ggaemo 393
firebathero 389
Mini 356
[ Show more ]
ZerO 249
Soulkey 228
Light 202
Hyuk 187
ToSsGirL 147
Mong 129
Rush 128
Mind 113
Pusan 65
Hyun 59
Sharp 52
Sea.KH 43
Sexy 29
soO 26
Barracks 25
910 23
JulyZerg 22
sorry 19
GoRush 14
HiyA 12
Movie 12
Terrorterran 8
ajuk12(nOOB) 8
Icarus 7
Rock 5
Dota 2
Gorgc7708
Dendi585
XcaliburYe117
Counter-Strike
olofmeister2544
allub222
markeloff179
edward134
Other Games
singsing2528
B2W.Neo726
hiko554
Happy347
Lowko268
crisheroes245
DeMusliM170
Liquid`VortiX57
QueenE48
Pyrionflax28
Liquid`LucifroN26
Trikslyr18
ZerO(Twitch)13
Organizations
Counter-Strike
PGL763
StarCraft: Brood War
lovetv 20
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 12 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Nemesis5310
Other Games
• Shiphtur35
Upcoming Events
WardiTV Qualifier
48m
GSL
20h 18m
Cure vs sOs
SHIN vs ByuN
Replay Cast
1d 10h
GSL
1d 20h
Classic vs Solar
GuMiho vs Zoun
WardiTV Spring Champion…
1d 21h
Replay Cast
2 days
Sparkling Tuna Cup
2 days
WardiTV Spring Champion…
2 days
Replay Cast
3 days
RSL Revival
3 days
Classic vs SHIN
Rogue vs Bunny
[ Show More ]
BSL
4 days
Replay Cast
4 days
Afreeca Starleague
4 days
Flash vs Soma
RSL Revival
4 days
BSL
5 days
Patches Events
5 days
Universe Titan Cup
5 days
Rogue vs Percival
Wardi Open
5 days
Monday Night Weeklies
6 days
Replay Cast
6 days
The PondCast
6 days
Kung Fu Cup
6 days
Liquipedia Results

Completed

Escore Tournament S2: W7
2026 GSL S1
Nations Cup 2026

Ongoing

BSL Season 22
ASL Season 21
IPSL Spring 2026
KCM Race Survival 2026 Season 2
Acropolis #4
KK 2v2 League Season 1
BSL 22 Non-Korean Championship
YSL S3
SCTL 2026 Spring
RSL Revival: Season 5
Heroes Pulsing #1
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2

Upcoming

Escore Tournament S2: W8
CSCL: Masked Kings S4
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
Maestros of the Game 2
WardiTV Spring 2026
2026 GSL S2
Bounty Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.