Recursion, How does it loop here?

In summary, the conversation discusses the regret of skipping lectures and the difficulty of understanding recursion. The speaker also asks for clarification on how recursion functions and shares an example of using recursion to find the largest number in a vector. They also advise others to attend lectures for better understanding.
  • #1
Nano-Passion
1,291
0
I skipped all of my lectures since I usually like to understand things on my own but I am regretting it for recursion. I don't know why but I am finding it quite unintuitive and confusing. I have an exam coming tomorrow so I have to make sure that I understand it correctly.

I know that recursion functions as a loop due to its circulatory nature. However, I don't know exactly what is going on within the program to make it loop around here. Can someone clarify?

Find largest number using recursion.

function output = maxrec(vec)
% finds the largest number in a vector recursively.
% base case, if the vector has a length of 1, reuturn the single element.
% if not, return the larger of the 2 numbers

if length(vec) == 1
output = vec;
else
current = maxrec(vec(1,1));
next = maxrec(vec(1, 2:1:length(vec)));
if current > next
output = current;
else
output = next;
end
end
end
 
Technology news on Phys.org
  • #2
You are at the wrong forums.

Next time go to lecture.
 

Related to Recursion, How does it loop here?

1. How does recursion work?

Recursion is a programming technique where a function calls itself repeatedly until a certain condition is met. This allows for the solution of complex problems by breaking them down into smaller, simpler problems.

2. What is the base case in recursion?

The base case is the condition that stops the recursive function from calling itself. It is the simplest form of the problem that can be solved without further recursion.

3. How does recursion differ from iteration?

Recursion and iteration both involve repetition, but recursion involves a function calling itself, while iteration uses loops to repeat a block of code.

4. What are the advantages of using recursion?

Recursion can make code more concise and easier to read, especially for problems that can be broken down into smaller subproblems. It can also be more efficient for certain problems.

5. What are some common pitfalls of recursion?

Recursion can easily lead to infinite loops if the base case is not properly defined. It can also be less efficient for certain problems than an iterative solution. Additionally, using too many recursive function calls can lead to stack overflow.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
11
Views
828
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
925
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
12K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Introductory Physics Homework Help
Replies
1
Views
183
Back
Top