recursion and implementation
TRANSCRIPT
-
7/25/2019 Recursion And Implementation
1/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 1
Recursion and Implementation of
Functions
CS-2303
System Programming Concepts
Slides include materials from The C Programming Language, 2ndedition, !y "ernig#an and Ritc#ie and
from C: How to Program, $t#and %t#editions, !y &eitel and &eitel'
-
7/25/2019 Recursion And Implementation
2/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 2
&efinition
( Recursive Function:a function t#at callsitself
( &irectly or indirectly
( )ac# recursi*e call is made +it# a ne+,independent set of arguments
( Pre*ious calls are suspended
( llo+s *ery simple programs for *erycomple pro!lems
-
7/25/2019 Recursion And Implementation
3/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 3
Simplest )ample
int factorial(int x) {
if (x
-
7/25/2019 Recursion And Implementation
4/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 .
/ore Interesting )ample
To+ers of anoi
( /o*e stac of diss from one peg to anot#er
( /o*e one dis at a time
( arger dis may ne*er !e on top of smaller dis
-
7/25/2019 Recursion And Implementation
5/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 $
To+er of anoi Program
#include
void ove (int n! int a! intc! int ");
int ain() {
int diss; $rintf (%&o' an diss%);
scanf (%d%! +diss);
ove (diss! 1! ,! );
return ;
} // ain
/* 02 n >= . 3iss are arran4edsall to lar4e on the $e4s a! "!and c. 5t least n diss on $e4a. 6o dis on " or c is sallerthan the to$ n diss of a.
7892 9he n diss have "een ovedfro a to c. 8all to lar4eorder is $reserved. 7ther disson a! "! c are undistur"ed. */
void ove (int n! int a! int c! int") {
if (n > )
{
ove (n-1! a! "! c);
$rintf (%:ove one dis
fro d to dn%! a! c); ove (n-1! "! c! a);
} // if (n > )
return;
} // ove
( Ispre-conditionsatisfied !efore
t#is call toove
-
7/25/2019 Recursion And Implementation
6/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 %
To+er of anoi Program
#include
void ove (int n! int a! intc! int ");
int ain() {
int diss; $rintf (%&o' an diss%);
scanf (%d%! +diss);
ove (diss! 1! ,! );
return ;
} // ain
/* 02 n >= . 3iss are arran4edsall to lar4e on the $e4s a! "!and c. 5t least n diss on $e4a. 6o dis on " or c is sallerthan the to$ n diss of a.
7892 9he n diss have "een ovedfro a to c. 8all to lar4eorder is $reserved. 7ther disson a! "! c are undistur"ed. */
void ove (int n! int a! int c! int") {
if (n > )
{
ove (n-1! a! "! c);
$rintf (%:ove one dis
fro d to dn%! a! c); ove (n-1! "! c! a);
} // if (n > )
return;
} // ove
Ifpre-condition
is satisfied here, is itstill satisfied here?
And here?
-
7/25/2019 Recursion And Implementation
7/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 4
To+er of anoi Program
#include
void ove (int n! int a! intc! int ");
int ain() {
int diss; $rintf (%&o' an diss%);
scanf (%d%! +diss);
ove (diss! 1! ,! );
return ;
} // ain
/* 02 n >= . 3iss are arran4edsall to lar4e on the $e4s a! "!and c. 5t least n diss on $e4a. 6o dis on " or c is sallerthan the to$ n diss of a.
7892 9he n diss have "een ovedfro a to c. 8all to lar4eorder is $reserved. 7ther disson a! "! c are undistur"ed. */
void ove (int n! int a! int c! int") {
if (n > )
{
ove (n-1! a! "! c);
$rintf (%:ove one dis
fro d to dn%! a! c); ove (n-1! "! c! a);
} // if (n > )
return;
} // ove
Ifpre-conditionis true and
if n = 1, doesovesatisfy
thepost-condition?
Can we reason that this
program correctly plays
Tower of Hanoi?
-
7/25/2019 Recursion And Implementation
8/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 5
6#y Recursion
( re article of fait# among CS students and faculty
!ut
( a surprise to non-CS students7
( Some pro!lems are too hardto sol*e +it#out
recursion
8 /ost nota!ly, t#e compiler9
8 To+er of anoi pro!lem
8 /ost pro!lems in*ol*ing lined lists and trees
( ater in t#e course'
-
7/25/2019 Recursion And Implementation
9/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 :
Recursion vs. Iteration
( Some simple recursi*e pro!lems can !e
;un+ound< into loops( =ut code !ecomes less compact, #arder to follo+9
( ard pro!lems cannot easily !e epressed
in non-recursi*e code
( To+er of anoi( Ro!ots or a*atars t#at ;learn!ser*ation
( From my o+n eperience, programming
languages, en*ironments, and computer
arc#itectures t#at do not support recursion
( are usually not ric# enoug# to support a
di*erse portfolio of applications
( I7e7, a +ide *ariety of pro!lems in many differentdisciplines
-
7/25/2019 Recursion And Implementation
11/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 11
?uestions
-
7/25/2019 Recursion And Implementation
12/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 12
Implementing Recursion The Stac
( &efinition 8 The Stac
8 !ast-in" #irst-out data structure pro*ided !y
t#e operating system for eachrunning program
8For temporar$storage of automatic *aria!les,arguments, function results, and ot#er stuff
( I7e7, t#e +oring storage for each" separate#unction ca!!7
-
7/25/2019 Recursion And Implementation
13/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 13
The Staccontinued'
( %ver$ sing!e timea function is called, an
area of t#e stac is reser*ed#or that
particu!ar ca!!.
( "no+n as its activation recordin compiler
circles7
-
7/25/2019 Recursion And Implementation
14/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 1.
Recursion isso important
( t#at all modern computer arc#itectures
specifically support it( Stac register
( Instructions for manipulating The Stac
( most modern programming languages
allo+ it
( =ut not Fortran and not Co!ol
-
7/25/2019 Recursion And Implementation
15/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 1$
Recursion in C
( Parameters, results, and automatic *aria!lesallocated on the stac7
( llocated +#en function or compound statementis entered
( Released +#en function or compound statement iseited
( @alues are not retainedfrom one call to net oramong recursions'
-
7/25/2019 Recursion And Implementation
16/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 1%
rguments and Results
17 Space forstoring resu!tis allocated !y caller( >n The Stac
( ssigned !y returnstatement of function
( For use !y caller
&. 'rgumentsare va!uescalculated !y caller offunction
( Placed on The Stac!y caller in locations set aside for t#ecorresponding parameters
( Function may assign ne+ *alue to parameter, !ut A
( Acaller neverloos at parameterBargument *alues again9
37 rguments are remo*ed +#en callee returns( ea*ing only t#e resu!t*alue for t#e caller
-
7/25/2019 Recursion And Implementation
17/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 14
Typical Implementation of The Stac
( inear region of memory
( Stac Pointer;gro+ing< do+n+ard
( )ac# time some information ispushedonto
T#e Stac, pointer mo*es do+n+ard
( )ac# time info ispoppedoff of T#e Stac,
pointer mo*es !ac up+ard
-
7/25/2019 Recursion And Implementation
18/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 15
Typical /emory for Running Program6indo+s inu'
0x00000000
0xFFFFFFFF
address space
program code(text)
static data
heap(dynamically allocated)
stac
(dynamically allocated)
!C
"!
-
7/25/2019 Recursion And Implementation
19/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 1:
Typical /emory for Running Program6indo+s inu'
0x00000000
0xFFFFFFFF
address space
program code(text)
static data
heap(dynamically allocated)
stac
(dynamically allocated)
!C
"!
Heapto!ediscussedlater
incourse
-
7/25/2019 Recursion And Implementation
20/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 20
o+ it +ors
( Imagine t#e follo+ing programD8int factorial(int n){
/* "od of factorial function */
} // factorial
( Imagine also t#e callerD8int x = factorial(1);
( 6#at does compiled code loo lie
-
7/25/2019 Recursion And Implementation
21/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 21
Compiled codeD t#e ca!!er
int x = factorial(1);
( Pro*ide integer-siEed space on stac for result, so
t#at ca!!ing #unctioncan find it
( )*aluate t#e epression ;100< and lea*e it on t#e
stac after result'
( Put t#e current program counter some+#ere so
t#at#actoria!function can return to t#e rig#t placein ca!!ingfunction
( Transfer control to t#e called function
-
7/25/2019 Recursion And Implementation
22/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 22
Compiled codeD#actoria!function
( Sa*e t#e ca!!ers registers in a dedicated space in t#eacti*ation record
( Get t#e parameter nfrom t#e stac
( Set aside some memory for local *aria!les and
intermediate results on t#e stac
( &o +#ate*er#actoria!+as programmed to do
( Put t#e result in t#e space allocated !y t#e ca!!er( Restore t#e ca!!ers registers
( Transfer !ac to t#e program counter sa*ed !y t#eca!!er
-
7/25/2019 Recursion And Implementation
23/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 23
Typical ddress Space 6indo+s inu'
0x00000000
0xFFFFFFFF
#emory
address space
program code(text)
static data
heap(dynamically allocated)
stac
(dynamically allocated)
!C
"!
-
7/25/2019 Recursion And Implementation
24/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 2.
Hote
( T#roug# t#e magic of operating systems,
eac# running program #as its ownmemory
8Complete +it# stac e*eryt#ing else
( Called aprocess
(indows" Linu)" *ni)" etc.
-
7/25/2019 Recursion And Implementation
25/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 2$
Hote continued'
( Hot necessarily true in small, em!edded
systems( Real-time control systems
( /o!ile p#one P&( Remote sensors, instrumentation, etc7
( /ultiple running programssharea memory( )ac# in o+n partition +it# o+n stac( =arriers to pre*ent eac# from corrupting ot#ers
-
7/25/2019 Recursion And Implementation
26/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 2%
S#ared P#ysical /emory
$" %ernel
stac
!rocess &
stac
!rocess '
0x00000000
0x0000FFFF
!hysical
memory
stac
!rocess
-
7/25/2019 Recursion And Implementation
27/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 24
?uestions
-
7/25/2019 Recursion And Implementation
28/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 25
The Stacsummary'
( T#e stac gi*es eac# function ca!!its o+n,
pri*ate place to +or( Separate from all ot#er calls to same function
( Separate from all calls to ot#er functions( Place for automatic *aria!les, parameters, results
-
7/25/2019 Recursion And Implementation
29/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 2:
The Staccontinued'
( >riginally intended to support recursion( /ostly for computer scientists
( Compiler +riters, etc7
( Po+erful ena!ling concept( llo+s function to !e s#ared among multiple
running programs
( S#ared li!raries
( Concurrent acti*ities +it#in a process
-
7/25/2019 Recursion And Implementation
30/30
Recursion and Function
Implementation
CS-2303, C-Term 2010 30
?uestions