SlideShare une entreprise Scribd logo
1  sur  13
Basic Scientific Programming
Recursion
Recursion




All of the examples considered thus far
involved a main program referencing a
subprogram or one subprogram
referencing another.
A subprogram may also reference itself,
this is called Recursion.
Ex: n!




n! = 1 * 2* 3* … * n
0! = 1
1! = 1
2! = 1 * 2 = 2
3! = 2! * 3 = 2*3 = 6
4! = 3! * 4 = 6*4 = 24
It is clear that once one factorial has been
calculated, it can be used to calculate the
next one.
n! = n * (n-1)!


A function is defined recursively if the
definition consists of two parts:




A base case: in which the value of the
function is specified for one or more values
of the argument(s) 0! = 1
A recursive step: in which the function’s
value for a current value of argument is
defined in terms of a previously defined
function value.
n>0 n! = n* (n-1)!
4!


4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1!
1! = 1* 0!
0! =1
4!


4!=4*3!=4*6=24
3!= 3*2!=3*2=6
2!=2*1!=2*1=2
1!=1*0!=1 * 1 = 1
0! =1
Notes




Subprograms may be declared to be
recursive by attaching the word
RECURSIVE at the beginning of the
subprogram heading.
For a recursive function, a RESULT
clause must be attached at the end of
the function heading.
Notes




Return value will be assigned to the
result variable instead of the function
name.
The type of the function is specified by
declaring the type of the result variable.


Recursive function factorial(n) result(fact)
integer:: fact
integer,intent(in)::n
if(n==0) then
fact = 1
else
fact = factorial(n-1) * n
end if
End Function factorial






Fact = 1
do I = 1,5
fact = fact * I
end do
Nonrecursive programs may execute more
rapidly and utilize less memory than
corresponding recursive programs.
For some problems, recursion is the most
natural and straightforward technique.
Ex:


Recursive function f(n) result(f_value)
integer:: f_value
integer,intent(in) :: N
if(n==0) then
f_value = 0
else
f_value = n+ f(n-1)
end if
end function f
!Find f(5), f(0)
Ex:


recursive function f(num1,num2) result(f_val)
integer:: f_value
integer,intent(in):: num1,num2
if (num1>num2) then
f_val = 0
Else if(num2==num1+1) then
f_val = 1
Else
f_val = f(num1+1,num2-1) + 2
End if
end function f !! F(2,2), F(1,5), F(8,3)
xn


Recursive function f(x,n) result(x2n)
integer:: x2n
integer,intent(in):: x,n
if(n==0)
x2n = 1
else
x2n = f(x,n-1) * x
end if
end function f

Contenu connexe

Tendances

Call by value
Call by valueCall by value
Call by value
Dharani G
 

Tendances (20)

[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
 
recursive problem_solving
recursive problem_solvingrecursive problem_solving
recursive problem_solving
 
C function
C functionC function
C function
 
C# p8
C# p8C# p8
C# p8
 
Ecet 370 week 1 lab
Ecet 370 week 1 labEcet 370 week 1 lab
Ecet 370 week 1 lab
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 
Method parameters in c#
Method parameters in c#Method parameters in c#
Method parameters in c#
 
Function in c
Function in cFunction in c
Function in c
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Functions in C
Functions in CFunctions in C
Functions in C
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
evaluating recursive_applications
evaluating recursive_applicationsevaluating recursive_applications
evaluating recursive_applications
 
parallelizing Trapezoidal rule
parallelizing Trapezoidal rule parallelizing Trapezoidal rule
parallelizing Trapezoidal rule
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
Function in c
Function in cFunction in c
Function in c
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Call by value
Call by valueCall by value
Call by value
 
First Look at Pointers
First Look at PointersFirst Look at Pointers
First Look at Pointers
 

En vedette (8)

8 if
8 if8 if
8 if
 
VBA for technical writers
VBA for technical writersVBA for technical writers
VBA for technical writers
 
Vba class 4
Vba class 4Vba class 4
Vba class 4
 
15 functions
15 functions15 functions
15 functions
 
Aitken process
Aitken processAitken process
Aitken process
 
16 subroutine
16 subroutine16 subroutine
16 subroutine
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 
Linear Equations and Inequalities in One Variable
Linear Equations and Inequalities in One VariableLinear Equations and Inequalities in One Variable
Linear Equations and Inequalities in One Variable
 

Similaire à 17recursion

Recursion in C
Recursion in CRecursion in C
Recursion in C
v_jk
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
Jeevan Raj
 
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxJLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
vrickens
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 

Similaire à 17recursion (20)

Functions
FunctionsFunctions
Functions
 
algo_vc_lecture8.ppt
algo_vc_lecture8.pptalgo_vc_lecture8.ppt
algo_vc_lecture8.ppt
 
FUNDAMETAL ALG.ppt
FUNDAMETAL ALG.pptFUNDAMETAL ALG.ppt
FUNDAMETAL ALG.ppt
 
Functions
Functions Functions
Functions
 
lecture 10 Recursive Function and Macros.ppt
lecture 10 Recursive Function and Macros.pptlecture 10 Recursive Function and Macros.ppt
lecture 10 Recursive Function and Macros.ppt
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
14. Recursion.pdf
14. Recursion.pdf14. Recursion.pdf
14. Recursion.pdf
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
M2-Recursion.pptx
M2-Recursion.pptxM2-Recursion.pptx
M2-Recursion.pptx
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
 
Looping
LoopingLooping
Looping
 
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxJLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptx
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
 
Recursion.ppt
 Recursion.ppt Recursion.ppt
Recursion.ppt
 

Plus de fyjordan9

Plus de fyjordan9 (14)

14 arrays
14 arrays14 arrays
14 arrays
 
13 arrays
13 arrays13 arrays
13 arrays
 
12 doloops
12 doloops12 doloops
12 doloops
 
11 doloops
11 doloops11 doloops
11 doloops
 
10 examples for if statement
10 examples for if statement10 examples for if statement
10 examples for if statement
 
9 case
9 case9 case
9 case
 
7 files
7 files7 files
7 files
 
6 read write
6 read write6 read write
6 read write
 
5 format
5 format5 format
5 format
 
4 design
4 design4 design
4 design
 
3 in out
3 in out3 in out
3 in out
 
2 int real
2 int real2 int real
2 int real
 
1 arithmetic
1 arithmetic1 arithmetic
1 arithmetic
 
PHYS303
PHYS303PHYS303
PHYS303
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Dernier (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

17recursion

  • 2. Recursion   All of the examples considered thus far involved a main program referencing a subprogram or one subprogram referencing another. A subprogram may also reference itself, this is called Recursion.
  • 3. Ex: n!   n! = 1 * 2* 3* … * n 0! = 1 1! = 1 2! = 1 * 2 = 2 3! = 2! * 3 = 2*3 = 6 4! = 3! * 4 = 6*4 = 24 It is clear that once one factorial has been calculated, it can be used to calculate the next one. n! = n * (n-1)!
  • 4.  A function is defined recursively if the definition consists of two parts:   A base case: in which the value of the function is specified for one or more values of the argument(s) 0! = 1 A recursive step: in which the function’s value for a current value of argument is defined in terms of a previously defined function value. n>0 n! = n* (n-1)!
  • 5. 4!  4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1* 0! 0! =1
  • 7. Notes   Subprograms may be declared to be recursive by attaching the word RECURSIVE at the beginning of the subprogram heading. For a recursive function, a RESULT clause must be attached at the end of the function heading.
  • 8. Notes   Return value will be assigned to the result variable instead of the function name. The type of the function is specified by declaring the type of the result variable.
  • 9.  Recursive function factorial(n) result(fact) integer:: fact integer,intent(in)::n if(n==0) then fact = 1 else fact = factorial(n-1) * n end if End Function factorial
  • 10.    Fact = 1 do I = 1,5 fact = fact * I end do Nonrecursive programs may execute more rapidly and utilize less memory than corresponding recursive programs. For some problems, recursion is the most natural and straightforward technique.
  • 11. Ex:  Recursive function f(n) result(f_value) integer:: f_value integer,intent(in) :: N if(n==0) then f_value = 0 else f_value = n+ f(n-1) end if end function f !Find f(5), f(0)
  • 12. Ex:  recursive function f(num1,num2) result(f_val) integer:: f_value integer,intent(in):: num1,num2 if (num1>num2) then f_val = 0 Else if(num2==num1+1) then f_val = 1 Else f_val = f(num1+1,num2-1) + 2 End if end function f !! F(2,2), F(1,5), F(8,3)
  • 13. xn  Recursive function f(x,n) result(x2n) integer:: x2n integer,intent(in):: x,n if(n==0) x2n = 1 else x2n = f(x,n-1) * x end if end function f