02 Recursive
02 Recursive
Programming II
Topic: Recursive function
1
Outline
▪ Review on Function
2
Review on Function
3
Function
❑ What and Why?
▪ A function is a block of code that performs a specific task. A function may
return a value.
▪ Why function?
✓ A large and complex program can be divided into smaller programs
✓ assign task and work in team is easy
4
Function
❑ Type of function
5
Syntax C language
❑ Create Function with returning value
c) Parameter of a function
It has parameter type and parameter name
If have more than one, separate them by comma
type functionName(type parameter1){
... .. ... b) Name of function
... .. ... it should start with a verb
return value;
} int sumTwoNumber(int n1, int n2){
int s;
s=n1+n2;
a) Type of value returning d) Return value return s;
from the function It must be the same data type as a) }
return value;
}
void greetMessage(char name[20]){
printf(“Hi, %s”, name);
printf(“Welcome back!”);
a) Void means no returning d) No need to return value since we
value from the function use void in a) }
8
Syntax C language
❑ How to execute the created function?
▪ The execution of a C program begins from the main( ) function.
▪ Our user-defined function can be executed by calling it from main() function
Function
User-defined
Local variables
the function
res=(a+b)*2;
✓ Its value will be destroyed after the function n++;
finish executing return res;
Output: }
int main(){
int num=10;
▪ Global value is variable that creates int res=2;
outside the function below all library inclusion printf("*Start program \n");
✓ Its value can be accessed from any functions printf(“%d”, sum(2,5));
res = sum(2,5)
✓ Its value will be destroyed after the whole printf(“%d”, res);
program finish executing printf(“%d”, n);
Call created function
} 10
Recursive Function
11
Recursive
❑ Definition
▪ A recursion is an algorithm which calls itself with smaller (or simpler) input values
12
Recursive
❑ Definition
▪ Recursive function is a function that calls itself repetitively until certain condition is satisfied.
It works like a loop
Remark: When call itself again, we should update its parameter in order to reach the stopping condition
▪ If-else statement
▪ A return statement
13
Recursive
❑ Example of recursion in factorial
▪ The subproblem (n-1)! is the same as the initial problem, BUT a simpler case
since n-1 < n
14
How to do it in coding?
15
Recursive
❑ Example of recursion in calculating a factorial number
16
Example of recursion in
Suit Fibonacci
0 1 1 2 3 5 8 13 21 34
17
Recursive
❑ Example of recursion in Suit Fibonacci
18
Recursive
❑ Example of recursion Summation
▪ Base case is a case that it does not call to the main problem. It stops recursive
20
Recursive: Direct and Indirect
❑ Definition
▪ Direct recursive
▪ Subproblem uses the main problem to call for defining the result
▪ Indirect recursive
▪ Subproblem A calls subproblem B
▪ Example
f(x)=1 if x=1
f(x)=1 if x=1 or x=2 f(x)=g(x)+2 if x>1
f(x)=f(x-1)*f(x-2) if x>2 g(x)=0 if x=2
g(x)=f(x)-1 if x>2
Direct recursive Indirect recursive 21
Practice
Write a C++ program using recursive to ….
22
Q&A
23