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

Can't run android test suite: Exception in thread “main” java.lang.ClassNotFoundException
How to prevent MongoDB from printing cluster update logs to console?
Android Gestures code explanation
Web service running constantly and Tomcat app displays results
Openstreetmap to identify routes
Set max number of map markers per day
Android Studio - Passing a title and description through ListView
Spring 4.2: java.lang.NoClassDefFoundError: Could not initialize class org.springframework.test.web.servlet.result.MockMvcResultHandlers
Do I need both jdk and jre?
Error while connecting remote connection SFTP in Netbenas
Rest api pagination
How would I convert an integer value into an ASCII Character value in Java
Cast java Object to a generic type with a known type parameter
How can I print numbers within given range in random order
How to check for null demited values in Java?
Java : JTextArea can not append()

Categories

HOME
cakephp
client
openlayers
bluetooth
deployment
vbscript
listview
keras
urbancode
tizen
cplex
youtube-dl
alpha
fingerprint
pivotal-cloud-foundry
gorm
adobe-analytics
reveal.js
spring-xd
modelica
apache-cayenne
netflix
pugjs
swingx
kryo
firebase-crash-reporting
vlsi
tdd
sparse-matrix
wpfdatagrid
windows-dev-center
vxworks
restlet
tasker
hp-ux
powershell-dsc
nomethoderror
eclipse-gef
sencha-touch-2.3
outlook-2013
parentheses
prolog-setof
magma
pg-dump
typed-lambda-calculus
knpmenubundle
memory-alignment
hittest
sonarlint-vs
elements
home-directory
dstu2-fhir
underscore.js-templating
thrust
google-web-starter-kit
packagist
pervasive-sql
qtableview
thredds
python-3.2
riak-cs
unity5.2.3
geonetwork
system.reflection
relocation
issuu
php-5.4
mesa
xceed-datagrid
android-2.2-froyo
balanced-payments
imdbpy
html5-notifications
pyhdf
excel-2003
gwt-rpc
dataadapter
smtp-auth
simba
browser-detection
parametric-equations
doh
heartbeat
genshi
substrings
uimenucontroller
swing-app-framework
sustainable-pace
signal-handling

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