Calling the same function from within the function??
Mostra commenti meno recenti
So I ran into this very interesting file from the file exchange...
Where the function calls itself around line 140 (its between 138 thru 144 as I started to play with some things...). This was wild to me. I have never attempted this and was wondering if there are other examples out there.
Does this work for C++ or Fortran?
Comments? Why is this blowing my mind?
Risposta accettata
Più risposte (3)
ES
il 9 Ott 2013
2 voti
It is mere recursion supported by most high level languages.
Jan
il 9 Ott 2013
1 voto
I'm convinced that recursion does not blow your mind. You only have to get familiar with it. Then it is useful in many situations. A standard example for a recursive definition is the Fibonacci sequence (ask WikiPedia for details). And this is also a good example, why recursive functions are in many cases not efficient.
Another typical example are "Divide and Conquer" algorithms: The program splits the problem into two halves and provide both as inputs to itself until the problems are such small, that a solution is trivial. E.g. you can find the maximum value of a vector by splitting the vector until both parts have a single element only. This is a nice solution, but not fast.
1 Commento
Marc
il 9 Ott 2013
Simon
il 9 Ott 2013
1 voto
Hi!
For Fortran77 programs recursive functions (or subroutines) are not possible, as a standard compiler gives you a compile time error. Altough there are ways to trick the compiler ...
Fortran90/95 supports recursion as long as the specific function/subroutine is explicitly labeled as a recursive function.
Categorie
Scopri di più su Fortran with MATLAB in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!