SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

recursive problem_solving
recursive problem_solvingrecursive problem_solving
recursive problem_solvingRajendran
 
[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 PrecedenceMuhammad Hammad Waseem
 
Presentation on function
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
evaluating recursive_applications
evaluating recursive_applicationsevaluating recursive_applications
evaluating recursive_applicationsRajendran
 
parallelizing Trapezoidal rule
parallelizing Trapezoidal rule parallelizing Trapezoidal rule
parallelizing Trapezoidal rule zainab mohammed
 
Call by value
Call by valueCall by value
Call by valueDharani G
 

Was ist angesagt? (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
 

Andere mochten auch

VBA for technical writers
VBA for technical writersVBA for technical writers
VBA for technical writersTCUK
 
15 functions
15 functions15 functions
15 functionsfyjordan9
 
16 subroutine
16 subroutine16 subroutine
16 subroutinefyjordan9
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++Praveen M Jigajinni
 
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 Variablemisey_margarette
 

Andere mochten auch (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
 

Ähnlich wie 17recursion

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.pptAbhishekkumarsingh630054
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Recursion in C
Recursion in CRecursion in C
Recursion in Cv_jk
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsAbdullah Al-hazmy
 
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 1Jeevan 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.docxvrickens
 
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].pdfTeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdfTeshaleSiyum
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptxajajkhan16
 
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.pptx15AnasKhan
 

Ähnlich wie 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
 

Mehr von fyjordan9

Mehr von 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
 

Kürzlich hochgeladen

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 

Kürzlich hochgeladen (20)

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 

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