Title: Recursion
1Recursion
A recursive function is a function that
invokes itself Example
1 if n 0 n!
n( n - 1) ... 2 1 if n gt1 Note n! n
(n - 1)!
2Program
include ltstdio.hgt int fact (int) void main (
) int num coutltltendlltlt Please
enter a number" cingtgtnum
Continued ...
3 Program...
if (num lt 0) coutltltendlltlt Error number must
be gt 0" else coutltltendlltltFactorial of
\ ltltnumltlt is ltltfact(num)
Continued ...
4 Program...
int fact (int num) if (num lt 1)
return (1) else return (num fact
(num-1))
5Function Overloading
- Two or more functions are said to be overloaded
when their names are identical, but their
argument lists are different. - Example
- int print (int)
- int print (int, int)
- int print (float)
- int print (int,float)
6- int print (int i)
- if (igt0)
- cout ltlti
- return 1
-
- return 0
7- int print (int i, int j)
- if (iltj)
- cout ltltiltlt ltltj
- return 1
-
- return 0
8- int print (int i, float f)
- if (fgt0.0001 ilt999)
- cout ltltf
- return 1
-
- return 0
9- With the statement
- float a
- int b
- ...
- if (print(b,a)1)
- bdo_somthing(a)
- // the definition of function
- // print(int,float) will be used
10Default Arguments
- Functions can receive default arguments.
- Example
- int print (int0)
- void main ()
- print (19)
- print()
-
- int print (int i)
- cout ltlti
-
11- Only trailing parameters may be defaulted.
- Examples
- void display (int, float, int100) //legal
- void display (int100, float, int) //illegal
12 Variable Scope
The scope of a variable defines
the location within a program where that
variable can be used.
Local Variables vs Global Variables
13 Program
int firstnum / create a global variable
named firstnum / void
valfun (void ) void main ( ) int secnum
/ create a local variable
named secnum / firstnum
10 / store a value into the
global variable / secnum
20 / store a value into the
local variable
/
Continued ...
14 Program...
coutltltendlltltFrom main ( )
firstnum " \ ltlt
firstnum coutltltendlltltFrom main ( ) secnum
" \ ltlt secnum
valfun ( ) // call the function valfun
coutltltFrom main ( ) again \
firstnum "ltltfirstnum coutltltFrom
main ( ) again \
secnum "ltltsecnum
Continued ...
15 Program...
// no values are passed to this function void
valfun (void ) int secnum // create
a second local // variable
named secnum secnum 30 / this only
affects this local
variable's value / coutltltendlltltFrom
valfun ( ) \ firstnum "ltlt
firstnum
Continued ...
16 Program...
coutltltendlltltFrom valfun ( ) \
secnum "ltlt secnum firstnum 40 /
this changes firstnum for both
functions / return
Continued ...
17 Output
From main ( ) firstnum 10 From main ( )
secnum 20 From valfun ( ) firstnum 10 From
valfun ( ) secnum 30 From main ( ) again
firstnum 40 From main ( ) again secnum 20
18 Variable Storage Class or Lifetime of a variable
4 Storage Classes
auto static extern register
19 Local variables storage classes
auto static register
can only take
auto is default ( Variable is alive
while the functions executing )
20 Program
void testauto (void) main ( ) int count
/ create the auto
variable count / for (count 1 count lt 3
count) testauto ( )
Continued ...
21 Program
testauto ( ) int num 0 /
create the auto variable num / /
and initialize to zero /
coutltltendlltltThe value of the automatic \
variable num is ltltnum num
return
22 Output
The value of the automatic variable
num is 0 The value of
the automatic variable
num is 0 The value of the automatic
variable num is
0
23 static storage class
Causes the program to retain the
variable and its latest value.
int num 0 / is runtime initialization
for auto class /
initialization for static class is
done only once.
24 Program
void teststat (void ) void main ( ) int
count / count is a local
auto variable / for (count 1 count lt
3 count) teststat ( )
Continued ...
25 Program
teststat ( ) static int num 0
/ num is a local static variable /
coutltltendlltltThe value of the static \
variable num is"ltlt num) num
return
26 Output
The value of the static variable num is 0
The value of the static variable num is 1 The
value of the static variable num is 2
27 static variables can only be initialized
using a constant or constant variables.
static variables are set to zero when no
initialization is given.
28 Global variable storage classes
lifetime is beyond the calling of any
functions, so no auto or register
declaration.
static extern
They can be
static and extern can only affect the
scope of the variables.
29 A Program May Extend Beyond One File
file1
int price float yield static double
coupon void func1 (void) void func2
(void) void func3 (void) void func4
(void) void main ( ) func1 ( ) func2 (
) func3 ( ) func4 ( )
30 file1 . . .
func1 ( )
31 double interest func2 ( ) func3
( ) func4 ( )
file2
32 Extending the Scope of a Global Variable
int price float yield static double
coupon void func1 (void) void func2
(void) void (func3 (void) void func4
(void) void main ( ) func1 ( ) func2 (
) func3 ( ) func4 ( )
file1
33 file1 ...
extern double interest func1 ( )
34 double interest extern int price func2 ( )
func3 ( ) func4 (
) extern float yield
file2
35Inline Functions
- For functions with less than 3 lines of code,
inline functions may be used - inline function call may not actually result in a
real function call. - The complier duplicate the body of the function
with appropriate substitution at each call to the
function
36- Example
- inline int increment (int x, int val)
- return xval
-
- call
- increment (number,10)
- will be replaced with
- number 10