Title: Variable types
1Recursion
2Functions reminder
return-type name(arg_type1 arg_name1, arg_type2
arg_name2, ) function body return value
- a group of variables and statements that is
assigned a name - a sub-program
- A function can call other functions
3Return Statement - reminder
- Return causes the execution of the function to
terminate and returns a value to the calling
function - The type of the value returned must be the same
as the return-type defined for the function
4Scope of variables - reminder
- A variable declared within a function is
unrelated to variables declared elsewhere - A function cannot access variables that are
declared in other functions
5Func. declaration - reminder
return-type name(arg_type1 arg_name1, arg_type2
arg_name2, )
- We can call a function starting from the point in
the file in which it was declared, and until the
end of the file
6The functions Stack
g() -gth()
f() -gtg()
main -gt f()
7Factorial
- As we saw, n! 123 (n-1)n
- Thus, we can also define factorial the following
way - 0! 1
- n! n(n-1)! for ngt0
n
8A recursive definition
- C functions can also call themselves!
- However, usually not with the same parameters
- Some functions can be defined using smaller
occurrences of themselves. - Every recursive function has a termination
condition. The function stops calling itself
when it is satisfied.
9Example - factorial
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
- int factorial(int n)
-
- int fact 1
- while (n gt 1)
-
- fact n
- n--
-
- return fact
10Example - factorial
factRec(1)
factRec(2) -gt2factRec (1)
factRec(3) -gt3factRec (2)
main -gt factRec (3)
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
11Recursive factorial step by step
FactRec(4)
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
n
4
Returns
12Recursive factorial step by step
FactRec(4)
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
n
4
Returns
4
13Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
14Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
15Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
16Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
17Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
18Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
19Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
20Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
21Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
22Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
23Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
24Recursive factorial step by step
- int factRec(int n)
-
- if (n0 n1)
- return 1
- return nfactRec(n-1)
-
25Example
- int sum_digits(int n)
-
- int sum 0
- while (n gt 0)
-
- sum n10 n n/10
-
- return sum
- Given the following iterative version of
sum-of-digits calculation - Find the recursive definition of this function
26Solution
- int sum_digit_rec(int n)
-
- if (n 0)
- return 0
- return n10 sum_digit_rec(n/10)
27Exercise _at_ home
- Write a program that receives two non-negative
integers and computes their product recursively. - Hint Notice that the product ab is actually
aaa (b times). - How does this help us define the problem
recursively?
28Solution
- int mult_rec(int a,int b)
- if (b1)
- return a
- return amult_rec(a,b-1)
-
- int main()
- int num1,num2
- printf(Please enter two non-negative
integers) - scanf(d d,num1,num2)
- printf(Their product is d,mult_rec(num1,num2)
- return 0
29Exercise _at_home
- int mod(int x,int y)
-
- while (x gt y)
- x x - y
- return x
-
- Given the following iterative version of modulo
calculation - Find the recursive definition of modulo
30Solution
- int mod_rec(int x, int y)
-
- if(xlty)
- return x
- return mod_rec(x-y,y)
31Example recursive strchr
- A recursive implementation of strchr
- Input a string and a character
- Output pointer to the first occurrence of the
character in the string - Solution strchr_rec.c
32strchr_rec.c
- char strchr_rec(char str, char c)
- if (str '\0')
- return NULL
- if (str c)
- return str
- return rec_func(str1, c)
33Exercise _at_ home
- Write a recursive implementation of strcmp
- Input two strings
- Output 0 if both are equal, 1 if not
- Write a program that accepts two strings from the
user and checks whether they are equal
34Solution
35Recursive Max step by step _at_ home
- Write a recursive maximum function
- Input an int array its size
- Output the maximum element
- Go over it _at_ home for better understanding of the
recursive call
36max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
37max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
38max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
39max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
40max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
41max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
42max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
43max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
44max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
45max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
46max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
47max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
48max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
49max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
50max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
51max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
52max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
53max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
54max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
55max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
56max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
57max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
58max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
59max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
60max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
61max_rec step by step
- int rec_max(int arr, int size)
-
- int rest
- if (size 1)
- return arr0
- else
-
- rest rec_max(arr1, size-1)
- if (arr0 gt rest)
- return arr0
- else
- return rest
-
62Some problems are naturally recursive.
- For example, fibonacci series.
- Because the nth term is the sum of the (n-1)th
and (n-2)th terms. - Therefore, the recursive definition is
- fib(1) 0 fib(2) 1. / base /
- fib(n) fib(n-1) fib(n-2).
63Or, in the C form
- int fib_iter(int n)
-
- int prev1 0, prev2 1,
- current 0, count
- for(count1countltncount)
-
- current prev1 prev2
- prev1 prev2
- prev2 current
-
- return prev1
- int fib_rec(int n)
-
- if(n 1)
- return 0
- if(n 2)
- return 1
- return fib_rec(n-1) fib_rec(n-2)
64Recursion and Efficiency
- The recursive form, however elegant, may be much
less efficient - The number of redundant calls grow exponentially!
65Efficient Recursive Fibonacci
- Pass the values that were already calculated to
the recursive function - This technique is called Memoization
66Fibonacci with Memoization
- int main(void)
- int fibSIZE,n,i
- fib1 0 fib2 1
- for (i 3 i lt SIZE i)
- fibi -1
-
- printf("Please enter a non-negative number\n")
- scanf("d",n)
- printf("fib(d) d\n",n,fib_rec_mem(n,fib))
- return 0
67Fibonacci with Memoization
- int fib_rec_mem(int n,int fib)
- if (fibn ! -1)
- return fibn
- fibn fib_rec_mem(n-1,fib)
fib_rec_mem(n-2,fib) - return fibn
68Odd-Even
- Given a function int odd(int n)
- Return 1 on n that are odd, 0 on even n
- Write a function int even(int n) that
- Return 1 on n that are even, 0 on odd n
- This is easy
69Odd-Even
int odd(int n) if (n1) return 1 if
(even(n-1) 1) return 1 return 0
- int even(int n)
- if (n0)
- return 1
- if (odd(n-1) 1)
- return 1
- return 0
70Decimal-gtBinary _at_ home
- We want to print the binary representation of a
decimal number. - Example
- 0 -gt 0
- 8 -gt 1000
- 12 -gt 1100
- 31 -gt 11111
71Decimal-gtBinary Recursively
- The conversion of a number d from decimal to
binary - If d is 0 or 1 write d to the left and stop.
Else - If d is even, write 0 to the left.
- If d is odd, write 1 to the left.
- Repeat recursively with floor(d/2).
72Example d 14
d Output at the end of stage. Action at the end of stage
14 0 Insert 0 to left of output divide d by 2
7 10 Insert 1 to left of output divide d by 2 and round down
3 110 Insert 1 to left of output divide d by 2 and round down
1 1110 Insert 1 to left of output return.
73How to implement this?
- We want to print the binary representation of a
decimal number. - dec2bin_rec.c .
- Notice that we first call the function
recursively and only afterwards print it (why?).
74Solution
- void dec2bin_rec(int d)
- if(d 0 d 1)
- printf("d",d)
- return
-
- / call recursively first /
- dec2bin_rec(d/2)
- printf("d", d2)