java


Recursion clarification


I just wanted to make sure that I was completely solid on recursion. I have used it in a bunch of applications, but realized that when someone asked me to define it (a newer programmer asked this), I was a bit shaky on the definition and had a bit of trouble explaining it. I just wanted to reach out to a large programming community to make sure I was on the right track.
From what I know, recursion in computer science is when some answers to a given problem or check (i.e. an if statement) depend on something else related to the same method. A way to solve this could be a function calling on itself (which the majority of programming languages support). I wrote a simple Fibonacci program below:
public int fib(int n) {
if(n <= 1) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}
Let me know if I'm on the right track. Also, I am aware that there are similar questions on recursion, but please do not close this question as a duplicate because this is a more general question not limited to a specific language, but more a concept on what recursion is.
Thanks,
brld
You're on the right track. I would break this up into pieces:
Definition: By dictionary definition, recursion is a a process calling itself. This call is usually direct, as in your example, but can also be indirect: f1 and f2 call each other, but not themselves.
Example: Just as you did ... show a well-known function with an easily understood recursive definition. I usually use factorial, since it has only one recursive call; then I present the Fibonacci case.
Mechanics: Describe the critical properties of base case (what makes it stop, at last) and simplification (reduce the problem before you recur).
Proper use: Pretty much any real programming application with a recursive description will have an iterative (looping) solution that takes less computing time. However, if the natural description is recursive, it's quite possible that the most efficient solution in the long run is recursive. Consider repair and maintenance resources in addition to mere execution cycles, and remember that FLOPS get cheaper every month.

Related Links

androidstudio constructor error
NaN Error w/ Quadratic Equation Calculator
Java - Use binarySearch to find # of occurrences of certain element in array, without If
Injecting EJB in spring
Setting request body on JAX-RS 2.0 Client API With RestEasy
Morphia #Version Not Working
Threadpool Deadlock: designing against or detecting
Random array from resource Android
JAXB: How to convert Map to array-like format
Can't get SQL query results to display on a JSP Page table?
Extended child object inheritance in Hibernate
Pass Data from Fragment to Fragment Through Button Click
Escaping individual characters in a character class
cannot compile a simple hbase java program
java.util.NoSuchElementException Error?
Java 8 Method references called on a local variable

Categories

HOME
ibm-bluemix
compiler-construction
vim
osgi
server
smarty
layout
framework7
yarn
malloc
ip
datastax-java-driver
mouse
swagger-ui
amazon-cloudformation
cloudkit
dacpac
kibana-4
here-api
phaser
messages
percona
serverless-framework
libtiff
footer
ab-testing
google-cloud-speech
crystal-reports-2008
xlsxwriter
accessor
core-text
claims-based-identity
crosstab
tdd
su
apache-commons-io
nhapi
filezilla
elasticsearch-ruby
bluestacks
google-rich-snippets
yadcf
noraui
geopositioning
overlap
devextreme
main
bitbucket-pipelines
xcode-extension
gesture
jna
greenrobot-eventbus
netcdf4
s
wso2carbon
taffy
alphabet
sqldf
mcafee
objective-c-swift-bridge
deadbolt-2
android-fonts
gridpane
file-writing
pcf
ctest
impresspages
ready-api
packagist
oauth2client
msys
collapse
ruby-2.2
emailrelay
cakephp-3.1
tableau-online
ideamart
remobjects
kineticjs
pick
code-access-security
onactivityresult
android-radiobutton
contenttype
mechanize-ruby
message-driven-bean
ccss
spring-validator
distutils
ohm
removeclass
pendrive
asyncfileupload
punbb
doh
data-loss
blackberry-playbook
xtype
mongomapper
jmock
xfbml
fixed-width
iweb
microsoft-virtualization
photoshop-cs4

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App